add ratelimit logging, fix issue where duplicate names would overwrite
previous emojis

Change-Id: I55054fed2b2c883c7078541714509aa49419072a
Reviewed-on: https://git.clicks.codes/c/coded/EmojiUploader/+/288
Tested-by: Samuel Shuert <coded@clicks.codes>
Reviewed-by: Samuel Shuert <coded@clicks.codes>
diff --git a/.gitignore b/.gitignore
index 92bf048..3cecfb4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,7 +2,7 @@
 
 .direnv
 out.json
-
+etc
 
 # Logs
 
diff --git a/index.ts b/index.ts
index 09fbb19..85ef717 100644
--- a/index.ts
+++ b/index.ts
@@ -1,8 +1,7 @@
-import { ChannelType, Client, Collection, Guild, GuildChannel, GuildEmoji, GuildInvitableChannelResolvable, IntentsBitField } from 'discord.js';
+import { ChannelType, Client, Collection, Guild, GuildEmoji, GuildInvitableChannelResolvable, IntentsBitField } from 'discord.js';
 import { writeFileSync, readdirSync, readFileSync } from 'fs'
 
 
-
 async function uploadEmoji(guild: Guild, toUpload: {name: string, attachment: Buffer}): Promise<GuildEmoji> {
     const emoji = await guild.emojis.create(toUpload)
 
@@ -36,6 +35,13 @@
 
 client.on('warn', (m) => console.warn(m));
 client.on('error', (m) => console.error(m.message));
+client.rest.on("rateLimited", rateLimitData => {
+    console.table({
+        Global: rateLimitData.global,
+        Limit: rateLimitData.limit,
+        TimeToRetry: `${Math.floor((rateLimitData.retryAfter / 1000) / 60)}M ${Math.floor((rateLimitData.retryAfter / 1000) % 60)}S`
+    });
+});
 
 client.once('ready', async (client) => {
     if (!process.env.EMOJI_DIR) throw new Error("Missing Emoji Directory");
@@ -51,7 +57,7 @@
     const guilds = await propogateGuilds(client);
     console.log('Fetched Guilds');
 
-    for (const [_id, guild] of guilds) {
+    for (const [_gid, guild] of guilds) {
         if (guild.invites.cache.size > 0) {
             console.log(`${guild.name}: ${guild.invites.cache.first()?.url}`);
         } else {
@@ -63,7 +69,7 @@
             console.log(`${guild.name}: ${invite.url}`);
         }
 
-        for (const [_, emoji] of guild.emojis.cache) {
+        for (const [_eid, emoji] of guild.emojis.cache) {
             console.log(`Deleting ${emoji.name}`)
             await emoji.delete();
         }
@@ -75,9 +81,15 @@
             let attachment = readFileSync(`${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));
-            const outEmoji = await uploadEmoji(guild, {name, attachment});
-            console.log(`${name}: ${outEmoji.id}`)
-            emojis.set(name, outEmoji.id);
+            let i = 1;
+            while (emojis.get(name)) {
+                name = name.substring(0,-(i.toString().length)) + i
+                i++
+            }
+            console.log(name)
+            // const outEmoji = await uploadEmoji(guild, {name, attachment});
+            // console.log(`${name}: ${outEmoji.id}`)
+            // emojis.set(name, outEmoji.id);
 
             emojiCount--;
             currentEmojiIndex++;