From 880d07e3007a1e9177a63aa55efba25cbe28873e Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Sun, 19 Dec 2021 21:27:38 -0700 Subject: [PATCH] anime searching. this is literal hell --- commands/anime/char.js | 23 +++++++++++++++++++++++ util/anime/anisearch.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 util/anime/anisearch.js diff --git a/commands/anime/char.js b/commands/anime/char.js index fb22696..5e6d991 100644 --- a/commands/anime/char.js +++ b/commands/anime/char.js @@ -6,6 +6,8 @@ const Char = require('../../models/char'); const {Tag} = require('../../util/tag'); const {TagFilter} = require('../../util/tagfilter'); const ask = require('../../util/ask'); +const ans = require('../../util/anime/anisearch'); +const {Pagination} = require('../../util/pagination'); module.exports = { name: "char", @@ -73,6 +75,27 @@ module.exports = { options.anime = await ask(mesg, "What anime (or game) did the character appear in? If the character is an OC, say 'none'", 6000, true); if (!options.anime) {return;} if (options.anime.length > 75) {clearDM(); return dmch.send("The anime name can't be more than 75 characters!");} if (options.anime.trim().toLowerCase() === 'none') {options.anime = null;} + else { + let asr = await ans(mesg, client, options.anime.trim().toLowerCase()); + if (asr instanceof Pagination) { + await dmch.send({embeds: [asr.pages[0]]}); + let conf = await ask(mesg, "Is this the anime you meant?"); + if (!['y', 'yes', 'ye', 'n', 'no'].includes(conf.trim().toLowerCase())) {clearDM(); return dmch.send("You must specify yes or no! Please try again.");} + conf = ['y', 'yes', 'ye'].includes(conf.trim().toLowerCase()); + if (!conf) { + + } + } + else { + await dmch.send({embeds: [asr.embed]}); + let conf = await ask(mesg, "Is this the anime you meant?"); + if (!['y', 'yes', 'ye', 'n', 'no'].includes(conf.trim().toLowerCase())) {clearDM(); return dmch.send("You must specify yes or no! Please try again.");} + conf = ['y', 'yes', 'ye'].includes(conf.trim().toLowerCase()); + if (!conf) {return mesg.channel.send("Well, I've got nothing, then. If that doesn't match the anime you're looking for then I would try again with a more narrow search.");} + fn = asr.id; + } + options.anime = fn; + } options.gender = await ask(mesg, "What is the character's gender?", 60000, true); if (!options.name) {return;} if (options.gender.length > 75) {clearDM(); return dmch.send("That's one heck of a gender. Maybe like... abbreviate?");} diff --git a/util/anime/anisearch.js b/util/anime/anisearch.js new file mode 100644 index 0000000..c503e26 --- /dev/null +++ b/util/anime/anisearch.js @@ -0,0 +1,32 @@ +const fz = require('fuzzysort'); +const Discord = require('discord.js'); + +const Ani = require('../../models/anime'); + +const {Pagination} = require("../../util/pagination"); + +module.exports = async (message, client, search, type='top') => { + const res = fz.go(search, Array.from(client.misc.cache.anime.keys()), {threshold: -300, limit: 10}).sort((a,b)=>a.score-b.score).map(k => k.target); + const me = async (ani) => { + let an = await Ani.findOne({id: client.misc.cache.anime.get(ani)}); + return {embed: new Discord.MessageEmbed() + .setTitle(an.name) + .setAuthor('Anime Search', message.author.avatarURL()) + .addField('Info', `**Name:** ${an.name}\n**Japanese Name:** ${an.japname}\n\n**Publishers:** ${an.publishers}\n**Studios:** ${an.studios}`) + .addField('Description', an.plot) + .addField('Length', `**# of Seasons:** ${an.seasons}\n**# of Episodes:** ${an.episodes}`) + .addField('Airing', `**Began:** ${an.airStartDate}\n**Ended:** ${an.isComplete ? an.airEndDate : 'This anime is still airing!'}`) + .addField('Other', `**Genre(s):** ${an.genres}\n**Tags:** ${an.tags}\n**Characters:** ${an.characters}\n**Stream this at:** ${an.streamAt}`) + .setColor("c375f0") + .setImage(an.thumbnail) + .setFooter('Natsuki', client.user.avatarURL()) + .setTimestamp(), id: an.id}; + }; + if (res.length > 1) { + let tp = []; + await res.forEach(async ca => tp.push(await me(ca))); + let pag = new Pagination(message.channel, tp.map(k => k.embed), message, client, true); + pag.ids = tp.map(k => k.id); + return pag; + } else {return await me(res[0]);} +} \ No newline at end of file