Files
src
.gitignore
package.json
Code
3 68
src/index.js
import 'dotenv/config';
import {
Client,
Events,
GatewayIntentBits,
REST,
Routes,
SlashCommandBuilder,
} from 'discord.js';
const { DISCORD_TOKEN, CLIENT_ID, GUILD_ID } = process.env;
if (!DISCORD_TOKEN || !CLIENT_ID) {
throw new Error('DISCORD_TOKEN and CLIENT_ID must be set in the environment.');
}
const commands = [
new SlashCommandBuilder()
.setName('hi')
.setDescription('Replies with a hello.')
.toJSON(),
];
const rest = new REST({ version: '10' }).setToken(DISCORD_TOKEN);
const commandRoute = GUILD_ID
? Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID)
: Routes.applicationCommands(CLIENT_ID);
await rest.put(commandRoute, { body: commands });
const client = new Client({
intents: [GatewayIntentBits.Guilds],
});
client.once(Events.ClientReady, (readyClient) => {
console.log(`Logged in as ${readyClient.user.tag}`);
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand() || interaction.commandName !== 'hi') {
return;
}
await interaction.reply('Hello!');
});
await client.login(DISCORD_TOKEN);