-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.ts
68 lines (62 loc) · 2.11 KB
/
bot.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import fs from 'fs';
import { Client, Collection, Intents } from 'discord.js';
import { token } from './config';
import { IBotClient } from './models/botClient';
import { IBotCommand } from './models/botCommand';
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MEMBERS,
],
partials: ['MESSAGE', 'CHANNEL', 'REACTION', 'GUILD_MEMBER'],
});
const commands = new Collection<string, IBotCommand>();
const botClient: IBotClient = { client, commands };
export const startBot = (): void => {
console.log(__dirname);
const commandFiles = fs
.readdirSync(`${__dirname}/commands`)
.filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
// const command = require(`./commands/${file}`);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { command } = require(`${__dirname}/commands/${file}`);
// set a new item in the Collection
// with the key as the command name and the value as the exported module
botClient.commands.set(command.name, command);
}
const eventFiles = fs
.readdirSync(`${__dirname}/events`)
.filter((file) => file.endsWith('.js'));
for (const file of eventFiles) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { event } = require(`${__dirname}/events/${file}`);
if (event.needsClient === true) {
if (event.once) {
botClient.client.once(event.name, (...args: unknown[]) => {
event.execute(...args, botClient);
});
} else {
botClient.client.on(event.name, (...args: unknown[]) => {
event.execute(...args, botClient);
});
}
} else {
if (event.once) {
botClient.client.once(event.name, (...args: unknown[]) => {
event.execute(...args);
});
} else {
botClient.client.on(event.name, (...args: unknown[]) => {
event.execute(...args);
});
}
}
}
botClient.client.login(token);
};
export const stopBot = (): void => {
botClient.client.destroy();
};