-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.ts
76 lines (66 loc) · 2.1 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
69
70
71
72
73
74
75
76
import { Interaction, Message } from "discord.js";
import fs from "node:fs";
import path from "node:path";
import { Client, Collection, GatewayIntentBits } from "discord.js";
import { env } from "./lib/config";
import { seed } from "./core/seed"; // Optional way to seed events
import { BaseEvent, EventHandler } from "./core/eventHandler";
import { initializeDb } from "./db/seed";
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
const commands = new Collection();
const pathToCommands = path.join(__dirname, "commands");
const commandFiles: string[] = fs
.readdirSync(pathToCommands)
.filter((file: string) => file.endsWith(".ts"));
(async () => {
await initializeDb();
for (const file of commandFiles) {
const filePath = path.join(pathToCommands, file);
const command = await import(filePath);
if ("data" in command && "execute" in command) {
commands.set(command.data.name, command);
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
);
}
}
})();
seed.map((event) => EventHandler.addEvent(event as BaseEvent)); // Optional way to seed events
client.once("ready", () => {
console.log(`Ready! Logged in as ${client.user.tag}`);
});
client.on("messageCreate", async (msg: Message) => {
try {
if (!msg.author.bot) {
EventHandler.handleEvent(msg);
}
} catch (err) {
console.log(err);
}
});
client.on("interactionCreate", async (interaction: Interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = commands.get(interaction.commandName) as {
execute: (interaction: Interaction) => Promise<void>;
};
if (!command)
return console.error(
`No command matching ${interaction.commandName} was found.`
);
try {
await command.execute(interaction);
} catch (error) {
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
});
client.login(env.get("DISCORD_TOKEN"));