-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (47 loc) · 1.69 KB
/
index.js
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
const fs = require("fs");
const path = require("path");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord.js");
const { Client, Collection, GatewayIntentBits } = require("discord.js");
require("dotenv").config();
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const rest = new REST({ version: "10" }).setToken(process.env.BOTTOKEN);
// Define the commands collection
const commands = new Collection();
async function deploycommands() {
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const { default: command } = await import(`file://${filePath}`);
commands.set(command.data.name, command); // Set the command in the collection
}
rest
.put(Routes.applicationCommands(process.env.CLIENTID), {
body: commands.map((command) => command.data.toJSON()),
})
.then(() => console.log(`Successfully registered Global commands.`));
}
deploycommands();
// Assign the commands collection to the client
client.commands = commands;
client.on("interactionCreate", async (interaction) => {
if (interaction.isChatInputCommand()) {
const command = interaction.client.commands.get(interaction.commandName);
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
}
});
client.login(process.env.BOTTOKEN);
client.once("ready", () => {
console.log("Ready!");
});