From e2f587994cdde699c9279e0e3317bc0156aa08b7 Mon Sep 17 00:00:00 2001 From: skkeye <4247532@gmail.com> Date: Sun, 5 Nov 2023 20:28:44 -0500 Subject: [PATCH] feature: bulk emote dl --- .gitignore | 1 + commands/emotes/dl.js | 35 +++++++++++++++++++++++++++++++ commands/emotes/emotelog.js | 2 +- commands/emotes/list.js | 3 +-- dev/clonesticker.js | 41 +++++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 commands/emotes/dl.js create mode 100644 dev/clonesticker.js diff --git a/.gitignore b/.gitignore index cb75afc..dc6e3c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ config.json node_modules/ +downloads/ diff --git a/commands/emotes/dl.js b/commands/emotes/dl.js new file mode 100644 index 0000000..12e461b --- /dev/null +++ b/commands/emotes/dl.js @@ -0,0 +1,35 @@ +const { SlashCommandBuilder } = require("discord.js"); +const fs = require('fs/promises'); + +module.exports = { + data: new SlashCommandBuilder() + .setName("dl") + .setDescription("Downloads the emote input (local).") + .addStringOption(option => + option.setName("emotes") + .setDescription("The emotes to download.") + .setRequired(true) + ), + async execute(interaction) { + await interaction.deferReply(); + + // this regex matches emotes in the format <:name:id> or + let emoteIdRegex = /<(?a?):(?\w{0,22})\w*:(?\d+)>/gm; + let replyMessage = ""; + const input = interaction.options.getString("emotes"); + // remove duplicates from the emote list + let emoteList = input.matchAll(emoteIdRegex); + emoteList = [...new Set(emoteList)].join(" "); + for (const match of emoteList.matchAll(emoteIdRegex)) { + const link = "https://cdn.discordapp.com/emojis/" + match.groups.id + ((match.groups.animated == 'a') ? ".gif": ".webp") + "?quality=lossless"; + const copyname = match.groups.name + "_FE"; + const response = await fetch(link); + // save the file + await fs.writeFile(`./downloads/${copyname}${(match.groups.animated == 'a') ? ".gif": ".webp"}`, response.body); + await interaction.editReply(`Dowloaded ${match[0]}!\n`); + replyMessage += `Logged ${match[0]}!\n`; + } + + await interaction.editReply("Done!"); + }, +}; \ No newline at end of file diff --git a/commands/emotes/emotelog.js b/commands/emotes/emotelog.js index 184eaf1..3af0678 100644 --- a/commands/emotes/emotelog.js +++ b/commands/emotes/emotelog.js @@ -12,7 +12,7 @@ module.exports = { async execute(interaction) { await interaction.deferReply(); - // this regex matches emotes in the format <:name:id> or + // this regex matches emotes in the format <:name:id> or let emoteIdRegex = /<(?a?):(?\w{0,22})\w*:(?\d+)>/gm; let replyMessage = ""; const input = interaction.options.getString("emote"); diff --git a/commands/emotes/list.js b/commands/emotes/list.js index cd5fb0d..5c6a315 100644 --- a/commands/emotes/list.js +++ b/commands/emotes/list.js @@ -22,7 +22,6 @@ module.exports = { interaction.followUp(emoteList); } interaction.followUp("Total emotes: " + emojis.size + "\n"); - }) - .catch(console.error); + }).catch(console.error); }, }; diff --git a/dev/clonesticker.js b/dev/clonesticker.js new file mode 100644 index 0000000..6a67386 --- /dev/null +++ b/dev/clonesticker.js @@ -0,0 +1,41 @@ +const { SlashCommandBuilder } = require("discord.js"); + +module.exports = { + data: new SlashCommandBuilder() + .setName("emotelog") + .setDescription("Logs the emote inputted.") + .addStringOption(option => + option.setName("emote") + .setDescription("The emote to log.") + .setRequired(true) + ), + async execute(interaction) { + await interaction.deferReply(); + + // this regex matches emotes in the format <:name:id> or + let emoteIdRegex = /<(?a?):(?\w{0,22})\w*:(?\d+)>/gm; + let replyMessage = ""; + const input = interaction.options.getString("emote"); + // remove duplicates from the emote list + let emoteList = input.matchAll(emoteIdRegex); + emoteList = [...new Set(emoteList)].join(" "); + for (const match of emoteList.matchAll(emoteIdRegex)) { + const link = "https://cdn.discordapp.com/emojis/" + match.groups.id + ((match.groups.animated == 'a') ? ".gif": ".webp") + "?quality=lossless"; + const copyname = match.groups.name + "_FE"; + await interaction.guild.emojis.create({attachment: link, name: copyname}); + await interaction.editReply(`Logged ${match[0]}!\n`); + replyMessage += `Logged ${match[0]}!\n`; + } + + // add a message to say how many emote spots are left + interaction.guild.emojis.fetch() + .then(emojis => { + let animatedEmojis = emojis.filter(emoji => emoji.animated); + replyMessage += `\nAnimated emotes: ${animatedEmojis.size}/50\n` + let staticEmojis = emojis.filter(emoji => !emoji.animated); + replyMessage += `Static emotes: ${staticEmojis.size}/50\n` + interaction.editReply(replyMessage); + }) + .catch(console.error); + }, +}; \ No newline at end of file