Files
.gitignore
index.js
package.json
Code
3 80
index.js
require('dotenv').config();
const {
Client,
Events,
GatewayIntentBits,
REST,
Routes,
SlashCommandBuilder
} = require('discord.js');
const { DISCORD_TOKEN, CLIENT_ID } = process.env;
if (!DISCORD_TOKEN || !CLIENT_ID) {
throw new Error('Set DISCORD_TOKEN and CLIENT_ID in your .env file.');
}
const commands = [
new SlashCommandBuilder()
.setName('say')
.setDescription('Make the bot say something.')
.addStringOption(option =>
option
.setName('message')
.setDescription('What the bot should say')
.setRequired(true)
)
.toJSON()
];
const client = new Client({
intents: [GatewayIntentBits.Guilds]
});
client.once(Events.ClientReady, async readyClient => {
try {
const rest = new REST({ version: '10' }).setToken(DISCORD_TOKEN);
await rest.put(Routes.applicationCommands(CLIENT_ID), { body: commands });
console.log(`Ready as ${readyClient.user.tag}; /say is registered.`);
} catch (error) {
console.error('Failed to register /say:', error);
}
});
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand() || interaction.commandName !== 'say') {
return;
}
const message = interaction.options.getString('message', true);
await interaction.reply({
content: message,
allowedMentions: { parse: [] }
});
});
client.on(Events.Error, error => {
console.error('Discord client error:', error);
});
client.login(DISCORD_TOKEN).catch(error => {
console.error('Failed to log in to Discord:', error);
process.exitCode = 1;
});