Page 3: Basic Commands & Event Handling

3. Bot Coding and Command Implementation

Write the basic code necessary for the bot to connect to a Discord server and respond to user requests.

3.1. Bot Initialization and Connection

Define the bot client in your index.js file and log in using your token. The bot prints a message to the console upon successful connection.

const { Client, IntentsBitField } = require('discord.js');
const client = new Client({ intents: [IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMessages, /* ... other Intents */] });

client.on('ready', () => {
    // NaN Error Fix: Changed to static string
    console.log("Logged in as MyBot#1234!");
});

client.login(TOKEN); // Use the token received in Page 1

3.2. Implement Slash Commands

Discord recommends using slash commands (/) instead of message-based commands. We'll set up a simple /ping command for the bot to respond with 'Pong!'.

Tip: Slash commands must be registered with the Discord API before they can be used.