Skip to content

Commit

Permalink
Config override check
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Sep 24, 2024
1 parent eb4fb56 commit e70cd4b
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions Setup.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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';
Expand All @@ -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));
})();

0 comments on commit e70cd4b

Please sign in to comment.