This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
218 lines (200 loc) · 6.2 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//dependancy for discordjs
import {
Client,
GatewayIntentBits,
Collection,
ClientApplication,
InteractionType,
Interaction,
CommandInteraction,
Partials,
ButtonInteraction,
ModalSubmitInteraction,
SelectMenuInteraction,
} from "discord.js";
import fs, { read, readdirSync } from "fs";
import dotenv, { config } from "dotenv";
import { regCMD } from "./src/deploy-commands";
import { devConfig } from "./devconfig";
import path from "node:path";
import cron from "node-cron";
export type TaskFunction = () => void;
dotenv.config();
export const client: any = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMessageTyping,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.DirectMessageTyping,
GatewayIntentBits.GuildEmojisAndStickers,
],
partials: [
Partials.Channel, // Required to receive DMs
],
});
/*
The following code below takes all the events in the events folder and put it in an array and filters it by .js files
The entire thing allows handling events to be as easy as adding it to the events folder and then restarting the bot
*/
const eventPath = path.join(__dirname, "src/events");
const eventFiles = fs
.readdirSync(eventPath)
.filter((file) => file.endsWith(".js"));
// This retrieves the event files and runs them if they should be run once or constantly ↓ this actually runs the event files code
for (const file of eventFiles) {
const filePath = path.join(eventPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args: any) => event.execute(...args));
} else {
client.on(event.name, (...args: any) => event.execute(...args));
}
}
// Get the list of task files in the tasks folder
const tasksFolder = path.join(__dirname, "src/tasks");
const taskFiles = fs
.readdirSync(tasksFolder)
.filter((file) => file.endsWith(".js"));
taskFiles.forEach((file: string) => {
// Dynamically import the task module
const taskModule: { default: TaskFunction } = require(path.join(
tasksFolder,
file
));
// Find the exported function and call it to schedule the task
const taskFunction = taskModule.default;
if (!taskFunction) return;
taskFunction();
});
client.commands = new Collection();
// This gets the command modules from the command folders
const cmdPath = path.join(__dirname, "src/commands");
const commandFiles = fs
.readdirSync(cmdPath)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(cmdPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
}
// This executes an Application commands when a player does a Application command
client.on("interactionCreate", async (interaction: CommandInteraction) => {
const command = client.commands.get(interaction.commandName);
if (!command) return;
if (interaction.isChatInputCommand()) {
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
}
} else if (interaction.isContextMenuCommand()) {
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
}
} else if (interaction.isAutocomplete()) {
try {
await command.autocomplete(interaction);
} catch (error) {
console.error(error);
}
}
});
client.buttons = new Collection();
client.modals = new Collection();
client.selectMenus = new Collection();
const compPath = path.join(__dirname, "src/components");
const componentFolders = readdirSync(compPath);
for (const folder of componentFolders) {
const comps = path.join(compPath, folder);
const componentFiles = readdirSync(comps).filter((file) =>
file.endsWith(".js")
);
switch (folder) {
case "buttons":
for (const file of componentFiles) {
const filePath = path.join(compPath, folder, file);
const button = require(filePath);
client.buttons.set(button.data.name, button);
}
break;
case "modals":
for (const file of componentFiles) {
const filePath = path.join(compPath, folder, file);
const modal = require(filePath);
client.modals.set(modal.data.name, modal);
}
break;
case "selectMenus":
for (const file of componentFiles) {
const filePath = path.join(compPath, folder, file);
const selectmenu = require(filePath);
client.selectMenus.set(selectmenu.data.name, selectmenu);
}
break;
default:
break;
}
}
client.on(
"interactionCreate",
async (
interaction:
| ButtonInteraction
| ModalSubmitInteraction
| SelectMenuInteraction
) => {
if (interaction.isButton()) {
const button = client.buttons.get(interaction.customId);
if (!button) return;
try {
await button.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while pressing this button!",
ephemeral: true,
});
}
} else if (interaction.isAnySelectMenu()) {
const selectMenu = client.selectMenus.get(interaction.customId);
if (!selectMenu) return;
try {
await selectMenu.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while selecting this option!",
ephemeral: true,
});
}
} else if (interaction.isModalSubmit()) {
const modal = client.modals.get(interaction.customId);
if (!modal) return;
try {
await modal.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while submitting this modal!",
ephemeral: true,
});
}
}
}
);
//This is what logs the bot in
client.login(process.env.TOKEN);
client.on("ready", async () => {
console.log(
`The bot is up! Logged in as ${client.user?.tag} at ${client.readyAt}`
);
if (devConfig.registerCmd === true) {
regCMD(client.user.id);
}
});