DEV Community

DET171
DET171

Posted on • Edited on

Part III: The Member Object in Eris

Part 3

In this post, I will be teaching you how to write a whois command.

The whois command

So, here's the code:

const { Command } = require('yuuko');
const moment = require('moment');
module.exports = new Command(['whois', 'member'], async (message, args, context) => { // eslint-disable-line no-unused-vars
    if (!args[0]) {
        return message.channel.createMessage(`${message.author.mention}, apologies! Please specify a particular member!`);
    }
    const user = message.mentions[0];
    const guild = message.channel.guild;
    const member = await guild.members.get(user.id);
    message.channel.createMessage({
        embed: {
            title: `User information for ${user.username}#${user.discriminator}`,
            thumbnail: {
                url: user.avatarURL,
            },
            color: 0x008000,
            fields: [
                {
                    name: 'Account created at:',
                    value: `${moment.utc(user.createdAt).format('MMMM, Do YYYY, h:mm:ss a')}`,
                    inline: false,
                },
                {
                    name: 'User ID:',
                    value: `\`${user.id}\``,
                    inline: false,
                },
                {
                    name: 'Roles:',
                    value: '<@&' + member.roles.map((r) => `${r}`).join('>, <@&') + '>',
                    inline: false,
                },
                {
                    name: 'Joined server at:',
                    value: `${moment.utc(member.joinedAt).format('MMMM, Do YYYY, h:mm:ss a')}`,
                    inline: false,
                },
            ],
        },
    });
});

Enter fullscreen mode Exit fullscreen mode

Create a file in ./commands, and name it whois.js. Proceed to dump the above code into whois.js. You MIGHT have to run npm i moment --save to install the moment module.

Now, let me explain the code.
As usual, we require the packages, create the command, and export it:

const { Command } = require('yuuko');
const moment = require('moment');
module.exports = new Command('whois', async (message, args, context) => {
  // code here
});
Enter fullscreen mode Exit fullscreen mode

We will then check for arguments. If there are none, we stop the code (or it will return undefined):

if (!args[0]) {
    return message.channel.createMessage(`${message.author.mention}, apologies! Please specify a particular member!`);
}
Enter fullscreen mode Exit fullscreen mode

We use message.author.mention to mention the message author.

We get the first user that is mentioned in the message, get the guild the message was sent in, and get the member object from the guild object:

const user = message.mentions[0];
const guild = message.channel.guild;
const member = await guild.members.get(user.id);
Enter fullscreen mode Exit fullscreen mode

After that, we proceed to send the embed message with the member and user information:

message.channel.createMessage({
        embed: {
            title: `User information for ${user.username}#${user.discriminator}`,
            thumbnail: {
                url: user.avatarURL,
            },
            color: 0x008000,
            fields: [
                {
                    name: 'Account created at:',
                    value: `${moment.utc(user.createdAt).format('MMMM, Do YYYY, h:mm:ss a')}`,
                    inline: false,
                },
                {
                    name: 'User ID:',
                    value: `\`${user.id}\``,
                    inline: false,
                },
                {
                    name: 'Roles:',
                    value: '<@&' + member.roles.map((r) => `${r}`).join('>, <@&') + '>',
                    inline: false,
                },
                {
                    name: 'Joined server at:',
                    value: `${moment.utc(member.joinedAt).format('MMMM, Do YYYY, h:mm:ss a')}`,
                    inline: false,
                },
            ],
        },
    });
Enter fullscreen mode Exit fullscreen mode

However, what if you wanted this command the have two triggers (e.g. whois and member) instead of just one trigger(whois)?
That's quite easy. You just have to replace module.exports = new Command('whois', async (message, args, context) => with module.exports = new Command(['whois', 'member'], async (message, args, context) =>

This are just some user and member properties, more of them can found at the following pages:

Conclusion

In this article, we learnt how to send more advanced embed with fields, create command aliases, and fetch members from the guild objects. In my next post, I will be making a guild command that shows information about the guild the message was sent in.
Have a nice day!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (2)

Collapse
 
bdt4248 profile image
BDT-4248

new Command(['whois', 'member'],...)
What is ['whois', 'member'] supposed to be/do anyway?

Collapse
 
canaris profile image
DET171 • Edited

It's basically adding an alias, so that sending both <prefix>whois and <prefix>member will trigger the command.

SurveyJS custom survey software

JavaScript UI Library for Surveys and Forms

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

View demo

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay