From 90ad9ed8015bf9190f39363ef710f43a3b85ce99 Mon Sep 17 00:00:00 2001 From: Faf4a <87046111+Faf4a@users.noreply.github.com> Date: Fri, 8 Dec 2023 11:04:00 +0100 Subject: [PATCH] fix: emoji handling (getEmoji) --- src/functions/info/reactionCount.js | 24 +++---- src/functions/interaction/addButton.js | 70 +++++++++++-------- .../interaction/addClientReactions.js | 28 +++++--- src/functions/interaction/addCmdReactions.js | 33 +++++---- .../interaction/addMessageReactions.js | 41 ++++++----- src/functions/interaction/addSelectMenu.js | 50 +++++-------- 6 files changed, 136 insertions(+), 110 deletions(-) diff --git a/src/functions/info/reactionCount.js b/src/functions/info/reactionCount.js index aef3a3a62..2bd7ca699 100644 --- a/src/functions/info/reactionCount.js +++ b/src/functions/info/reactionCount.js @@ -5,22 +5,22 @@ module.exports = async (d) => { const [channelID, messageID, emojiResolver] = data.inside.splits; const channel = await d.util.getChannel(d, channelID); - if (!channel) - return d.aoiError.fnError(d, "channel", {inside: data.inside}); + if (!channel) return d.aoiError.fnError(d, "channel", { inside: data.inside }); const message = await d.util.getMessage(channel, messageID); - if (!message) - return d.aoiError.fnError(d, "message", {inside: data.inside}); + if (!message) return d.aoiError.fnError(d, "message", { inside: data.inside }); - const emoji = message.reactions.cache.find( - (x) => - x.emoji.id === emojiResolver || - x.emoji.toString().toLowerCase() === - emojiResolver.addBrackets().toLowerCase(), - ); - if (!emoji) return d.aoiError.fnError(d, "emoji", {inside: data.inside}); + let emoji; - data.result = emoji.count; + try { + emoji = message.reactions.cache.find((x) => x.emoji.id === d.util.getEmoji(d, emojiResolver).id)?.count; + } catch { + emoji = message.reactions.cache.find((x) => x.emoji.toString().toLowerCase() === emojiResolver.toLowerCase())?.count; + } finally { + emoji = emoji ?? 0; + } + + data.result = emoji; return { code: d.util.setCode(data), diff --git a/src/functions/interaction/addButton.js b/src/functions/interaction/addButton.js index 822de77f8..66d09eacd 100644 --- a/src/functions/interaction/addButton.js +++ b/src/functions/interaction/addButton.js @@ -1,31 +1,43 @@ module.exports = async (d) => { - const {code} = d.command; - const inside = d.unpack(); - const err = d.inside(inside); - if (err) return d.error(err); - let [index, label, style, custom, disabled = "false", emoji] = inside.splits; - if (isNaN(index) || Number(index) < 1) - d.aoiError.fnError(d, "custom", {inside}, "Invalid Index Provided In"); - index = Number(index) - 1; - style = isNaN(style) - ? d.util.constants.ButtonStyleOptions[style] - : Number(style); - disabled = disabled === "true"; - if (!style || style > 5 || style < 1) - d.aoiError.fnError(d, "custom", {inside}, "Invalid Style Provided In"); - if (!d.components[index]) d.components[index] = {type: 1, components: []}; - const button = { - label, - type: 2, - style, - disabled, - emoji, - }; - button[style === 5 ? "url" : "customId"] = custom; - d.components[index].components.push(button); - return { - code: d.util.setCode({function: d.func, inside, code, result: ""}), - data: { ...d.data, components: Object.assign({}, d.data.components, d.components) }, - components: d.components, - }; + const data = d.util.aoiFunc(d); + const { code } = d.command; + if (data.err) return d.error(data.err); + + let [index, label, style, custom, disabled = "false", emoji] = data.inside.splits; + + if (isNaN(index) || Number(index) < 1) return d.aoiError.fnError(d, "custom", { inside: data.inside }, "Index"); + + index = Number(index) - 1; + style = isNaN(style) ? d.util.constants.ButtonStyleOptions[style] : Number(style); + disabled = disabled === "true"; + + if (!style || style > 5 || style < 1) return d.aoiError.fnError(d, "custom", { inside: data.inside }, "Style"); + + try { + emoji = d.util.getEmoji(d, emoji.addBrackets()).id; + } catch { + emoji = emoji?.addBrackets() ?? undefined; + } + + const button = { + label, + type: 2, + style, + disabled, + emoji, + }; + + button[style === 5 ? "url" : "customId"] = custom; + + if (!d.components[index]) d.components[index] = { type: 1, components: [] }; + d.components[index].components.push(button); + + return { + code: d.util.setCode({ function: d.func, inside: data.inside, code }), + data: { + ...d.data, + components: Object.assign({}, d.data.components, d.components), + }, + components: d.components, + }; }; diff --git a/src/functions/interaction/addClientReactions.js b/src/functions/interaction/addClientReactions.js index 01db768c6..e49f5b248 100644 --- a/src/functions/interaction/addClientReactions.js +++ b/src/functions/interaction/addClientReactions.js @@ -1,15 +1,25 @@ module.exports = async (d) => { - const data = d.util.aoiFunc(d); - if (data.err) return d.error(data.err); + const data = d.util.aoiFunc(d); + if (data.err) return d.error(data.err); - let [...reactions] = data.inside.splits; + let [...reactions] = data.inside.splits; - reactions = reactions.map(x => x.addBrackets()).reverse(); + reactions = reactions.map((x) => { + try { + let emoji = d.util.getEmoji(d, x.addBrackets()); + x = emoji.id ? `:${emoji.name}:${emoji.id}` : emoji.name; + } catch { + x = x?.addBrackets() ?? undefined; + } finally { + if (x === undefined) return d.util.aoiError.fnError(d, "custom", { inside: data.inside }, "Emoji"); + } + return x; + }); - data.result = "" + data.result = ""; - return { - code: d.util.setCode(data), - reactions, - }; + return { + code: d.util.setCode(data), + reactions, + }; }; diff --git a/src/functions/interaction/addCmdReactions.js b/src/functions/interaction/addCmdReactions.js index 48c0fa07c..3238c2677 100644 --- a/src/functions/interaction/addCmdReactions.js +++ b/src/functions/interaction/addCmdReactions.js @@ -1,16 +1,25 @@ -module.exports = async d => { - const data = d.util.aoiFunc(d); - if (data.err) return d.error(data.err); +module.exports = async (d) => { + const data = d.util.aoiFunc(d); + if (data.err) return d.error(data.err); - let [...reactions] = data.inside.splits; - reactions = reactions.reverse() - for (let i = reactions.length - 1; i >= 0; i--) { - await d.message.react(reactions[i]).catch(err => d.aoiError.fnError(d, "custom", {}, err.message)) + let [...reactions] = data.inside.splits; + reactions = reactions.reverse(); + for (let i = reactions.length - 1; i >= 0; i--) { + let reaction; + try { + reaction = d.util.getEmoji(d, reactions[i].addBrackets()).id; + } catch { + reaction = reactions[i]?.addBrackets() ?? undefined; + } finally { + if (reaction === undefined) return d.util.aoiError.fnError(d, "custom", { inside: data.inside}, "Emoji"); } - data.result = ""; + await d.message.react(reaction).catch((err) => d.aoiError.fnError(d, "custom", {}, err.message)); + } - return { - code: d.util.setCode(data) - } -} \ No newline at end of file + data.result = ""; + + return { + code: d.util.setCode(data), + }; +}; diff --git a/src/functions/interaction/addMessageReactions.js b/src/functions/interaction/addMessageReactions.js index a4ab22b7e..22fa0f81a 100644 --- a/src/functions/interaction/addMessageReactions.js +++ b/src/functions/interaction/addMessageReactions.js @@ -1,23 +1,32 @@ module.exports = async (d) => { - const data = d.util.aoiFunc(d); - if (data.err) return d.error(data.err); + const data = d.util.aoiFunc(d); + if (data.err) return d.error(data.err); - let [channelID, messageID, ...reactions] = data.inside.splits; - const channel = await d.util.getChannel(d, channelID); + let [channelID, messageID, ...reactions] = data.inside.splits; + const channel = await d.util.getChannel(d, channelID); - if (!channel) return d.aoiError.fnError(d, "channel", {inside: data.inside}); - const message = await d.util.getMessage(channel, messageID); + if (!channel) + return d.aoiError.fnError(d, "channel", { inside: data.inside }); + const message = await d.util.getMessage(channel, messageID); - if (!message) return d.aoiError.fnError(d, "message", {inside: data.inside}); - reactions = reactions.reverse(); - for (let i = reactions.length - 1; i >= 0; i--) { - message - .react(reactions[i]) - .catch((err) => d.aoiError.fnError(d, "custom", {}, err.message)); + if (!message) + return d.aoiError.fnError(d, "message", { inside: data.inside }); + reactions = reactions.reverse(); + + for (let i = reactions.length - 1; i >= 0; i--) { + let reaction; + try { + reaction = d.util.getEmoji(d, reactions[i].addBrackets()).id; + } catch { + reaction = reactions[i]?.addBrackets() ?? undefined; + } finally { + if (reaction === undefined) return d.util.aoiError.fnError(d, "custom", { inside: data.inside }, "Emoji" ); } + await message.react(reaction).catch((err) => d.aoiError.fnError(d, "custom", {}, err.message)); + } - data.result = ""; - return { - code: d.util.setCode(data) - }; + data.result = ""; + return { + code: d.util.setCode(data), + }; }; diff --git a/src/functions/interaction/addSelectMenu.js b/src/functions/interaction/addSelectMenu.js index 0abeb20d9..2a9c51926 100644 --- a/src/functions/interaction/addSelectMenu.js +++ b/src/functions/interaction/addSelectMenu.js @@ -5,44 +5,25 @@ module.exports = async (d) => { const { code } = d.command; if (data.err) return d.error(data.err); - let [ index = 1, type, customId, placeHolder, minValues = 1, maxValues = 1, disabled = "false", ...options] = data.inside.splits; + let [ index = 1, type, customId, placeholder, minValues = 1, maxValues = 1, disabled = "false", ...options ] = data.inside.splits; index = Number(index) - 1; - if (isNaN(index) || index < 0) - d.aoiError.fnError(d, "custom", { inside: data.inside }, "Invalid Index Provided In"); + if (isNaN(index) || index < 0) return d.aoiError.fnError(d, "custom", { inside: data.inside }, "Index"); disabled = disabled === "true"; - placeHolder = placeHolder?.addBrackets(); + placeholder = placeholder?.addBrackets(); customId = customId?.addBrackets(); minValues = Number(minValues); maxValues = Number(maxValues); if (!options.length && type === "string") return d.aoiError.fnError(d, "custom", { inside: data.inside }, "Options Are Not Provided In"); - if (minValues > 25 || minValues < 0) - return d.aoiError.fnError( - d, - "custom", - { inside: data.inside }, - "minValues must be between 0 and 25 (both inclusive). Provided Invalid In" - ); - - if (maxValues > 25 || maxValues < 1) - return d.aoiError.fnError( - d, - "custom", - { inside: data.inside }, - "maxValues must be between 1 and 25 (both Inclusive). Provided Invalid In" - ); - - if (placeHolder.length > 100) - return d.aoiError.fnError( - d, - "custom", - {}, - "Placeholder should be at most 100 Characters long" - ); + if (minValues > 25 || minValues < 0) return d.aoiError.fnError(d, "custom", { inside: data.inside }, "minValues must be between 0 and 25 (both inclusive)."); + + if (maxValues > 25 || maxValues < 1) return d.aoiError.fnError(d, "custom", { inside: data.inside }, "maxValues must be between 1 and 25 (both Inclusive)."); + + if (placeholder.length > 100) return d.aoiError.fnError(d, "custom", {}, "Placeholder should be at most 100 Characters long"); let selectBuilder; @@ -63,12 +44,12 @@ module.exports = async (d) => { selectBuilder = new ChannelSelectMenuBuilder(); break; default: - return d.aoiError.fnError(d, "custom", { inside: data.inside }, "Invalid Select Menu Type"); + return d.aoiError.fnError(d, "custom", { inside: data.inside }, "Invalid Select Menu Type"); } selectBuilder .setCustomId(customId) - .setPlaceholder(placeHolder) + .setPlaceholder(placeholder) .setMaxValues(maxValues) .setMinValues(minValues) .setDisabled(disabled); @@ -79,8 +60,13 @@ module.exports = async (d) => { const description = option[1].addBrackets(); const value = option[2].addBrackets(); const def = option[3]?.addBrackets() === "true"; - let emoji = option[4]?.addBrackets(); - emoji = emoji === "" ? undefined : emoji; + let emoji; + + try { + emoji = d.util.getEmoji(d, option[4]?.addBrackets()).id; + } catch { + emoji = option[4]?.addBrackets() ?? undefined; + } switch (type.toLowerCase()) { case "string": @@ -103,7 +89,7 @@ module.exports = async (d) => { }); break; default: - d.aoiError.fnError(d, "custom", { inside: data.inside }, "Invalid Select Menu Type"); + d.aoiError.fnError(d, "custom", { inside: data.inside }, "Select Menu Type"); } }