Skip to content

Commit d7b762c

Browse files
committed
Good progress towards improving the workflow on this project
1 parent 63b170e commit d7b762c

File tree

5 files changed

+53
-41
lines changed

5 files changed

+53
-41
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
node_modules/
33
package-lock.json
44
.env
5+
nodemon.json
56

67
# Typescript Output
78
dist/
89

910
# IDEs
1011
.idea/
11-
.vscode/
12+
.vscode/

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"dependencies": {
1717
"@types/node": "^20.3.1",
18+
"dotenv": "^16.4.7",
1819
"sequelize": "^6.32.1",
1920
"sqlite3": "^5.1.6"
2021
}

src/baseBot.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { AllyClient } from "./interfaces/AllyClient.js";
22
import { config } from "./config.js"; // Not ready to do this yet
3+
import dotenv from "dotenv";
4+
dotenv.config()
5+
36

47
import {
58
GatewayIntentBits,
@@ -94,7 +97,7 @@ process.on("uncaughtException", (error) => {
9497
}
9598
});
9699

97-
client.login(config.token);
100+
client.login(process.env.BOT_TOKEN);
98101

99102
const loadCommands = (files: string[], location: string) => {
100103
files.forEach((fileName) => {

src/config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ interface Config {
1717
}
1818

1919
export const config: Config = {
20-
token: "token",
21-
prefix: "prefix",
22-
applicationID: "applicationID",
23-
clientID: "clientID",
20+
token: process.env.BOT_TOKEN,
21+
prefix: "=",
22+
applicationID: process.env.APPLICATION_ID,
23+
clientID: process.env.CLIENT_ID,
2424
botName: "botName",
2525
botAuthor: "botAuthor",
2626

src/registerCommands.ts

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,49 @@
1-
const { REST, Routes } = require('discord.js');
2-
const config = require('../dist/config.js');
3-
const fs = require('node:fs');
4-
const path = require('node:path');
1+
import {REST, Routes} from 'discord.js';
2+
import { config } from "./config.js"; // Not ready to do this yet
3+
import fs from 'node:fs';
4+
import path from 'node:path';
5+
import dotenv from 'dotenv';
6+
dotenv.config({path: path.join(__dirname, '..', '.env')});
7+
58

69
const commands = [];
7-
// Grab all the command files from the commands directory you created earlier
8-
const foldersPath = path.join(__dirname, 'commands');
9-
const commandFolders = fs.readdirSync(foldersPath);
10-
11-
for (const folder of commandFolders) {
12-
// Grab all the command files from the commands directory you created earlier
13-
const commandsPath = path.join(foldersPath, folder);
14-
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
15-
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
16-
for (const file of commandFiles) {
17-
const filePath = path.join(commandsPath, file);
18-
const command = require(filePath);
19-
commands.push(command.SlashCommand.command.toJSON());
20-
}
21-
}
10+
const commandsDirectory = path.join(__dirname, "commands");
2211

23-
// Construct and prepare an instance of the REST module
24-
const rest = new REST().setToken(config.config.token);
12+
const filterNonJsFiles = (items: string[]) => {
13+
return items.filter((files) => files.split(".").pop() === "js");
14+
};
2515

26-
// and deploy your commands!
27-
(async () => {
28-
try {
29-
console.log(`Started refreshing ${commands.length} application (/) commands.`);
16+
if (!fs.existsSync(commandsDirectory)) {
17+
fs.mkdirSync(commandsDirectory);
18+
}
3019

31-
// The put method is used to fully refresh all commands in the guild with the current set
32-
const data = await rest.put(
33-
config.config.production ? Routes.applicationCommands(config.config.clientID): Routes.applicationGuildCommands(config.config.clientID, config.config.guildID),
34-
{ body: commands },
35-
);
20+
const items = fs.readdirSync(commandsDirectory);
21+
const baseFiles = filterNonJsFiles(items);
22+
const folders = items.filter((files: string) => files.split(".").pop() != "js");
3623

37-
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
38-
} catch (error) {
39-
// And of course, make sure you catch and log any errors!
40-
console.error(error);
24+
for (const baseFile of baseFiles) {
25+
let properties = require(path.join(commandsDirectory, baseFile));
26+
commands.push(properties.SlashCommand.command.toJSON());
27+
}
28+
29+
for (const folderName of folders) {
30+
const folderItems = fs.readdirSync(path.join(commandsDirectory, folderName));
31+
const files = filterNonJsFiles(folderItems);
32+
for (const file of files) {
33+
let properties = require(path.join(commandsDirectory, folderName, file));
34+
commands.push(properties.SlashCommand.command.toJSON());
4135
}
42-
})();
36+
}
37+
38+
const rest = new REST().setToken(process.env.BOT_TOKEN);
39+
40+
try {
41+
console.log(`Started refreshing ${commands.length} application (/) commands.`);
42+
const data = rest.put(
43+
Routes.applicationGuildCommands(process.env.CLIENT_ID, "926369143186403329"),
44+
{ body: commands },
45+
);
46+
console.log(`Successfully reloaded ${commands.length} application (/) commands.`);
47+
} catch (error) {
48+
console.error(error);
49+
}

0 commit comments

Comments
 (0)