-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
51 lines (41 loc) · 1.67 KB
/
index.ts
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
import { client } from './bot-constants';
import { token } from './config';
import { Collection } from 'discord.js';
import { warn } from './services/logger';
import fs = require('node:fs');
import path = require('node:path');
import { Errors } from './strings';
import { template } from './utils';
client.commands = new Collection();
client.buttons = new Collection();
loadItems('command', 'commands', client.commands);
loadItems('bouton', 'buttons', client.buttons);
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.ts') || file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once)
client.once(event.name, (...args) => event.execute(...args));
else
client.on(event.name, (...args) => event.execute(...args));
}
(async () =>
await client.login(token)
)();
function loadItems(itemType: string, folderName: string, collection: Collection<string, any>) {
const itemPath = path.join(__dirname, folderName);
const itemFolders = fs.readdirSync(itemPath).filter(folder => !folder.endsWith('.DS_Store'));
for (const folder of itemFolders) {
const itemFilesPath = path.join(itemPath, folder);
const itemFiles = fs.readdirSync(itemFilesPath).filter(file => file.endsWith('.ts') || file.endsWith('.js'));
for (const file of itemFiles) {
const filePath = path.join(itemFilesPath, file);
const item = require(filePath);
if ('data' in item && 'execute' in item)
collection.set(item.data.name, item);
else
warn(template(Errors.missingDataOrExecute, {itemType: itemType, filePath: filePath}));
}
}
}