TOOLBOX Course

Make a Discord bot in JavaScript

Go back

We are storing here tips and links to resources that will help you when making a Discord bot in JavaScript.

To use a Discord bot, you need to create a bot and get your bot token. That's something private that should not be put on GitHub.

// based on v12
const Discord = require('discord.js');
const client = new Discord.Client();

client.once('ready', () => { console.debug(`Logged in as ${client.user.tag}!`) });

client.login('your-token');

// when a message is received
client.on('message', async (message) => { // check event message
    // it's a normal message and not from us so we process it
    if (message.type === 'DEFAULT' && message.author.hasOwnProperty("bot")
        && !message.author.bot) {
        console.log("channel:"+message.channel);
        console.log("message:"+message.content.trim());
        // sending a reply
        message.reply("response");
        // sending a simple text message
        // you may change channel by chaning the id
        // message.channel.id
        await message.channel.send("message")
    }
}

I won't dig more into the details,