-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
118 lines (103 loc) · 3.33 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
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
116
117
118
import { Client, Interaction, REST, Routes } from "discord.js";
import * as fs from "fs";
import startCron from "./cron";
import path from "path";
require("dotenv").config();
const token = process.env.DISCORD_TOKEN;
if (!token) {
throw new Error("Discord token is required");
}
const clientId = process.env.APP_ID;
if (!clientId) {
throw new Error("App ID is required");
}
const client = new Client({
intents: ["Guilds", "GuildMessages"],
});
client.login(token);
// Load commands
const commandFiles = fs
.readdirSync(path.join(__dirname, "./commands"))
.filter((file) => file.endsWith(".ts") || file.endsWith(".js"));
const commands = commandFiles.map((file) =>
require(path.join(__dirname, `./commands/${file}`))
);
// Register commands
const rest = new REST({ version: "9" }).setToken(token);
(async () => {
try {
console.log("Started refreshing application (/) commands.");
const body = commands.map((c) => {
return {
name: c.name,
description: c.description,
options: c.options,
};
});
await rest.put(Routes.applicationCommands(clientId), {
body: body,
});
console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
})();
const buttonFiles = fs
.readdirSync(path.join(__dirname, "./buttons"))
.filter((file) => file.endsWith(".ts") || file.endsWith(".js"));
const buttons = buttonFiles.map((file) =>
require(path.join(__dirname, `./buttons/${file}`))
);
const modalFiles = fs
.readdirSync(path.join(__dirname, "./modals"))
.filter((file) => file.endsWith(".ts") || file.endsWith(".js"));
const modals = modalFiles.map((file) =>
require(path.join(__dirname, `./modals/${file}`))
);
// Command handler
client.on("interactionCreate", async (interaction: Interaction) => {
if (interaction.guildId !== process.env.GUILD_ID) return;
if (interaction.isCommand()) {
const command = commands.find((c) => c.name === interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
} else if (interaction.isButton()) {
for (let index = 0; index < buttonFiles.length; index++) {
const buttonName = buttonFiles[index]
.replace(".ts", "")
.replace(".js", "");
if (interaction.customId.startsWith(buttonName))
buttons[index](interaction);
}
} else if (interaction.isModalSubmit()) {
for (let index = 0; index < modalFiles.length; index++) {
const modalName = modalFiles[index].replace(".ts", "").replace(".js", "");
if (interaction.customId.startsWith(modalName))
modals[index](interaction);
}
}
});
client.on("ready", () => {
// Start cron
startCron(client);
});
// Load events
const eventsFiles = fs
.readdirSync(path.join(__dirname, "./events"))
.filter((file) => file.endsWith(".ts") || file.endsWith(".js"));
const events = eventsFiles.map((file) =>
require(path.join(__dirname, `./events/${file}`))
);
for (let index = 0; index < eventsFiles.length; index++) {
const eventName = eventsFiles[index].replace(".ts", "").replace(".js", "");
console.log(`Registering event: ${eventName}`);
client.on(eventName, events[index]);
}