Untitled
Gray
plain_text
8 months ago
1.1 kB
5
Indexable
const { Client, GatewayIntentBits, REST, Routes, SlashCommandBuilder } = require('discord.js');
require('dotenv').config(); // Load token from .env file
const client = new Client({
intents: [GatewayIntentBits.Guilds]
});
// Register a simple slash command
const commands = [
new SlashCommandBuilder().setName('ping').setDescription('Replies with Pong!')
].map(command => command.toJSON());
const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);
client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}`);
try {
console.log("Refreshing slash commands...");
await rest.put(
Routes.applicationCommands(client.user.id),
{ body: commands }
);
console.log("Slash commands registered!");
} catch (error) {
console.error(error);
}
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
if (interaction.commandName === 'ping') {
await interaction.reply('Pong!');
}
});
client.login(process.env.TOKEN);
Editor is loading...
Leave a Comment