-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
115 lines (103 loc) · 3.89 KB
/
index.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const Discord = require('discord.js');
const config = require("./config");
const fs = require('fs');
const path = require('path');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');
const db = require('quick.db');
const client = new Discord.Client({
intents: 53608447,
});
client.db = db;
client.commands = new Discord.Collection();
function loadCommands(dir) {
try {
const commandFolders = fs.readdirSync(dir);
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`${dir}/${folder}`).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
try {
const command = require(`${dir}/${folder}/${file}`);
if (command.data && command.data.name) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${file} is missing a required "data" or "execute" property.`);
}
} catch (error) {
console.error(`Error loading command ${file}:`, error);
}
}
}
} catch (error) {
console.error('Error loading commands:', error);
}
}
loadCommands('./src/commands');
function loadEvents(dir) {
try {
const eventFolders = fs.readdirSync(dir);
for (const folder of eventFolders) {
const eventFiles = fs.readdirSync(`${dir}/${folder}`).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
try {
const event = require(`${dir}/${folder}/${file}`);
if (event.once) {
client.once(event.name, (...args) => {
try {
event.execute(...args, client);
} catch (error) {
console.error(`Error executing event ${event.name}:`, error);
}
});
} else {
client.on(event.name, (...args) => {
try {
event.execute(...args, client);
} catch (error) {
console.error(`Error executing event ${event.name}:`, error);
}
});
}
} catch (error) {
console.error(`Error loading event ${file}:`, error);
}
}
}
} catch (error) {
console.error('Error loading events:', error);
}
}
loadEvents('./src/events');
client.on('ready', () => {
const rest = new REST({ version: '10' }).setToken(client.token);
(async () => {
try {
const commands = [];
client.commands.forEach((command) => {
commands.push(command.data.toJSON());
});
await rest.put(
Routes.applicationCommands(client.user.id),
{ body: commands },
);
} catch (error) {
console.error('Error registering application commands:', error);
}
})();
});
process.on('unhandledRejection', (reason, promise) => {
console.log('----- Unhandled Rejection at -----');
console.log(promise);
console.log('----- Reason -----');
console.log(reason);
});
process.on('uncaughtException', (error) => {
console.log('----- Uncaught Exception -----');
console.log(error);
});
client.on('error', (error) => {
console.error('Discord client error:', error);
});
client.login(config.token).catch(error => {
console.error('Error logging in:', error);
});