Add Personal Score to Manga

This commit is contained in:
Dragon Fire
2020-03-21 20:42:56 -04:00
parent a63d8af88f
commit fc0d59178a
3 changed files with 55 additions and 16 deletions
+2 -10
View File
@@ -69,20 +69,12 @@ const statuses = {
NOT_YET_RELEASED: 'Unreleased',
CANCELLED: 'Cancelled'
};
const userStatuses = {
CURRENT: 'Watching',
PLANNING: 'Planning',
COMPLETED: 'Completed',
DROPPED: 'Dropped',
PAUSED: 'Paused',
REPEATING: 'Rewatching'
};
module.exports = class AnimeCommand extends Command {
constructor(client) {
super(client, {
name: 'anime',
aliases: ['anilist-anime', 'anilist'],
aliases: ['anilist-anime', 'anilist', 'ani', 'myanimelist', 'mal', 'mal-score'],
group: 'search',
memberName: 'anime',
description: 'Searches AniList for your query, getting anime results.',
@@ -117,7 +109,7 @@ module.exports = class AnimeCommand extends Command {
const id = await this.search(query);
if (!id) return msg.say('Could not find any results.');
const anime = await this.fetchAnime(id);
await this.fetchPersonalList();
if (!this.personalList) await this.fetchPersonalList();
const entry = this.personalList.find(ani => ani.mediaId === id);
const malScore = await this.fetchMALScore(anime.idMal);
const embed = new MessageEmbed()
+52 -5
View File
@@ -4,10 +4,17 @@ const request = require('node-superfetch');
const cheerio = require('cheerio');
const { stripIndents } = require('common-tags');
const { cleanAnilistHTML } = require('../../util/Util');
const ANILIST_USERNAME = process.env.ANILIST_USERNAME || 'dragonfire535';
const searchGraphQL = stripIndents`
query ($search: String, $type: MediaType, $isAdult: Boolean) {
anime: Page (perPage: 1) {
results: media (type: $type, isAdult: $isAdult, search: $search) { id }
anime: Page (perPage: 10) {
results: media (type: $type, isAdult: $isAdult, search: $search) {
id
title {
english
romaji
}
}
}
}
`;
@@ -18,9 +25,12 @@ const resultGraphQL = stripIndents`
idMal
title {
english
userPreferred
romaji
}
coverImage {
large
medium
}
coverImage { large }
startDate { year }
description(asHtml: false)
siteUrl
@@ -33,6 +43,20 @@ const resultGraphQL = stripIndents`
}
}
`;
const personalGraphQL = stripIndents`
query ($name: String, $type: MediaType) {
MediaListCollection(userName: $name, type: $type) {
lists {
entries {
mediaId
score(format: POINT_10)
status
}
name
}
}
}
`;
const statuses = {
FINISHED: 'Finished',
RELEASING: 'Releasing',
@@ -70,6 +94,8 @@ module.exports = class MangaCommand extends Command {
}
]
});
this.personalList = null;
}
async run(msg, { query }) {
@@ -77,18 +103,21 @@ module.exports = class MangaCommand extends Command {
const id = await this.search(query);
if (!id) return msg.say('Could not find any results.');
const manga = await this.fetchManga(id);
if (!this.personalList) await this.fetchPersonalList();
const entry = this.personalList.find(manga => manga.mediaId === id);
const malScore = await this.fetchMALScore(manga.idMal);
const embed = new MessageEmbed()
.setColor(0x02A9FF)
.setAuthor('AniList', 'https://i.imgur.com/iUIRC7v.png', 'https://anilist.co/')
.setURL(manga.siteUrl)
.setThumbnail(manga.coverImage.large || manga.coverImage.medium || null)
.setTitle(manga.title.english || manga.title.userPreferred)
.setTitle(manga.title.english || manga.title.romaji)
.setDescription(manga.description ? cleanAnilistHTML(manga.description) : 'No description.')
.addField(' Status', statuses[manga.status], true)
.addField(' Chapters / Volumes', `${manga.chapters || '???'}/${manga.volumes || '???'}`, true)
.addField(' Year', manga.startDate.year || '???', true)
.addField(' Average Score', manga.meanScore ? `${manga.meanScore}/100` : '???', true)
.addField(` ${ANILIST_USERNAME}'s Score`, entry && entry.score ? `${entry.score}/10` : '?/10', true)
.addField(` MAL Score`, malScore ? `${malScore}/10` : '???', true);
return msg.embed(embed);
} catch (err) {
@@ -132,4 +161,22 @@ module.exports = class MangaCommand extends Command {
return null;
}
}
async fetchPersonalList() {
if (this.personalList) return this.personalList;
const { body } = await request
.post('https://graphql.anilist.co/')
.send({
variables: {
name: ANILIST_USERNAME,
type: 'MANGA'
},
query: personalGraphQL
});
const { lists } = body.data.MediaListCollection;
this.personalList = [];
for (const list of Object.values(lists)) this.personalList.push(...list.entries);
setTimeout(() => { this.personalList = null; }, 3.6e+6);
return this.personalList;
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "112.12.2",
"version": "112.12.3",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {