Last active
September 20, 2024 11:50
-
-
Save JPBM135/bccf14cb8123398bfddc8a2b39d55621 to your computer and use it in GitHub Desktop.
Sync folder to discord application emojis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readdirSync, readFileSync, writeFileSync } from "fs"; | |
import { setTimeout } from "timers/promises"; | |
const APPLICATION_ID = "APPLICATION_ID"; | |
const TOKEN = | |
"BOT_TOKEN"; | |
const discordApiUrl = `https://discord.com/api/v10/applications/${APPLICATION_ID}/emojis`; | |
const headers = { | |
Authorization: `Bot ${TOKEN}`, | |
}; | |
const emojis = readdirSync("./emojis"); | |
const emojiList = []; | |
const failedList = []; | |
let current = 0; | |
for (const emoji of emojis) { | |
const file = readFileSync(`./emojis/${emoji}`); | |
const name = emoji.replace(".png", "").toLowerCase(); | |
const payload = { | |
name, | |
image: ["data:", "image/png;", "base64,", file.toString("base64")].join(""), | |
}; | |
console.log(`(${current++}/${emojis.length}) Uploading emoji ${name}`); | |
const response = await fetch(discordApiUrl, { | |
method: "POST", | |
headers: { | |
...headers, | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify(payload), | |
}); | |
if (!response.ok) { | |
console.error(`[!] Failed to upload emoji ${emoji}`, await response.text()); | |
failedList.push(emoji); | |
continue; | |
} | |
const data = await response.json(); | |
emojiList.push(data); | |
console.log(`[+] Emoji ${name} uploaded`); | |
await setTimeout(300); | |
} | |
writeFileSync("./emojis.json", JSON.stringify(emojiList, null, 2)); | |
writeFileSync("./failed.json", JSON.stringify(failedList, null, 2)); | |
const TS_FILE = `// Generated with https://gist.github.com/JPBM135/bccf14cb8123398bfddc8a2b39d55621 | |
/* eslint-disable id-length */ | |
export const EMOJIS = { | |
${emojiList | |
.map( | |
(emoji) => | |
`"${emoji.name.replace("icons_", "").toUpperCase()}": { id: '${ | |
emoji.id | |
}', formatted: '<:${emoji.name}:${emoji.id}>' }` | |
) | |
.join(",\n ")} | |
} as const; | |
`; | |
writeFileSync("./emojis.ts", TS_FILE); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment