-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (53 loc) · 1.7 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
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({
intents: [
GatewayIntentBits.MessageContent,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
],
});
client.commands = new Collection();
// General function to load items (commands or events)
function loadItems(dir, type) {
const files = fs.readdirSync(dir);
for (const file of files) {
const fullPath = path.join(dir, file);
if (fs.lstatSync(fullPath).isDirectory()) {
loadItems(fullPath, type);
}
else if (file.endsWith('.js')) {
const item = require(fullPath);
if (type === 'commands' && 'data' in item && 'execute' in item) {
client.commands.set(item.data.name, item);
}
else if (type === 'events') {
if (item.once) {
client.once(item.name, (...args) => item.execute(...args));
}
else {
client.on(item.name, (...args) => item.execute(...args));
}
}
else {
console.log(`[WARNING] The ${type} at ${fullPath} is missing required properties.`);
}
}
}
}
// Start recursion from root directories
loadItems(path.join(__dirname, 'src/commands'), 'commands');
loadItems(path.join(__dirname, 'src/events'), 'events');
// Instantiate services
const getRepositories = require('./src/utils/getRepositories');
const GuildProfileService = require('./src/services/GuildProfileService');
let guildProfileService;
getRepositories()
.then((repositories) => {
guildProfileService = new GuildProfileService(repositories.guildProfileRepository);
client.guildProfileService = guildProfileService;
})
.catch(console.error);
client.login(token);