How do I make a command that only moderators can use?

Great question! In this tutorial, I will be going over how to make commands that only specific users can use.

This is how your code should look after it is all done:

module.exports = {
name: 'ping', // name the command something
description: 'Makes sure the bot is online.', // Describe your command; shows this with the help command
aliases: ['p', 'pong', 'bing', 'bong', 'beep', 'boop'], // Include if you have other names you want to use for this command as well.
usage: '++ping', // Shows how the commmand is used.
inHelp: 'yes', // Necessary so that it displays the information in an Embed when using ++help [command]
execute(message, args) {
if(message.member.roles.cache.has('780941276602302523'))
return message.channel.send(`🏓 Pong!`)
else {message.reply(':x: You do not have permission to use this command!');}
}
};

In this example, I am using the ping command and the ping command is in another folder. If you don’t know how to separate your commands into a separate folder, I recommend checking out our last tutorial and then coming back here.

The only part of this code you need to find important (or that you don’t already recognize) is this:

if(message.member.roles.cache.has('780941276602302523'))
return message.channel.send(`🏓 Pong!`)
else {message.reply(':x: You do not have permission to use this command!');}

The first line is getting a role ID from your server and checking if the member running the command has this role. If it does, it displays Pong! if it does not it displays You do not have permission to use this command!.

That is one way to do it. Another way is as follows:

if(!message*.member.guild.me.*hasPermission(['MANAGE_MEMBERS']))
*return* message*.channel.*send(`🏓 Pong!`)
else {message*.*reply(':x: You do not have permission to use this command!');}

This one checks if a user has the MANAGE_MEMBERS permission instead of checking for a role. That is the only difference between the two.

That’s all that is to it! Let me know if you have any questions in the comments below!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.