This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
84 lines (72 loc) · 2.61 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
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
// import { createRequire } from "module";
// const require = createRequire(import.meta.url);
// import { fileURLToPath } from 'url';
// import { dirname } from 'path';
// const __filename = fileURLToPath(import.meta.url);
// const __dirname = dirname(__filename);
const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
// Require the necessary discord.js classes
const { Client, GatewayIntentBits, Collection } = require('discord.js');
// Create a new client instance
const client = new Client({
intents: [GatewayIntentBits.Guilds]
});
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.cjs') || file.endsWith('.js'));
const dotenv = require('dotenv');
dotenv.config();
const TOKEN = process.env['TOKEN'];
const TEST_GUILD_ID = process.env['TEST_GUILD_ID'];
const commands = [];
// Creating a collection for commands in client
client.commands = new Collection();
(async () => {
for (const file of commandFiles) {
let command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
client.commands.set(command.data.name, command);
}
})();
// When the client is ready, this only runs once
client.once('ready', () => {
console.log('Ready!');
// Registering the commands in the client
const CLIENT_ID = client.user.id;
const rest = new REST({
version: '9'
}).setToken(TOKEN);
(async () => {
try {
if (!TEST_GUILD_ID) {
await rest.put(
Routes.applicationCommands(CLIENT_ID), {
body: commands
},
);
console.log('Successfully registered application commands globally');
} else {
await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, TEST_GUILD_ID), {
body: commands
},
);
console.log('Successfully registered application commands for development guild');
}
} catch (error) {
if (error) console.error(error);
}
})();
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(client, interaction);
} catch (error) {
if (error) console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.login(TOKEN);