A Guide to Discord Bots

Fetching Users & Members

Working with IDs

We will start working with IDs from now on.
How to get IDs? Why?
IDs are useful because they never change: they are the most reliable property of an object to differentiate it from another.

To get IDs on Discord, go to your User Settings (little cog on the bottom left corner), click 'Appearance', scroll down and enable 'Developer Mode'.
You can now right-click a server, channel, user or message to get its ID!
To get a role ID, you can:

  • Get it with a bot -> see 'Debugging / Testing Tips' -> the 'Roles' part
  • Mention it (when possible) and add a \ before it (it will ping / send a notification to those who have this role)

Fetching Members

Let's add a *whois command, to get informations about someone from the same server.

let command = message.content.split(' ')[0].slice(1);
let args = message.content.replace('.' + command, '').trim();

switch (command) {
    case 'whois': {
        // Usage: *whois <@user>, *whois user_id

        // Replace mentions to IDs
        // A mention is formatted like this: <@user_id> or <@!user_id>
        let userID = args.includes('<@!') ? args.replace('<@!', '').replace('>', '')
            : args.includes('<@') ? args.replace('<@', '').replace('<', '') : '';

        if (userID == '') {
            message.reply('Invalid user ID or mention.');
            return;
        }

        // Check the 'Promises' part to learn about .then() and .catch()!
        message.guild.fetchMember(userID).then(member => {
            // Got the member!
            message.channel.send('Member found: ' + member.user.tag
            + '\nJoined: ' + member.joinedAt);
        }).catch(() => {
            // Error, member not found
            message.channel.send('Could not find a member with the given ID or mention!');
        });
    }
}

The Guild object.
Nothing too complicated, .then() and .catch() are explained in the 'Promises' part.
The conditional operator is very easy to understand: condition ? if true : else.

  • if args.includes('<@!): userID = args.replace('<@!', '').replace('>', '')
  • else if args.includes('<@'): userID = args.replace('<@', '').replace('<', '')
  • else: userID = '';

Fetching Users

We used fetchMember() above, and it contains a .user property, so why bother?
Well, the answer is simple: you can get users even if your bot doesn't share a server with them.
It is useful for operations where you can't get a Member object, and is very simple to use too.
Let's get our users with... fetchUser()!

const client = new Discord.Client(); // just a reminder

let command = message.content.split(' ')[0].slice(1);
let args = message.content.replace('.' + command, '').trim();

switch (command) {
    case 'getuser': {
        // Usage: *getuser user_id

        // args = user_id, because we only take one argument
        if (args == '') {
            message.reply('Syntax: *getuser user_id');
            return;
        }

        // Check the 'Promises' part to learn about .then() and .catch()!
        client.fetchUser(args).then(user => {
            // Got the user!
            message.channel.send('Found user: ' + user.tag
            + '\nAvatar: ' + user.displayAvatarURL);
        }).catch(() => {
            // User not found
            message.channel.send('Could not find user with the given ID.');
        });
    }
}

The Client object, as seen in the first parts.

results matching ""

    No results matching ""