From 48c12d99f94afb083feb8ae18f6cefcba8080221 Mon Sep 17 00:00:00 2001 From: Neko Life Date: Sun, 9 May 2021 09:12:58 +0000 Subject: [PATCH] fixed in DM avatar command --- cmds/utility/avatar.js | 132 ++++++++++++++++++++++------------------- cmds/utility/lookup.js | 2 +- resources/functions.js | 14 +++-- resources/shaChat.js | 2 +- 4 files changed, 80 insertions(+), 70 deletions(-) diff --git a/cmds/utility/avatar.js b/cmds/utility/avatar.js index 520f673..fa46b80 100644 --- a/cmds/utility/avatar.js +++ b/cmds/utility/avatar.js @@ -5,6 +5,7 @@ const { MessageEmbed } = require("discord.js"); const { ranLog, errLog, trySend, findMemberRegEx, multipleMembersFound, cleanMentionID } = require("../../resources/functions"); const { database } = require("../../database/mongo"); const { randomColors } = require("../../config.json"); +const { isArray } = require("util"); module.exports = class avatar extends commando.Command { constructor(client) { @@ -25,7 +26,6 @@ module.exports = class avatar extends commando.Command { } const footerQuote = r?.["settings"]?.defaultEmbed?.footerQuote; const withPerm = arg.trim().split(/,+/); - console.log(withPerm); const option = arg.trim().split(/(\-\-)+/); let user, avatar, member, show; let [allEmb, multipleMemMes, dupliCheck] = [[], [], []]; @@ -37,83 +37,91 @@ module.exports = class avatar extends commando.Command { if (!msg.guild || msg.guild.member(msg.author).hasPermission("MANAGE_MESSAGES")) { args = withPerm; } else { - args = withPerm[0]; - if (withPerm.length > 1) { + if (withPerm.length < 2) { + args = withPerm; + } else { + args = withPerm[0]; trySend(this.client, msg, "Manage messages permission required to show two or more avatar at once!"); } } for (const ops of option) { if (ops.toLowerCase().startsWith("show")) { const val = ops.trim().split(/ +/); - const theVal = val[1]?.trim().replace(",", ""); - if (theVal && !/\D/.test(theVal)) { - show = parseInt(val[1].trim(), 10); + const theVal = val[1]?.match(/\d*/); + if (theVal[0]) { + show = parseInt(theVal[0].trim(), 10); } } } - if (args.length !== 0) { + if (arg) { + let onceOnly = false; for(const theAvThis of args) { - const avThis = theAvThis.replace(/\-\-show *\d*/i, ""); - let uID = avThis.trim(); - uID = cleanMentionID(uID); - if (uID.length === 1) { - return trySend(this.client, msg, "One character for searching member isn't allowed <:catstareLife:794930503076675584>"); - } else { - if (uID) { - let ree = []; - async function nonDigit(client) { - const theree = await findMemberRegEx(msg, client, uID); - if (theree.length === 0) { - user = undefined; - trySend(client, msg, `Can't find user: **${avThis.trim()}**`); - } + let avThis = theAvThis.replace(/\-\-show *\d*/i, ""); + if (avThis.length === 1) { + avThis = args.replace(/\-\-show *\d*/i, ""); + onceOnly = true; + if (avThis.length === 1) { + return trySend(this.client, msg, "One character for searching member isn't allowed <:catstareLife:794930503076675584>"); + } + } + let uID = cleanMentionID(avThis.trim()); + if (uID.length > 1) { + let ree = []; + async function nonDigit(client) { + const theree = await findMemberRegEx(msg, uID); + if (theree) { for (const reeRes of theree) { ree.push(reeRes); } - } - if (/\D/.test(uID)) { - await nonDigit(this.client); } else { - if (msg.guild.member(uID)) { - ree.push(msg.guild.member(uID)); - } else { - await this.client.users.fetch(uID).then(r => ree.push(r)).catch(async e => await nonDigit(this.client)); - } - } - if (ree.length > 0) { - const duplicateRes = dupliCheck.findIndex(yes => yes === ree[0].id); - if (duplicateRes !== -1) { - allEmb[duplicateRes].setDescription(`Duplicate result for: **${avThis.trim()}**`); - user = undefined; - } else { - dupliCheck.push(ree[0].id); - user = ree[0].user ?? ree[0]; - multipleMemMes.push(multipleMembersFound(this.client, msg, ree, uID, show)); - } - } - if (user) { - avatar = user.displayAvatarURL({size:4096,dynamic:true}); - let emb = new MessageEmbed() - .setImage(avatar) - .setFooter(footerQuote ?? ""); - member = msg.guild ? msg.guild.member(user) : undefined; - if (member) { - emb.setTitle(member.displayName); - if (member.displayColor) { - emb.setColor(member.displayColor) - } - } else { - emb.setTitle(user.username); - } - if (!msg.guild) { - emb.setColor(randomColors[Math.floor(Math.random() * randomColors.length)]); - } - if (emb.color === 16777215) { - emb.setColor(16777214); - } - allEmb.push(emb); + trySend(client, msg, `Can't find user: **${avThis.trim()}**`); } } + if (/\D/.test(uID)) { + await nonDigit(this.client); + } else { + if (msg.guild?.member(uID)) { + ree.push(msg.guild.member(uID)); + } else { + await this.client.users.fetch(uID).then(r => ree.push(r)).catch(async e => await nonDigit(this.client)); + } + } + if (ree.length > 0) { + const duplicateRes = dupliCheck.findIndex(yes => yes === ree[0].id); + if (duplicateRes !== -1) { + allEmb[duplicateRes].setDescription(`Duplicate result for: **${avThis.trim()}**`); + user = undefined; + } else { + dupliCheck.push(ree[0].id); + user = ree[0].user ?? ree[0]; + multipleMemMes.push(multipleMembersFound(this.client, msg, ree, uID, show)); + } + } + if (user) { + avatar = user.displayAvatarURL({size:4096,dynamic:true}); + let emb = new MessageEmbed() + .setImage(avatar) + .setFooter(footerQuote ?? ""); + member = msg.guild ? msg.guild.member(user) : undefined; + if (member) { + emb.setTitle(member.displayName); + if (member.displayColor) { + emb.setColor(member.displayColor) + } + } else { + emb.setTitle(user.username); + } + if (!msg.guild) { + emb.setColor(randomColors[Math.floor(Math.random() * randomColors.length)]); + } + if (emb.color === 16777215) { + emb.setColor(16777214); + } + allEmb.push(emb); + } + } + if (onceOnly) { + break } } } else { diff --git a/cmds/utility/lookup.js b/cmds/utility/lookup.js index 596cf71..d253078 100644 --- a/cmds/utility/lookup.js +++ b/cmds/utility/lookup.js @@ -34,7 +34,7 @@ module.exports = class lookup extends commando.Command { fetchedMember.push(msg.guild.member(memberID)); } if (/\D/.test(memberID) || fetchedMember[0] === null) { - fetchedMember = await findMemberRegEx(msg, this.client, memberID); + fetchedMember = await findMemberRegEx(msg, memberID); } if (fetchedMember.length > 1) { memMes = multipleMembersFound(this.client, msg, fetchedMember, memberID, show, true); diff --git a/resources/functions.js b/resources/functions.js index fb9496f..6d3de89 100644 --- a/resources/functions.js +++ b/resources/functions.js @@ -210,16 +210,18 @@ function multipleMembersFound(client, msg, arr, key, max = 5, withID) { * @param {String} name * @returns {Promise} Member object found */ -async function findMemberRegEx(msg, client, name) { +async function findMemberRegEx(msg, name) { let found = []; const re = new RegExp(name, "i"); - const list = msg.guild.members.cache.map(g => g); - for(const mem of list) { - if (re.test(mem.displayName) || re.test((await client.users.fetch(mem.id)).tag)) { - found.push(mem); + const list = msg.guild?.members.cache.array(); + if (list) { + for(const mem of list) { + if (re.test(mem.displayName) || re.test(mem.user.tag)) { + found.push(mem); + } } + return found; } - return found; } /** diff --git a/resources/shaChat.js b/resources/shaChat.js index d213e3f..f758997 100644 --- a/resources/shaChat.js +++ b/resources/shaChat.js @@ -45,7 +45,6 @@ async function shaChat(client, index, question) { await page.waitForSelector("input[id=\"question\"]"); await page.type("input[id=\"question\"]", query); await page.keyboard.press("Enter"); - await page.waitForSelector(`#answer > div:nth-child(${index})`, {timeout:5000}).catch(() => {}); return fetchAnswer(page, index); } catch (error) { throw error; @@ -60,6 +59,7 @@ async function shaChat(client, index, question) { */ async function fetchAnswer(page, index) { try { + await page.waitForSelector(`#answer > div:nth-child(${index})`, {timeout:5000}).catch(() => {}); const result = await page.evaluate((index) => { const res = document.querySelector(`#answer > div:nth-child(${index})`).childNodes[4].textContent; return res;