Agastonarii
Agastonarii

Reputation: 35

How can I fix my code so that the Discord command functions?

I am building a bot for Discord, and wanted to make a command to simplify governing spam. I have put the bot's role above all other roles, however, the general bot role is near the bottom. I am looking to create a command that will perform this function by running /spamhammer @user. However, the command does nothing, and no errors are shown in the console.

My code:

client.on("message", msg => {
  if (msg.content === "/spamhammer ") {
    if (message.member.hasPermission('MANAGE_ROLES')) {
    let rMember = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0])); 
    if (!rMember) return message.reply("That user does not exist.");
    let gRole = message.guild.roles.find('name', 'SpamHammer'); 

    rMember.removeRoles(rMember.roles).then(console.log).catch(console.error);
    rMember.addRole(gRole.id);


    message.channel.send("User got the SpamHammer :spamhammer:."); 

    
  } else {
    message.channel.send("You do not have permission to use this command.");
  }
}});

The desired outcome is for all roles to be stripped from the user, and to automatically have the SpamHammer role applied. Thanks!

Upvotes: 0

Views: 39

Answers (1)

WoJo
WoJo

Reputation: 516

You are using msg at the top, while using message in the rest of your code. Change all occurrences of message to msg, or vice versa.


You may also have to do this:


You have to change

msg.content === "/spamhammer"

to

msg.content.startsWith("/spamhammer")

Right now you are checking if a user sends this exact message:

/spamhammer

while you want the user to send

/spamhammer @user

By changing the above, you check if the message starts with "/spamhammer" rather than checking if the message is exactly /spamhammer.

Then after changing it, you can check if the message has arguments etc. etc.

Upvotes: 1

Related Questions