implement staticToAnimated in loop
Change-Id: I12c3c2e04f79110f5bab429cfcc3252d8af34eaf
Reviewed-on: https://git.clicks.codes/c/Coded/EmojiUploader/+/300
Reviewed-by: Samuel Shuert <coded@clicks.codes>
Tested-by: Samuel Shuert <coded@clicks.codes>
diff --git a/index.ts b/index.ts
index eda275f..d5092f5 100644
--- a/index.ts
+++ b/index.ts
@@ -1,5 +1,5 @@
import { ChannelType, Client, Collection, Guild, GuildEmoji, GuildInvitableChannelResolvable, IntentsBitField } from 'discord.js';
-import { writeFileSync, readdirSync, readFileSync } from 'fs'
+import { writeFileSync, readdirSync, readFileSync, Dirent } from 'fs'
import Canvas from 'canvas';
import GIFEncoder from 'gifencoder';
import Sharp from 'sharp';
@@ -75,20 +75,12 @@
});
});
-client.once('ready', async (client) => {
- if (!process.env.EMOJI_DIR) throw new Error("Missing Emoji Directory");
- console.log('ready')
- let emojis: Collection<string, string> = new Collection();
- const dir = readdirSync(process.env.EMOJI_DIR, {
- withFileTypes: true
- }).filter(file => file.isFile());
- console.log(dir)
- const emojiFiles = dir.filter(file => file.name.endsWith('.png'));
-
- let currentEmojiIndex = 0;
- const guilds = await propogateGuilds(client);
- console.log('Fetched Guilds');
-
+async function guildLoop(
+ guilds: Collection<string, Guild>,
+ currentEmojiIndex: number,
+ emojis: Collection<string, string>,
+ emojiFiles: Dirent[],
+ secondLoop: boolean = false): Promise<[number, Collection<string, string>]> {
for (const [_gid, guild] of guilds) {
if (guild.invites.cache.size > 0) {
console.log(`${guild.name}: ${guild.invites.cache.first()?.url}`);
@@ -110,7 +102,9 @@
while (emojiCount > 0 && !(currentEmojiIndex >= emojiFiles.length)) {
let emoji = emojiFiles[currentEmojiIndex];
- let attachment = readFileSync(`${process.env.EMOJI_DIR}/${emoji.name}`);
+ let attachment = !secondLoop ?
+ readFileSync(`${process.env.EMOJI_DIR}/${emoji.name}`) :
+ await staticToAnimatedEmoji(`${process.env.EMOJI_DIR}/${emoji.name}`);
let longName = emoji.name.split('.')[0].split('_').pop()!.replaceAll(/\-/g, "_");
let name = longName.substring(0, Math.min(longName.length, 32));
let i = 1;
@@ -128,6 +122,28 @@
console.log(`completed emojis for ${guild.name}`);
}
+ return [currentEmojiIndex, emojis]
+}
+
+client.once('ready', async (client) => {
+ if (!process.env.EMOJI_DIR) throw new Error("Missing Emoji Directory");
+ console.log('ready')
+ let emojis: Collection<string, string> = new Collection();
+ const dir = readdirSync(process.env.EMOJI_DIR, {
+ withFileTypes: true
+ }).filter(file => file.isFile());
+ console.log(dir)
+ let emojiFiles = dir.filter(file => file.name.endsWith('.png'));
+
+ let currentEmojiIndex = 0;
+ const guilds = await propogateGuilds(client);
+ console.log('Fetched Guilds');
+
+ [currentEmojiIndex, emojis] = await guildLoop(guilds, currentEmojiIndex, emojis, emojiFiles);
+
+ if (currentEmojiIndex < emojiFiles.length) [currentEmojiIndex, emojis] = await guildLoop(guilds, currentEmojiIndex, emojis, emojiFiles, true);
+
+ if (currentEmojiIndex < emojiFiles.length) console.error("Not Enough Emoji Slots");
console.log('Creating out.json');