feature: bulk emote dl

This commit is contained in:
skkeye
2023-11-05 20:28:44 -05:00
parent ecb0d624ab
commit e2f587994c
5 changed files with 79 additions and 3 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
config.json
node_modules/
downloads/

35
commands/emotes/dl.js Normal file
View File

@@ -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 <a:n>
let emoteIdRegex = /<(?<animated>a?):(?<name>\w{0,22})\w*:(?<id>\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!");
},
};

View File

@@ -12,7 +12,7 @@ module.exports = {
async execute(interaction) {
await interaction.deferReply();
// this regex matches emotes in the format <:name:id> or <a:name:id>
// this regex matches emotes in the format <:name:id> or <a:n>
let emoteIdRegex = /<(?<animated>a?):(?<name>\w{0,22})\w*:(?<id>\d+)>/gm;
let replyMessage = "";
const input = interaction.options.getString("emote");

View File

@@ -22,7 +22,6 @@ module.exports = {
interaction.followUp(emoteList);
}
interaction.followUp("Total emotes: " + emojis.size + "\n");
})
.catch(console.error);
}).catch(console.error);
},
};

41
dev/clonesticker.js Normal file
View File

@@ -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 <a:n>
let emoteIdRegex = /<(?<animated>a?):(?<name>\w{0,22})\w*:(?<id>\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);
},
};