-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjonson.js
More file actions
79 lines (65 loc) · 2.74 KB
/
Copy pathjonson.js
File metadata and controls
79 lines (65 loc) · 2.74 KB
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
// Import module Discord.js v14
const { Client, GatewayIntentBits, Collection } = require('discord.js');
const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');
// Membuat instance client Discord
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
// Tambahkan intent-intent lain yang diperlukan sesuai kebutuhan
],
});
// Membuat koleksi untuk menyimpan command
client.commands = new Collection();
// Mendapatkan token bot Anda dari environment variable atau file konfigurasi
const BOT_TOKEN = process.env.BOT_TOKEN || require('./config.json').botToken;
// Mendapatkan prefix bot Anda dari environment variable atau file konfigurasi
const PREFIX = process.env.PREFIX || require('./config.json').prefix;
// Mendapatkan direktori cogs
const COGS_DIR = './cogs/';
// Membaca file cogs dan menginisialisasi setiap cog
fs.readdirSync(COGS_DIR).forEach((file) => {
if (file.endsWith('.js')) {
const cog = require(`${COGS_DIR}/${file}`);
client.commands.set(cog.data.name, cog);
}
});
// Event handler untuk event ready
client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}`);
// Menyinkronkan slash command secara global
const rest = new REST({ version: '10' }).setToken(BOT_TOKEN);
try {
await rest.put(
Routes.applicationCommands(client.user.id),
{ body: client.commands.map(command => command.data.toJSON()) }
);
console.log('Slash command berhasil disinkronkan secara global!');
} catch (error) {
console.error(`Gagal menyinkronkan slash command secara global: ${error}`);
}
});
// Event handler untuk event interactionCreate
client.on('interactionCreate', async (interaction) => {
if (!interaction.isButton() && !interaction.isCommand()) return; // Memeriksa apakah interaksi merupakan slash command atau button interaction
if (interaction.isButton()) {
// Meng-handle button interaction (jika ada)
// implementasi button interaction Anda
}
if (interaction.isCommand()) {
// Meng-handle slash command
const command = client.commands.get(interaction.commandName); // Mendapatkan command berdasarkan nama
if (!command) return; // Jika command tidak ditemukan, keluar
try {
await command.execute(interaction); // Menjalankan fungsi execute dari command
} catch (error) {
console.error(`Gagal menjalankan perintah "${interaction.commandName}": ${error}`);
}
}
});
// Mengganti TOKEN_BOT_ANDA dengan token bot yang Anda dapatkan dari Discord Developer Portal
client.login(BOT_TOKEN);