A Guide to Discord Bots

Debugging / Testing Tips

Error messages

If you have an error that crashes your bot:

C:\Node_Projects\MyDiscordBot\index.js:42
        break;
        ^^^^^

SyntaxError: Illegal break statement

index.js:42 gives us two informations:

  • The error is in index.js
  • The error is at line 42

To fix your error, you will need to read the error message, SyntaxError: Illegal break statement.
In this case, it simply means that we used break outside of a for loop.
If you don't know what the error message means, you can just Google it. Which brings us to the next part.

Google knows all

If you need to search for an error on Google, I suggest adding site:stackoverflow.com after the error message.
If you don't know, stackoverflow.com is a website for developers to help each others.
People can ask questions about something from a programming language and get answers from other members.
As this is a very largely known website, I suggest searching for your question before asking one; you will most likely get an answer just by searching for it.

Quick testing

The 'ready' event is called every time your bot connects to Discord through client.login().
It is quicker to use it than having to use a command through the 'message' event.
You can connect to your bot account even if another client already uses it.

const testing = true; // false to push updates

client.on('ready', () => {

        // Your testing code
        if (testing) {
                console.log('My testing code goes here!');

                // You can get servers and channels like this
                let myServer = client.guilds.get('guild_id');
                let myChannel = myServer.channels.get('channel_id');
                myChannel.send('Hello World!');

                // The one-liner way
                client.guilds.get('guild_id').channels.get('channel_id').send('Hello World! 2');

                // Getting any user
                client.fetchUser('user_id').then(user =>
                        console.log(user.tag);
                });

                return; // <- don't forget this
        }

        // Your bot's code
        console.log('My REAL code!');
});

client.on('message', () => {
        // On each messsage
        // Do nothing if in testing mode
        if (testing)
                return;
});

// More events - don't forget to add 'if (testing)'!

Events

When debugging an event, you need it to be emitted.
This means that for testing guildMemberAdd, which is emitted when a member joins a server which your bot is in, you need... a member to join a server which contains your bot.
Oooor you can emit one with client.emit().

const client = new Discord.Client();

client.on('ready', () => {
        // Simulate the user joining a server
        client.guilds.get('guild_id').fetchMember('user_id').then(member => {
                client.emit('guildMemberAdd', member);
        });
});

A reminder of available events.

Roles

const client = new Discord.Client();

client.on('ready', () => {

        // Get every role and show their ID and name
        client.guilds.get('300977142853009408').roles.forEach(role => {
                console.log(role.id, role.name));
        }

        // Same but ordered by position: lowest to highest
        client.guilds.get('300977142853009408')._sortedRoles.forEach(role => {
                console.log(role.id, role.name));
        }
});

The Role object.

Channels

const client = new Discord.Client();

client.on('ready', () => {

        // Get every channel and show their ID and name
        client.guilds.get('300977142853009408').channels.forEach(channel => {
                // Exclude categories
                if (channel.type != 'category')
                        console.log(channel.id, channel.name));

                // Include only categories
                /*if (channel.type == 'category')
                        console.log(channel.id, channel.name));*/
        }

        // Same but ordered by position: lowest to highest
        // Might not work well when including both channels and categories
        client.guilds.get('300977142853009408')._sortedChannels.forEach(channel=> {
                // Exclude categories
                if (channel.type != 'category')
                        console.log(channel.id, channel.name));

                // Include only categories
                /*if (channel.type == 'category')
                        console.log(channel.id, channel.name));*/
        }
});

The Channel, the TextChannel, the DMChannel, the GroupDMChannel objects.

Guilds

const client = new Discord.Client();

client.on('ready', () => {
        client.guilds.forEach(guild => console.log(guild.id, guild.name));
});

The Guild object.

results matching ""

    No results matching ""