Samuel Shuert | 2d7c733 | 2024-01-25 14:46:03 -0500 | [diff] [blame^] | 1 | import { ChannelType, Client, Collection, Guild, GuildChannel, GuildEmoji, GuildInvitableChannelResolvable, IntentsBitField, TextChannel } from 'discord.js'; |
| 2 | import { writeFileSync, readdirSync, readFileSync } from 'fs' |
| 3 | |
| 4 | |
| 5 | |
| 6 | async function uploadEmoji(guild: Guild, toUpload: {name: string, attachment: Buffer}): Promise<GuildEmoji> { |
| 7 | const emoji = await guild.emojis.create(toUpload) |
| 8 | |
| 9 | return emoji; |
| 10 | } |
| 11 | |
| 12 | async function propogateGuilds(client: Client<true>): Promise<Collection<string, Guild>> { |
| 13 | let guilds = client.guilds.cache.filter((guild) => guild.ownerId == client.user.id); |
| 14 | |
| 15 | if (guilds.size < 10) { |
| 16 | await client.guilds.create({ |
| 17 | name: `BlueprintRadar Emoji ${guilds.size + 1}` |
| 18 | }); |
| 19 | propogateGuilds(client); |
| 20 | } |
| 21 | |
| 22 | return guilds; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | const client = new Client({ |
| 27 | intents: [ |
| 28 | IntentsBitField.Flags.GuildEmojisAndStickers, |
| 29 | IntentsBitField.Flags.Guilds |
| 30 | ] |
| 31 | }); |
| 32 | |
| 33 | client.once('ready', async (client) => { |
| 34 | |
| 35 | let emojis: Collection<string, string> = new Collection(); |
| 36 | |
| 37 | const dir = readdirSync("./deduplicated", { |
| 38 | withFileTypes: true |
| 39 | }).filter(file => file.isFile()); |
| 40 | const emojiFiles = dir.filter(file => file.name.endsWith('.png')); |
| 41 | |
| 42 | let currentEmojiIndex = 0; |
| 43 | |
| 44 | const guilds = await propogateGuilds(client); |
| 45 | |
| 46 | for (const [_id, guild] of guilds) { |
| 47 | if (guild.invites.cache.size > 0) { |
| 48 | console.log(`${guild.name}: ${guild.invites.cache.first()?.url}`); |
| 49 | } else { |
| 50 | if (guild.channels.cache.size === 0) await guild.channels.create({name: 'invite-channel', type: ChannelType.GuildText }); |
| 51 | const channels = guild.channels.cache.filter(channel => !(channel.type == ChannelType.GuildCategory)) as Collection<string, GuildInvitableChannelResolvable>; |
| 52 | const invite = await guild.invites.create(channels.first()!, { |
| 53 | temporary: false |
| 54 | }); |
| 55 | console.log(`${guild.name}: ${invite.url}`); |
| 56 | } |
| 57 | |
| 58 | for (const [_, emoji] of guild.emojis.cache) { |
| 59 | await emoji.delete(); |
| 60 | } |
| 61 | |
| 62 | let emojiCount = 50; |
| 63 | |
| 64 | while (emojiCount > 0) { |
| 65 | let emoji = emojiFiles[currentEmojiIndex]; |
| 66 | let attachment = readFileSync(emoji.path); |
| 67 | let name = emoji.name.split('.')[0] |
| 68 | const outEmoji = await uploadEmoji(guild, {name, attachment}); |
| 69 | |
| 70 | emojis.set(name, outEmoji.id); |
| 71 | |
| 72 | emojiCount--; |
| 73 | currentEmojiIndex++; |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |
| 78 | let json: Record<string, string> = {}; |
| 79 | for (const [k,v] of emojis) { |
| 80 | json[k] = v; |
| 81 | } |
| 82 | |
| 83 | writeFileSync('./out.json', JSON.stringify(json)); |
| 84 | }) |
| 85 | |
| 86 | client.login(process.env.TOKEN) |