From f1c7662e37505f76257130ca0909d2f4a6fa0247 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Thu, 19 Nov 2020 20:37:06 -0500 Subject: [PATCH] Anime Staff Command --- README.md | 4 +- commands/search/anime-character.js | 4 +- commands/search/anime-staff.js | 130 +++++++++++++++++++++++++++++ package.json | 2 +- 4 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 commands/search/anime-staff.js diff --git a/README.md b/README.md index 32c12900..256e8998 100644 --- a/README.md +++ b/README.md @@ -268,7 +268,7 @@ in the appropriate channel's topic to use it. ## Commands -Total: 559 +Total: 560 ### Utility: @@ -468,6 +468,7 @@ Total: 559 ### Search: * **anime-character:** Searches AniList for your query, getting character results. +* **anime-staff:** Searches AniList for your query, getting staff results. * **anime:** Searches AniList for your query, getting anime results. * **book:** Searches Google Books for a book. * **bulbapedia:** Searches Bulbapedia for your query. @@ -954,6 +955,7 @@ here. * anime ([API](https://anilist.gitbook.io/anilist-apiv2-docs/)) * anime-airing ([API](https://anilist.gitbook.io/anilist-apiv2-docs/)) * anime-character ([API](https://anilist.gitbook.io/anilist-apiv2-docs/)) + * anime-staff ([API](https://anilist.gitbook.io/anilist-apiv2-docs/)) * manga ([API](https://anilist.gitbook.io/anilist-apiv2-docs/)) - [Antidepressants or Tolkien](https://antidepressantsortolkien.now.sh/) * antidepressant-or-tolkien (Question Data) diff --git a/commands/search/anime-character.js b/commands/search/anime-character.js index 2894f4de..fed6dd24 100644 --- a/commands/search/anime-character.js +++ b/commands/search/anime-character.js @@ -29,7 +29,7 @@ const resultGraphQL = stripIndents` node { title { english - userPreferred + romaji } type siteUrl @@ -84,7 +84,7 @@ module.exports = class AnimeCharacterCommand extends Command { .setTitle(`${character.name.first || ''}${character.name.last ? ` ${character.name.last}` : ''}`) .setDescription(character.description ? cleanAnilistHTML(character.description, false) : 'No description.') .addField('❯ Appearances', trimArray(character.media.edges.map(edge => { - const title = edge.node.title.english || edge.node.title.userPreferred; + const title = edge.node.title.english || edge.node.title.romaji; return embedURL(`${title} (${types[edge.node.type]})`, edge.node.siteUrl); }), 5).join(', ')); return msg.embed(embed); diff --git a/commands/search/anime-staff.js b/commands/search/anime-staff.js new file mode 100644 index 00000000..9373725d --- /dev/null +++ b/commands/search/anime-staff.js @@ -0,0 +1,130 @@ +const Command = require('../../structures/Command'); +const { MessageEmbed } = require('discord.js'); +const request = require('node-superfetch'); +const { stripIndents } = require('common-tags'); +const { embedURL, cleanAnilistHTML, trimArray } = require('../../util/Util'); +const searchGraphQL = stripIndents` + query ($search: String) { + staff: Page (perPage: 1) { + results: staff (search: $search) { id } + } + } +`; +const resultGraphQL = stripIndents` + query ($id: Int!) { + Staff (id: $id) { + id + name { + first + last + } + image { + large + medium + } + description(asHtml: false) + siteUrl + characters(page: 1, perPage: 5) { + edges { + node { + name { + full + } + siteUrl + } + } + } + staffMedia(page: 1, perPage: 5) { + edges { + node { + title { + english + romaji + } + type + siteUrl + } + staffRole + } + } + } + } +`; +const types = { + ANIME: 'Anime', + MANGA: 'Manga' +}; + +module.exports = class AnimeStaffCommand extends Command { + constructor(client) { + super(client, { + name: 'anime-staff', + aliases: ['anilist-staff', 'staff', 'manga-staff', 'ani-staff'], + group: 'search', + memberName: 'anime-staff', + description: 'Searches AniList for your query, getting staff results.', + clientPermissions: ['EMBED_LINKS'], + credit: [ + { + name: 'AniList', + url: 'https://anilist.co/', + reason: 'API', + reasonURL: 'https://anilist.gitbook.io/anilist-apiv2-docs/' + } + ], + args: [ + { + key: 'query', + prompt: 'What staff member would you like to search for?', + type: 'string' + } + ] + }); + } + + async run(msg, { query }) { + try { + const id = await this.search(query); + if (!id) return msg.say('Could not find any results.'); + const staff = await this.fetchStaff(id); + const embed = new MessageEmbed() + .setColor(0x02A9FF) + .setAuthor('AniList', 'https://i.imgur.com/iUIRC7v.png', 'https://anilist.co/') + .setURL(staff.siteUrl) + .setThumbnail(staff.image.large || staff.image.medium || null) + .setTitle(`${staff.name.first || ''}${staff.name.last ? ` ${staff.name.last}` : ''}`) + .setDescription(staff.description ? cleanAnilistHTML(staff.description, false) : 'No description.') + .addField('❯ Voice Roles', staff.characters.edges.length + ? trimArray(staff.characters.edges.map(edge => embedURL(edge.name.full, edge.node.siteUrl)), 5).join(', ') + : 'None') + .addField('❯ Production Roles', staff.staffMedia.edges.length ? trimArray(staff.staffMedia.edges.map(edge => { + const title = edge.node.title.english || edge.node.title.romaji; + return embedURL(`${title} (${types[edge.node.type]})`, edge.node.siteUrl); + }), 5).join(', ') : 'None'); + return msg.embed(embed); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } + + async search(query) { + const { body } = await request + .post('https://graphql.anilist.co/') + .send({ + variables: { search: query }, + query: searchGraphQL + }); + if (!body.data.staff.results.length) return null; + return body.data.staff.results[0].id; + } + + async fetchStaff(id) { + const { body } = await request + .post('https://graphql.anilist.co/') + .send({ + variables: { id }, + query: resultGraphQL + }); + return body.data.Staff; + } +}; diff --git a/package.json b/package.json index 92b69cdd..738aaa90 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "119.44.2", + "version": "119.45.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {