Skip to content

Commit

Permalink
fix: emoji handling (getEmoji)
Browse files Browse the repository at this point in the history
  • Loading branch information
Faf4a committed Dec 8, 2023
1 parent bd63bb0 commit 90ad9ed
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 110 deletions.
24 changes: 12 additions & 12 deletions src/functions/info/reactionCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
70 changes: 41 additions & 29 deletions src/functions/interaction/addButton.js
Original file line number Diff line number Diff line change
@@ -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,
};
};
28 changes: 19 additions & 9 deletions src/functions/interaction/addClientReactions.js
Original file line number Diff line number Diff line change
@@ -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,
};
};
33 changes: 21 additions & 12 deletions src/functions/interaction/addCmdReactions.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
data.result = "";

return {
code: d.util.setCode(data),
};
};
41 changes: 25 additions & 16 deletions src/functions/interaction/addMessageReactions.js
Original file line number Diff line number Diff line change
@@ -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),
};
};
50 changes: 18 additions & 32 deletions src/functions/interaction/addSelectMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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":
Expand All @@ -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");
}
}

Expand Down

0 comments on commit 90ad9ed

Please sign in to comment.