Skip to content

Commit

Permalink
Add message app command to grab custom emojis (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
True-Aki authored Mar 19, 2024
1 parent f7c809a commit e375adf
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/commands/message/General/grabemojis
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js");

module.exports = {
name: "Grab Emojis",
type: 3,
run: async (client, interaction, config, db) => {
const messageContent = interaction.options.getMessage('message').content;
const emojiRegex = /<(a?):(\w+):(\d+)>/g;
let match;
const emojis = [];
const uniqueId = Date.now();

while ((match = emojiRegex.exec(messageContent)) !== null) {
emojis.push({
animated: match[1] === 'a',
name: match[2],
id: match[3]
});
}

if (emojis.length === 0) {
return interaction.reply({
content: "🚫 No custom emojis found in the message.",
ephemeral: true,
});
}

const createEmbedWithEmoji = (emoji, index, total) => {
const emojiUrl = `https://cdn.discordapp.com/emojis/${emoji.id}.${emoji.animated ? 'gif' : 'png'}`;
return new EmbedBuilder()
.setTitle(`Emoji ${index + 1} of ${total}`)
.addFields([
{ name: "Name", value: `\`${emoji.name}\``, inline: true },
{ name: "ID", value: `\`${emoji.id}\``, inline: true }
])
.setColor("Purple")
.setImage(emojiUrl)
.setFooter({ text: `Requested by ${interaction.user.username}`, iconURL: interaction.user.displayAvatarURL({ dynamic: true }) });
};

let currentEmojiIndex = 0;
let embeds = [createEmbedWithEmoji(emojis[currentEmojiIndex], currentEmojiIndex, emojis.length)];

const components = [];
if (emojis.length > 1) {
components.push(new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId(`previous_emoji_${uniqueId}`)
.setLabel("<")
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId(`next_emoji_${uniqueId}`)
.setLabel(">")
.setStyle(ButtonStyle.Primary),
));
}

await interaction.reply({
embeds: embeds,
components: components,
ephemeral: true,
});

const filter = (i) => i.user.id === interaction.user.id && i.customId.includes(uniqueId.toString());
const collector = interaction.channel.createMessageComponentCollector({ filter, time: 60000 });

collector.on('collect', async (i) => {
if (i.customId.includes("previous_emoji")) {
currentEmojiIndex = (currentEmojiIndex - 1 + emojis.length) % emojis.length;
} else if (i.customId.includes("next_emoji")) {
currentEmojiIndex = (currentEmojiIndex + 1) % emojis.length;
}

embeds = [createEmbedWithEmoji(emojis[currentEmojiIndex], currentEmojiIndex, emojis.length)];

try {
await i.update({ embeds: embeds, components: components });
} catch (error) {
const errorEmbed = new EmbedBuilder()
.setTitle("Error")
.setDescription("An error occurred while updating the emoji.")
.setColor("Red")
.setFooter({ text: `Requested by ${interaction.user.username}`, iconURL: interaction.user.displayAvatarURL({ dynamic: true }) });

const disabledComponents = components.map(component =>
new ActionRowBuilder()
.addComponents(component.components.map(btn =>
ButtonBuilder.from(btn).setDisabled(true)
))
);

await i.update({ embeds: [errorEmbed], components: disabledComponents }).catch(console.error);
}
});
},
};

0 comments on commit e375adf

Please sign in to comment.