DEV Community

Ty
Ty

Posted on

Making Discord Bot Commands

Oh boy! Another week has passed and I learned a lot more about discord bots in general. Discord.js makes everything so simple to run and use!

Today, I want to write about just coding a simple discord command. This would just output Hello world!

So first we would start on how to actually get what messages are being sent on your discord server

Right under my previous code where I showed you how to turn on your discord bot, we're gonna add a code to listen for any messages that may start with !hi

But first lets get the basics up on how to actually get messages from your discord server

Under your client.login

We're going to add
client.on('message', message => {
})};

Inside this code block is how we'll be getting our messages!

So we want the only the message content, not any other thing. Inside the code block lets check for the message to say !hi
client.on('message', message => {
if (message.content === `!hi`)
})};

Now we're checking if the message is exactly !hi
Now lets output something back!

client.on('message', message => {
if (message.content === `!hi`) {
message.channel.send('Greetings.');
}
})};

Message.channel.send basically is your bot outputting message into that discord server channel and only that channel.

It should look something like this at the end

Top comments (0)