-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.js
87 lines (74 loc) · 2.15 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const { Client, GatewayIntentBits } = require("discord.js");
const fs = require("fs");
const path = require("path");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord.js");
const Logger = require("./utils/logger");
require("dotenv").config();
const startTimestamp = new Date();
const logFileName = `${startTimestamp
.toLocaleDateString("en-US", {
month: "2-digit",
day: "2-digit",
year: "numeric",
})
.replace(/\//g, "_")}_${startTimestamp
.toLocaleTimeString("en-US", {
hour12: false,
hour: "2-digit",
minute: "2-digit",
})
.replace(/:/g, "-")}.log`;
const logger = new Logger(logFileName);
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
client.commands = [];
client.modules = [];
const modulesPath = path.join(__dirname, "modules");
fs.readdirSync(modulesPath).forEach((file) => {
if (file.endsWith(".js")) {
const ModuleClass = require(path.join(modulesPath, file));
const moduleInstance = new ModuleClass(client);
client.modules.push(moduleInstance);
if (moduleInstance.registerCommands) {
moduleInstance.registerCommands();
}
}
});
client.once("ready", async () => {
try {
const commandsData = client.commands.map((command) => command);
const uniqueCommandsData = commandsData.filter(
(value, index, self) =>
index === self.findIndex((t) => t.name === value.name)
);
logger.info(
`Registering the following commands: ${JSON.stringify(
uniqueCommandsData
)}`
);
const rest = new REST({ version: "10" }).setToken(
process.env.DISCORD_BOT_TOKEN
);
await rest.put(Routes.applicationCommands(client.user.id), {
body: uniqueCommandsData,
});
logger.info("Slash commands registered successfully.");
logger.alert(`Logged in as ${client.user.tag}!`);
} catch (error) {
logger.error(`Error registering slash commands: ${error.message}`);
}
});
client.on("interactionCreate", async (interaction) => {
for (const module of client.modules) {
if (module.handleInteraction) {
await module.handleInteraction(interaction);
}
}
});
client.login(process.env.DISCORD_BOT_TOKEN);