diff --git a/commands/games-sp/anime-score.js b/commands/games-sp/anime-score.js new file mode 100644 index 00000000..c40ff4fa --- /dev/null +++ b/commands/games-sp/anime-score.js @@ -0,0 +1,98 @@ +const Command = require('../../structures/Command'); +const { MessageEmbed } = require('discord.js'); +const request = require('node-superfetch'); +const { stripIndents } = require('common-tags'); +const searchGraphQL = stripIndents` + query ($page: Int, $type: MediaType, $isAdult: Boolean) { + Page (page: $page) { + pageInfo { + total + } + media (type: $type, popularity_greater: 1000, averageScore_not: 0, isAdult: $isAdult) { + id + averageScore + title { + english + } + coverImage { + large + medium + } + } + } + } +`; + +module.exports = class AnimeScoreCommand extends Command { + constructor(client) { + super(client, { + name: 'anime-score', + aliases: ['guess-anime-score'], + group: 'games-sp', + memberName: 'anime-score', + description: 'See if you can guess what a random anime\'s score is.', + clientPermissions: ['EMBED_LINKS'], + credit: [ + { + name: 'AniList', + url: 'https://anilist.co/', + reason: 'API', + reasonURL: 'https://anilist.gitbook.io/anilist-apiv2-docs/' + } + ] + }); + } + + async run(msg) { + try { + const data = await this.getRandomAnime(msg.channel.nsfw); + const embed = new MessageEmbed() + .setColor(0x02A9FF) + .setAuthor('AniList', 'https://i.imgur.com/iUIRC7v.png', 'https://anilist.co/') + .setThumbnail(anime.coverImage.large || anime.coverImage.medium || null) + .setTitle(anime.title.english || anime.title.romaji) + .setFooter(anime.id); + await msg.reply('**You have 15 seconds, what score do you think this anime has?**', { embed }); + const filter = res => { + if (res.author.id !== msg.author.id) return false; + return Boolean(Number.parseInt(res.content, 10)); + }; + const msgs = await msg.channel.awaitMessages(filter, { + max: 1, + time: 15000 + }); + if (!msgs.size) return msg.reply(`Sorry, time is up! It was **${anime.averageScore}%**.`); + const ans = Number.parseInt(msgs.first().content, 10); + if (Math.abs(ans - anime.averageScore) <= 10) return msg.reply(`Close! It was **${anime.averageScore}%**.`); + if (ans !== anime.averageScore) return msg.reply(`Nope, sorry, it was **${anime.averageScore}%**.`); + return msg.reply(`Nice job! It was **${anime.averageScore}%**!`); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } + + async getRandomAnime(nsfw) { + const { body: initialBody } = await request + .post('https://graphql.anilist.co/') + .send({ + variables: { + page: 0, + type: 'ANIME', + isAdult: Boolean(nsfw) + }, + query: searchGraphQL + }); + const selectedAnime = Math.floor(Math.random() * initialBody.data.Page.pageInfo.total); + const { body } = await request + .post('https://graphql.anilist.co/') + .send({ + variables: { + page: Math.ceil(selectedAnime / 50), + type: 'ANIME', + isAdult: Boolean(nsfw) + }, + query: searchGraphQL + }); + return body.data.Page.media[selectedAnime % 50]; + } +}; diff --git a/package.json b/package.json index fd0eb87b..9c62f0cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "138.1.0", + "version": "138.2.0", "description": "Your personal server companion.", "main": "Xiao.js", "private": true,