Skip to content

Instantly share code, notes, and snippets.

@mikkipastel
Created August 28, 2022 10:22
Show Gist options
  • Save mikkipastel/06f49afde4a01a2c4b52f830478c358c to your computer and use it in GitHub Desktop.
Save mikkipastel/06f49afde4a01a2c4b52f830478c358c to your computer and use it in GitHub Desktop.
default discord command bot
//include library
const { Client, GatewayIntentBits, SlashCommandBuilder, Routes } = require('discord.js');
const { REST } = require('@discordjs/rest');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
// map command list
const commands = [
new SlashCommandBuilder().setName('ping').setDescription('Replies with pong!')
].map(command => command.toJSON());
// regsiter command
const rest = new REST({ version: '10' }).setToken(process.env.token);
rest.put(Routes.applicationGuildCommands(process.env.clientId, process.env.guildId), { body: commands })
.then(() => console.log('Successfully registered application commands.'))
.catch(console.error);
//reply command
client.once('ready', () => {
console.log('Ready!');
});
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
const { commandName } = interaction;
if (commandName === 'ping') {
await interaction.reply('Pong!');
}
});
client.login(process.env.token);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment