Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Discord.js how to make an announcement command

Writer Matthew Barrera

I'm trying to make an announcement command for my discord bot running discord.js. For example, if I say +ann (#CHANNEL) (ANNOUNCEMENT) it will post it there. I don't have any code for this and have been looking for ages. May you please help?

3

2 Answers

Basic Command Setup

When making a basic command, the first thing you're gonna want to set up is something like this:

<Discord.Client instance here>.on("message", message => { if(message.author.bot) return; let messageArray = message.content.split(" "); let command = messageArray[0]; let args = messageArray.slice(1);
}

What does this do?

First of all, the code inside those curly braces is called whenever the bot "receives" a message. This means that the code is run whenever a message is sent in any channel in any server it is a part of, including DMs. The first line in this function ensures that the user who sent the message was not a bot, as you don't want other bots triggering commands on your bot.

Then, this code does some simple splitting on the message, so that the messageArray is an array of each word in the message, as determined by a space. For example, this would mean that if I sent Hello there, Cloudy Sounds!, messageArray would contain the following:

['Hello', 'there,', 'Cloudy', 'Sounds!']

command is the first word of this message (in this case 'Hello'). This is important because when sends a command to the bot, such as the +ann command you wanted above, the standard format of a message is

<prefix><command> <arguments>

So, later on in your code, you can use command to get both the prefix and the command in one string, allowing you to see if someone wants the bot to do something, or if they're just chatting with a friend and want nothing to do with the bot.

Finally, args is an array of the rest of the words, in this case ['there,', 'Cloudy', 'Sounds!'].

By using the 3 of these variables in combination, you can easily extract all the important information out of a message.

Getting The Channel

Since the user's command will contain a mentioned channel, the first thing we're gonna want to do is get that channel as a Discord.js Channel Class, so we can later send messages to it.

To get this, we can store the result of message.mentions.channels() into channel. Since we're going to be mentioning a chanel in a guild, we're gonna want to make sure it's not a DM first, and that the command actually starts with our prefix, in this case '+'. So now, our code looks like this:

<Discord.Client instance here>.on("message", message => { if(message.author.bot) return; let messageArray = message.content.split(" "); let command = messageArray[0]; let args = messageArray.slice(1); if(message.channel.type === "dm") return; if(!message.content.startsWith('+')) return; if(command === '+ann') { let channel = message.mentions.channels(); }
}

Extracting the Announcement

Now that we have out channel, we're gonna want to get our announcement out from the message. We'll do this using args.

So, how do we do this?

First of all, we know that args[1] is going to be the mention of the channel ('#channel'), so that's not a part of the command. Everything else, however, is a part of our announcement, so we combine that all into a string using the following:

let announcement = args.slice(1).join(" ");

Putting it all together

Now that we have our announcement and channel, we can finally send it.

The final code would be:

<Discord.Client instance here>.on("message", message => { if(message.author.bot) return; let messageArray = message.content.split(" "); let command = messageArray[0]; let args = messageArray.slice(1); if(message.channel.type === "dm") return; if(!message.content.startsWith('+')) return; if(command === '+ann') { let channel = message.mentions.channels(); let announcement = args.slice(1).join(" "); channel.send(announcement); }
}

Welcome to Stack Overflow, and let me know if you have any problems with this answer, or need further clarification!

Try this:

.on("message", message => {
if(message.author.bot) return;
let messageArray = message.content.split(" ");
let command = messageArray[0];
let args = messageArray.slice(1);
if(message.channel.type === "dm") return;
if(!message.content.startsWith('+')) return;
if(command === '+ann') { let channel = message.mentions.channels(); let announcement = args.slice(1).join(" "); channel.send(announcement);
}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy