From 30c481fc596d255ba3b0b31fb5e1ae8531ab3333 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Mon, 20 Dec 2021 03:13:18 -0700 Subject: [PATCH] char searching is a thing now --- commands/anime/char.js | 35 +++++++++++++++++++++++++++++++++++ util/anime/anisearch.js | 4 ++-- util/anime/charsearch.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 util/anime/charsearch.js diff --git a/commands/anime/char.js b/commands/anime/char.js index 9e1053d..772e339 100644 --- a/commands/anime/char.js +++ b/commands/anime/char.js @@ -8,6 +8,7 @@ const {Tag} = require('../../util/tag'); const {TagFilter} = require('../../util/tagfilter'); const ask = require('../../util/ask'); const ans = require('../../util/anime/anisearch'); +const chs = require('../../util/anime/charsearch'); const {Pagination} = require('../../util/pagination'); module.exports = { @@ -178,5 +179,39 @@ module.exports = { rc.on("end", collected => {if (!collected.size) {return message.author.send("Looks like you ran out of time! Try again?");}}); } catch {return message.author.send("Hmm... there was some kind of error when I tried to submit that character. Try again, and if it keeps not working, then go yell at my devs!");} } + if (['s', 'search'].includes(args[0].trim().toLowerCase())) { + args.shift(); + let asr = await chs(message, client, args.join(" ").trim().toLowerCase(), -100000); + if (asr === 0) { + return message.channel.send("That search returned no results! Try again?"); + } else if (asr instanceof Pagination) { + await asr.start({user: message.author.id, startPage: 1, endTime: 60000}); + } else { + await message.channel.send({embeds: [asr.embed]}); + } + return; + } + if (['v', 'view'].includes(args[0].trim().toLowerCase())) { + args.shift(); + let asr = await chs(message, client, args.join(" ").trim().toLowerCase(), -700); + if (asr === 0) { + return message.channel.send("That search returned no results! Try again?"); + } else if (asr instanceof Pagination) { + await asr.start({user: message.author.id, startPage: 1, endTime: 60000}); + } else { + await message.channel.send({embeds: [asr.embed]}); + } + return; + } + if (['reject'].includes(args[0].trim().toLowerCase())) { + let tu = await UserData.findOne({uid: message.author.id}); + if (!tu || !tu.staff) {await message.channel.send("Since you aren't a Natsuki Staff member, you can't reject character submissions!");} + let tr = await CharData.findOne({id: args[1].toLowerCase()}); + if (!tr) {return message.reply("That character submission doesn't seem to exist!");} + if (tr.queued !== true) {return message.reply("That character was already accepted, so you can't reject it.");} + return await CharData.deleteOne({id: args[1].toLowerCase()}) + .then(() => {return message.channel.send("I got that submission out of here!");}) + .catch(() => {return message.reply("It seems that submission wasn't deleted for some reason. \*insert head scratching*");}); + } } }; \ No newline at end of file diff --git a/util/anime/anisearch.js b/util/anime/anisearch.js index ac81b46..be36b57 100644 --- a/util/anime/anisearch.js +++ b/util/anime/anisearch.js @@ -8,7 +8,7 @@ const {Pagination} = require("../../util/pagination"); module.exports = async (message, client, search, threshold=-10000, type='top') => { const me = async (ani) => { - let an = await Ani.findOne({id: client.misc.cache.anime.get(ani)}); + let an = ani.plot ? ani : await Ani.findOne({id: client.misc.cache.anime.get(ani)}); let chs = []; for (let i = 0; i < an.characters.length; i++) { let tch = await Char.findOne({id: an.characters[i]}); @@ -29,7 +29,7 @@ module.exports = async (message, client, search, threshold=-10000, type='top') = }; let attF = await Ani.findOne({id: search.trim().toLowerCase()}); - if (attF) {return await me(res[0]);} + if (attF) {return await me(attF);} const res = fz.go(search, Array.from(client.misc.cache.anime.keys()), {threshold: threshold, limit: 10}).sort((a,b)=>a.score-b.score).map(k => k.target); if (res.length === 0) {return 0;} diff --git a/util/anime/charsearch.js b/util/anime/charsearch.js new file mode 100644 index 0000000..b96a0ee --- /dev/null +++ b/util/anime/charsearch.js @@ -0,0 +1,36 @@ +const fz = require('fuzzysort'); +const Discord = require('discord.js'); + +const Ani = require('../../models/anime'); +const Char = require('../../models/char'); + +const {Pagination} = require("../../util/pagination"); + +module.exports = async (message, client, search, threshold=-10000, type='top') => { + const me = async (char) => { + let cch = char.anime ? char : await Char.findOne({id: client.misc.cache.chars.get(char)}); + let ani = await Ani.findOne({id: cch.anime}); + let forceAni = false; if (!ani) {forceAni = true;} + return {embed: new Discord.MessageEmbed() + .setTitle(cch.name) + .setAuthor('Character Search', message.author.avatarURL()) + .setDescription(`**Name:** ${cch.name}`) + .addField('Other', `**Anime**: ${forceAni ? cch.anime : `${ani.name} | ${ani.japname} | \`${ani.id}\``}\n\n**Gender**: ${cch.gender}\n`) + .setColor("c375f0") + .setImage(cch.thumbnail) + .setFooter('Natsuki', client.user.avatarURL()) + .setTimestamp(), id: cch.id}; + }; + + let attF = await Char.findOne({id: search.trim().toLowerCase()}); + if (attF) {return await me(attF);} + + const res = fz.go(search, Array.from(client.misc.cache.chars.keys()), {threshold: threshold, limit: 10}).sort((a,b)=>a.score-b.score).map(k => k.target); + if (res.length === 0) {return 0;} + else if (res.length > 1) { + let tp = []; + await res.forEach(ca => tp.push(me(ca))); + tp = await Promise.all(tp); + return new Pagination(message.channel, tp.map(k => k.embed), message, client, true); + } else {return await me(res[0]);} +} \ No newline at end of file