From eb07f55bd11541ff17b8a17adf7581f61aaa93e7 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Tue, 9 Jul 2019 20:12:12 -0400 Subject: [PATCH] Add Personal Data to AniList --- .env.example | 1 + commands/search/anime.js | 37 ++++++++++++++++++++++++++++++++++++- package.json | 2 +- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index d40258f8..4f1f885b 100644 --- a/.env.example +++ b/.env.example @@ -17,6 +17,7 @@ SILVER_FISH_EMOJI_NAME= # API Keys, IDs, and Secrets ALPHA_VANTAGE_KEY= +ANILIST_USERNAME= CLEVERBOT_KEY= CUSTOM_SEARCH_ID= DEVIANTART_ID= diff --git a/commands/search/anime.js b/commands/search/anime.js index 4804fe5c..c55bcdda 100644 --- a/commands/search/anime.js +++ b/commands/search/anime.js @@ -3,6 +3,7 @@ const { MessageEmbed } = require('discord.js'); const request = require('node-superfetch'); 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: 10) { @@ -40,6 +41,17 @@ const resultGraphQL = stripIndents` } } `; +const personalGraphQL = stripIndents` + query ($name: String, $type: MediaType) { + MediaListCollection(userName: $name, type: $type) { + entries { + mediaId + score(format: POINT_10) + progress + } + } + } +`; const seasons = { WINTER: 'Winter', SPRING: 'Spring', @@ -76,6 +88,8 @@ module.exports = class AnimeCommand extends Command { } ] }); + + this.personalList = null; } async run(msg, { query }) { @@ -83,6 +97,8 @@ 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(); + const entry = this.personalList.find(ani => ani.media.id === id); const embed = new MessageEmbed() .setColor(0x02A9FF) .setAuthor('AniList', 'https://i.imgur.com/iUIRC7v.png', 'https://anilist.co/') @@ -93,7 +109,10 @@ module.exports = class AnimeCommand extends Command { .addField('❯ Status', statuses[anime.status], true) .addField('❯ Episodes', anime.episodes || '???', true) .addField('❯ Season', anime.season ? `${seasons[anime.season]} ${anime.startDate.year}` : '???', true) - .addField('❯ Average Score', anime.meanScore ? `${anime.meanScore}/100` : '???', true); + .addField('❯ Average Score', anime.meanScore ? `${anime.meanScore}/100` : '???', true) + .addField(`❯ ${ANILIST_USERNAME}'s Score`, entry && entry.score ? `${entry.score}/10` : '???', true) + .addField(`❯ ${ANILIST_USERNAME}'s Progress`, + entry && entry.progress ? `${entry.progress}/${anime.episodes || ''}` : '???', true); return msg.embed(embed); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); @@ -126,4 +145,20 @@ module.exports = class AnimeCommand extends Command { }); return body.data.Media; } + + async fetchPersonalList() { + if (this.personalList) return this.personalList; + const { body } = await request + .post('https://graphql.anilist.co/') + .send({ + variables: { + name: ANILIST_USERNAME, + type: 'ANIME' + }, + query: personalGraphQL + }); + this.personalList = body.data.MediaListCollection.entries; + setTimeout(() => { this.personalList = null; }, 3.6e+6); + return this.personalList; + } }; diff --git a/package.json b/package.json index 0ef771ff..9740da2e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "107.0.1", + "version": "107.0.2", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {