A Discord Bot connected to your Pterodactyl API.
1import { Client, Collection, GatewayIntentBits } from "discord.js";
2import * as fs from "node:fs";
3import * as path from "node:path";
4import { fileURLToPath } from "url";
5import { Logger } from "./Logger.js";
6import { config } from "dotenv";
7config();
8
9const __filename = fileURLToPath(import.meta.url);
10const __dirname = path.dirname(__filename);
11
12class Bot {
13 constructor() {
14 // Set bot token
15 this.token = process.env.BOT_TOKEN;
16 // Create a new client instance
17 this.client = new Client({
18 intents: [
19 GatewayIntentBits.Guilds,
20 GatewayIntentBits.GuildMembers,
21 GatewayIntentBits.GuildMessages,
22 ],
23 });
24 }
25
26 async run() {
27 // Log in to Discord with your token
28 await this.client.login(this.token);
29 // Launch registration of all slash commands
30 await this.registerCommands();
31 // Launch registration of events
32 await this.registerEvents();
33 }
34
35 async registerCommands() {
36 // Create a new collection instance for commands
37 this.client.commands = new Collection();
38 const commandsPath = path.join(__dirname, "..", "commands");
39 const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith(".js"));
40 for (const file of commandFiles) {
41 const filePath = path.join(commandsPath, file);
42 let command = await import(filePath);
43 command = command.default;
44 // Set a new item in the Collection with the key as the command name and the value as the exported module
45 if (command?.data && command?.execute) this.client.commands.set(command.data.name, command);
46 else Logger.warn(`The command at ${filePath} is missing a required "data" or "execute" property.`);
47 }
48 }
49
50 async registerEvents() {
51 const eventsPath = path.join(__dirname, "..", "events");
52 const eventsFiles = fs.readdirSync(eventsPath).filter((file) => file.endsWith(".js"));
53
54 for (const file of eventsFiles) {
55 const filePath = path.join(eventsPath, file);
56 let event = await import(filePath);
57 event = event.default;
58 if (event.once) this.client.once(event.name, (...args) => event.execute(...args));
59 else this.client.on(event.name, (...args) => event.execute(...args));
60 }
61 }
62}
63
64export { Bot };