add direnv support, fix uploading emojis, add logs

Change-Id: I15e22289f0ced8cd4c72ba9043b06373787cd331
Reviewed-on: https://git.clicks.codes/c/coded/EmojiUploader/+/285
Reviewed-by: Samuel Shuert <coded@clicks.codes>
Tested-by: Samuel Shuert <coded@clicks.codes>
diff --git a/.envrc b/.envrc
new file mode 100644
index 0000000..f558b66
--- /dev/null
+++ b/.envrc
@@ -0,0 +1,2 @@
+use_flake .
+dotenv
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
index 468f82a..e1d81c3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,7 @@
 # Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
 
+.direnv
+
 # Logs
 
 logs
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..aca3fbd
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,61 @@
+{
+  "nodes": {
+    "flake-utils": {
+      "inputs": {
+        "systems": "systems"
+      },
+      "locked": {
+        "lastModified": 1705309234,
+        "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
+        "type": "github"
+      },
+      "original": {
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "type": "github"
+      }
+    },
+    "nixpkgs": {
+      "locked": {
+        "lastModified": 1706006310,
+        "narHash": "sha256-nDPz0fj0IFcDhSTlXBU2aixcnGs2Jm4Zcuoj0QtmiXQ=",
+        "owner": "NixOS",
+        "repo": "nixpkgs",
+        "rev": "b43bb235efeab5324c5e486882ef46749188eee2",
+        "type": "github"
+      },
+      "original": {
+        "owner": "NixOS",
+        "ref": "nixpkgs-unstable",
+        "repo": "nixpkgs",
+        "type": "github"
+      }
+    },
+    "root": {
+      "inputs": {
+        "flake-utils": "flake-utils",
+        "nixpkgs": "nixpkgs"
+      }
+    },
+    "systems": {
+      "locked": {
+        "lastModified": 1681028828,
+        "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
+        "owner": "nix-systems",
+        "repo": "default",
+        "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
+        "type": "github"
+      },
+      "original": {
+        "owner": "nix-systems",
+        "repo": "default",
+        "type": "github"
+      }
+    }
+  },
+  "root": "root",
+  "version": 7
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..bc87703
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,22 @@
+{
+  description = "A basic flake with a shell";
+  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
+  inputs.flake-utils.url = "github:numtide/flake-utils";
+
+
+  outputs = { nixpkgs, flake-utils, ... }:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs {
+          inherit system;
+        };
+      in
+      {
+        devShells.default = pkgs.mkShell {
+          packages = [
+            pkgs.bun
+            pkgs.nodejs_21
+          ];
+        };
+      });
+}
diff --git a/index.ts b/index.ts
index 6c5e4f3..ce0000d 100644
--- a/index.ts
+++ b/index.ts
@@ -10,6 +10,7 @@
 }
 
 async function propogateGuilds(client: Client<true>): Promise<Collection<string, Guild>> {
+    await client.guilds.fetch();
     let guilds = client.guilds.cache.filter((guild) => guild.ownerId == client.user.id);
 
     if (guilds.size < 10) {
@@ -30,18 +31,22 @@
     ]
 });
 
-client.once('ready', async (client) => {
+client.on('warn', (m) => console.warn(m));
+client.on('error', (m) => console.error(m.message));
 
+client.once('ready', async (client) => {
+    console.log('ready')
     let emojis: Collection<string, string> = new Collection();
 
     const dir = readdirSync("./deduplicated", {
         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');
 
     for (const [_id, guild] of guilds) {
         if (guild.invites.cache.size > 0) {
@@ -56,6 +61,7 @@
         }
 
         for (const [_, emoji] of guild.emojis.cache) {
+            console.log(`Deleting ${emoji.name}`)
             await emoji.delete();
         }
 
@@ -63,10 +69,11 @@
 
         while (emojiCount > 0) {
             let emoji = emojiFiles[currentEmojiIndex];
-            let attachment = readFileSync(emoji.path);
-            let name = emoji.name.split('.')[0]
+            let attachment = readFileSync(`./deduplicated/${emoji.name}`);
+            let longName = emoji.name.split('.')[0].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);
 
             emojiCount--;