-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (52 loc) · 2.25 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
const { Client, Intents, Collection } = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require('fs');
const dotenv = require('dotenv');
dotenv.config();
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const commands = new Collection();
const token = process.env.BOT_TOKEN;
const commandFolders = fs.readdirSync('./commands');
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`);
commands.set(command.data.name, command);
}
}
client.once('ready', () => {
console.log(`${client.user.tag} sẵn sàng!`);
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
const command = commands.get(commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
interaction.reply('Đã xảy ra lỗi khi thực hiện lệnh này.');
}
});
client.login(token).then(() => {
console.log('Đang đăng ký lệnh...');
const commandsArray = commands.map(command => command.data.toJSON());
const rest = new REST({ version: '9' }).setToken(token);
rest.put(Routes.applicationCommands(client.user.id), { body: commandsArray })
.then(() => console.log('Đã đăng ký lệnh thành công!'))
.catch(error => console.error('Đã xảy ra lỗi khi đăng ký:', error));
});
require('./status');
client.on('guildCreate', async (guild) => {
try {
console.log(`Đã tham gia máy chủ: ${guild.name} (ID: ${guild.id}).`);
const commandsArray = commands.map(command => command.data.toJSON());
const rest = new REST({ version: '9' }).setToken(token);
await rest.put(Routes.applicationGuildCommands(client.user.id, guild.id), { body: commandsArray });
console.log(`Đã đăng ký lệnh cho máy chủ: ${guild.name} (ID: ${guild.id})`);
} catch (error) {
console.error(`Đã xảy ra lỗi khi đăng ký lệnh cho máy chủ: ${guild.name} (ID: ${guild.id})`, error);
}
});