Skip to content

Commit

Permalink
Use a special file to register commands
Browse files Browse the repository at this point in the history
  • Loading branch information
BurakYs committed Dec 7, 2024
1 parent 04c322d commit 7d73c5e
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1 +1 @@
TOKEN=
BOT_TOKEN=
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"start": "cross-env NODE_ENV=production node .",
"dev": "tsc && cross-env NODE_ENV=development node .",
"register-commands": "node . --redeployCommands",
"register-commands": "cross-env NODE_ENV=production node ./dist/registerCommands.js",
"build": "tsc",
"lint": "eslint"
},
Expand All @@ -17,14 +17,12 @@
"dotenv": "^16.4.5",
"glob": "^11.0.0",
"module-alias": "^2.2.3",
"tslog": "^4.9.3",
"yargs": "^17.7.2"
"tslog": "^4.9.3"
},
"devDependencies": {
"@eslint/js": "^9.12.0",
"@types/module-alias": "^2.0.4",
"@types/node": "^22.7.4",
"@types/yargs": "^17.0.33",
"cross-env": "^7.0.3",
"eslint": "~9.12.0",
"typescript-eslint": "^8.8.0"
Expand Down
72 changes: 0 additions & 72 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@ import 'module-alias/register';
import '@/utils/classes/Logger';

import Client from '@/loaders/client';
import yargs from 'yargs';
import checkEnvironmentVariables from '@/utils/checkEnvironmentVariables';

type ProcessArgv = {
redeployCommands: boolean;
redeploy: boolean;
}
checkEnvironmentVariables();

const argv = yargs.argv as Partial<ProcessArgv>;
Client.start({
registerCommands: !!argv.redeployCommands
});
Client.start();
18 changes: 2 additions & 16 deletions src/loaders/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import EventLoader from '@/loaders/event';

import type { CommandData, SendMessageOptions } from '@/types';

type StartOptions = {
registerCommands: boolean;
}

class Client extends DiscordClient<true> {
commands: CommandData[] = [];

Expand All @@ -20,23 +16,13 @@ class Client extends DiscordClient<true> {
});
}

async start(options: Partial<StartOptions>) {
if (!process.env.TOKEN) {
global.logger.fatal('Please set the TOKEN in the .env file');
process.exit(1);
}

if (options.registerCommands) {
await CommandLoader.loadCommands(true);
process.exit(0);
}

async start() {
process.on('unhandledRejection', (error) => global.logger.error(error));
process.on('uncaughtException', (error) => global.logger.error(error));

this.extendPrototypes();

await this.login(process.env.TOKEN).catch((error) => {
await this.login(process.env.BOT_TOKEN).catch((error) => {
global.logger.error(error);
process.exit(1);
});
Expand Down
2 changes: 1 addition & 1 deletion src/loaders/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class CommandLoader {
}));

if (registerOnly) {
const token = process.env.TOKEN!;
const token = process.env.BOT_TOKEN!;
const botId = Buffer.from(token.split('.')[0], 'base64').toString();
const rest = new REST({ version: '10' }).setToken(token);

Expand Down
10 changes: 10 additions & 0 deletions src/registerCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'dotenv/config';
import 'module-alias/register';
import '@/utils/classes/Logger';

import CommandLoader from '@/loaders/command';
import checkEnvironmentVariables from '@/utils/checkEnvironmentVariables';

checkEnvironmentVariables();

CommandLoader.loadCommands(true);
9 changes: 9 additions & 0 deletions src/utils/checkEnvironmentVariables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function checkEnvironmentVariables() {
const requiredVariables = ['BOT_TOKEN'];
const missingVariables = requiredVariables.filter(env => !process.env[env]);

if (missingVariables.length) {
global.logger.fatal(`Missing environment variables: ${missingVariables.join(', ')}`);
process.exit(1);
}
}

0 comments on commit 7d73c5e

Please sign in to comment.