From e70cd4ba2f929234e7dfb3d2699f3d14f100ed1c Mon Sep 17 00:00:00 2001 From: Jacob Date: Tue, 24 Sep 2024 09:44:14 +0800 Subject: [PATCH] Config override check --- Setup.ts | 53 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/Setup.ts b/Setup.ts index 1eb8b19..3015a65 100644 --- a/Setup.ts +++ b/Setup.ts @@ -1,26 +1,33 @@ +/* eslint-disable require-await */ /* eslint-disable no-console */ +import chalk from 'chalk'; import { Client, GatewayIntentBits, OAuth2Scopes, PermissionsBitField } from 'discord.js'; -import { confirm, number, password } from '@inquirer/prompts'; -import { readdirSync, writeFileSync } from 'fs'; +import { confirm, input, number, password } from '@inquirer/prompts'; +import { existsSync, readdirSync, unlinkSync, writeFileSync } from 'fs'; -function uploadEmojisToBot(token: string) { +async function uploadEmojisToBot(token: string) { const client = new Client({ intents: [GatewayIntentBits.Guilds] }); client.login(token); client.on('ready', async () => { const application = await client.application?.fetch(); if (!application) return; - const emojiFiles = readdirSync('./emojis').filter((file) => file.endsWith('.png')); - for (const emoji of emojiFiles) { - application.emojis - .create({ attachment: `./emojis/${emoji}`, name: emoji.split('.')[0] }) - .then((emoji) => console.log(`Uploaded ${emoji.name} emoji`)) - .catch(console.error); + const currentEmojis = await application.emojis.fetch(); + if (0 < currentEmojis.size) { + console.log('You already have emojis uploaded to the bot. Please remove them or upload these emojis manually.'); + } else { + const emojiFiles = readdirSync('./emojis').filter((file) => file.endsWith('.png')); + for (const emoji of emojiFiles) { + application.emojis + .create({ attachment: `./emojis/${emoji}`, name: emoji.split('.')[0] }) + .then((emoji) => console.log(`Uploaded ${emoji.name} emoji`)) + .catch(console.error); + } } client.destroy(); }); } -function setupBot(token: string) { +async function setupBot(token: string) { const client = new Client({ intents: [GatewayIntentBits.Guilds] }); client.login(token); client.on('ready', async () => { @@ -37,15 +44,26 @@ function setupBot(token: string) { }); console.log(`Bot is setup! Invite Url: https://discord.com/oauth2/authorize?client_id=${application.id}`); console.log( - `If you want to make the bot user installable, enable it on the Developer Portal. Link:https://discord.com/developers/applications/${application.id}/installation` + `If you want to make the bot user installable, enable it on the Developer Portal. Link: https://discord.com/developers/applications/${application.id}/installation` ); client.destroy(); }); } (async () => { + if (existsSync('config.json')) { + console.log(chalk.red(chalk.bold('Config file already exists.'))); + const check = await confirm({ message: 'Overrite Config File?', default: true }); + if (false === check) { + console.log(chalk.red(chalk.bold('Exiting...'))); + process.exit(0); + } else { + console.log(chalk.red(chalk.bold('Overwriting current config file...\n'))); + unlinkSync('config.json'); + } + } const token = await password({ - message: 'Discord Token', + message: 'Discord Token:', validate: (input) => { if ('' === input.trim()) { return 'Discord Token is required'; @@ -54,12 +72,13 @@ function setupBot(token: string) { } }); const spotifyClientId = await password({ - message: 'Spotify Client Id', + message: 'Spotify Client Id:', validate: (input) => '' !== input.trim() || 'Spotify Client Id is required' }); - const port = await number({ message: 'Port', default: 18173 }); + const port = await number({ message: 'Port:', default: 18173 }); + const ownerId = await input({ message: 'Bot Owner Discord Id (Used for logging errors):' }); const uploadEmojis = await confirm({ message: 'Upload Default Emojis?', default: true }); - if (uploadEmojis) uploadEmojisToBot(token); - setupBot(token); - writeFileSync('./config.json', JSON.stringify({ token, spotifyClientId, port }, null, 2)); + if (uploadEmojis) await uploadEmojisToBot(token); + await setupBot(token); + writeFileSync('./config.json', JSON.stringify({ token, spotifyClientId, port, ownerId }, null, 2)); })();