Files
events
src
.gitignore
bot.ts
package-lock.json
package.json
tsconfig.json
Code
32 3,883
bot.ts
import { Client, GatewayIntentBits, SlashCommandBuilder } from 'discord.js';
import * as dotenv from 'dotenv';
dotenv.config();
const token = process.env.DISCORD_TOKEN;
if (!token || token === 'your_discord_bot_token_here') {
throw new Error('Configurez DISCORD_TOKEN dans le fichier .env avant de démarrer le bot.');
}
const client = new Client({
intents: [GatewayIntentBits.Guilds],
});
const commands = [
new SlashCommandBuilder()
.setName('hello')
.setDescription('Le bot vous salue.'),
].map((command) => command.toJSON());
client.once('clientReady', async (readyClient) => {
try {
await readyClient.application.commands.set(commands);
console.log(`Connecté en tant que ${readyClient.user.tag}`);
console.log('KODARI_READY');
} catch (error) {
console.error('Impossible d’enregistrer les commandes slash :', error);
}
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;
try {
if (interaction.commandName === 'hello') {
await interaction.reply(`Bonjour ${interaction.user}! 👋`);
}
} catch (error) {
console.error(`Erreur lors de la commande /${interaction.commandName} :`, error);
if (!interaction.replied && !interaction.deferred) {
await interaction.reply({
content: 'Une erreur est survenue lors de l’exécution de cette commande.',
ephemeral: true,
});
}
}
});
client.login(token).catch((error: unknown) => {
console.error('Échec de la connexion à Discord :', error);
process.exitCode = 1;
});