SubTheSandwich
SubTheSandwich

Reputation: 21

Discord bot not running command, erroing out

I'm currently coding a discord bot with a giverank (role) command. The command works until the end, where it does not give you the specified role, and instead errors out. I've tried changing the code, but to no avail. Heres the code

const Discord = require("discord.js");

module.exports = {
  name: "giverank",
  aliases: ["gr"],
  category: "ranks",
    description: "die rank",
    usage: "<input>",
    run: (client, message, args) => {

      if(!message.member.hasPermission("ADMINISTRATOR")) return message.reply("No permission.").then(m => m.delete(5000));

      let rMember = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0]);

      if(!rMember) return message.reply('Could not find that user.');
      let role = args.join(" ").slice(22);

      if(!role) return message.reply('Specify a role.');

      let gRole = message.guild.roles.find(`name`, role);
      if(!gRole) return message.reply('Could not find that role.');

      if(rMember.roles.has(gRole.id));
      await(rMember.addRole(gRole.id));

      message.channel.send(`<@${rMember.id}> has been given the specified role.`).then(m => m.delete(5000));




    }
};

Thanks!

Error: (node:7048) DeprecationWarning: Collection#find: pass a function instead

Upvotes: 0

Views: 37

Answers (2)

Xzandro
Xzandro

Reputation: 977

await(rMember.addRole(gRole.id)); is not how await works. You might want to familiarize with await in general, cause used correctly is very useful.

Regarding your issue there is another thing you did wrong. You currently check if the member has the role and then try to assign it, which doesn't makes sense.

if(!rMember.roles.has(gRole.id));
  rMember.addRole(gRole.id).then(role => {
     message.channel.send(`<@${rMember.id}> has been given the specified role.`).then(m => m.delete(5000));
  });

Upvotes: 1

Denzik
Denzik

Reputation: 316

It seems it's error is coming from here: let gRole = message.guild.roles.find(`name`, role);

https://discord.js.org/#/docs/main/stable/class/Collection?scrollTo=find

Maybe you can try to access the information by using the .get() method?

Hope that helps!

Upvotes: 0

Related Questions