addRole is not a function

跟風遠走 提交于 2020-12-06 04:35:52

问题


I am creating a Discord Bot. I am trying create a Mute command, but I always get the same error.

What went wrong?

Background information:

  • Discord.js version: 12.0.0-dev

  • Klasa with version 0.5.0-dev is used

Code:

const { Command } = require('klasa');
const { MessageEmbed } = require('discord.js');

module.exports = class extends Command {

    constructor(...args) {
        super(...args, { description: 'Mute an user.' })
    }

    async run(msg, args) {
        if(!msg.member.hasPermission("MANAGE_MEMBERS")) return msg.channel.send("You can't use this command.");

        let MuteUser = msg.guild.member(msg.mentions.users.first() || msg.guild.members.get(args[0]))
        if(!MuteUser) return msg.channel.send("Can't find user!");

        let MuteReason = msg.content.split(" ").slice(2).join(" ");

        let MuteRole = msg.guild.roles.find(r => r.name === "Spammer");
        if(!MuteRole) return msg.channel.send("Can't find the Spammer role!");

        let MuteChannel = msg.guild.channels.find(guild => guild.name === 'bot-logs');
        if(!MuteChannel) return msg.channel.send("Can't find the #bot-logs channel.");

        if(MuteUser.roles.has(MuteRole)) return msg.channel.send("That user is already muted!.");

        MuteUser.addRole(MuteRole.id);

        return MuteChannel.send(new MessageEmbed()
            .setAuthor("Mute"|| 'Unknown', "http://wolfdevelopment.cf/BotSymbols/info.png")
            .setColor("#ff0000")
            .addField("Muted User", `${MuteUser}`)
            .addField("Muted By", `<@${msg.author.id}>`)
            .addField("Muted In", `${msg.channel}`)
            .addField("Time", `${msg.createdAt}`)
            .addField("Reason", `${MuteReason}`));
    }
}

I have checked that MuteUser is a person in this line:

    if(!MuteUser) return msg.channel.send("Can't find user!");

So it must be a person. Why doesn't it have an addRole function?


回答1:


I decided to look at this from another viewpoint and searched the Discord.js documentation for some more information. Sure enough, something is found:

I assume your call to msg.guild.member would result in a GuildMember because that is what the name implies.

Stable (Presumably 11.x): https://discord.js.org/#/docs/main/stable/class/GuildMember

Note that addRole is the first item below Methods.

Now, switching to master (aka Development branch - where you got 12.0.0-dev from)... https://discord.js.org/#/docs/main/master/class/GuildMember

addRole isn't there anymore.

Clicking the type of roles... https://discord.js.org/#/docs/main/master/class/GuildMemberRoleStore add is the first method.

You can probably replace MuteUser.addRole with MuteUser.roles.add.

Note: This does not invalidate any of my words in the comments because you didn't provide enough information in the question itself on what type MuteUser is when the error was thrown.

Note 2: This took one Google search only. How much work did you even put into research?




回答2:


User don't have addRole : https://discord.js.org/#/docs/main/stable/class/User But GuildMembers does.

Are you trying to cast to a member in the line where you defined "let MuteUser" ? It could be normal that it does not have the addRole methods if it' a user.




回答3:


First: users don't have a role. Only members have roles.

Users are a discord user = a real life person (or a bot)

A Member object is a user "attached" to a server. So a member can have roles, because you only have roles within a specific server.

I tried someMember.addRole(someRole) too, and got the same addRole is not a function, error message.

Just try someMember.roles.add(someRole) and it works!!!

Providing I have:

  1. guildID: the server ID
  2. userID: the user ID
  3. roleID: the role ID
  4. memberID: NO !!! haha, got you!!! Members don't have IDs on Discord. A member is defined by a guildID and a userID, but it does not have an ID of it's own.

So, if I want to add a role to a member, here is what I do:

// get the guild
const guild = client.guilds.cache.get(guildID);
if (! guild) return console.error("404: guild with ID", guildID, "not found");

// get the member
const member = guild.members.cache.get(userID);
if (! member) return console.error("404: user with ID", userID, "not found in guild", guild.name);

// we check our bot has role management permission
if (! guild.me.hasPermission('MANAGE_ROLES'))
    return console.error("I don't have the right to manage roles !!!  :-( ");

// get the role
const role = guild.roles.cache.get(roleID);
if (! role) return console.error("404: role with ID", roleID, "not found in guild", guild.name);

// add role to member
member.roles.add(role)
   .then(function() {
        console.log("role", role.name, "added to", member.user.name, "on server", guild.name);
        // do your stuff
   })
   .catch(function(err) {
        console.error("could not assign role", role.name,
            "to", member.user.name,
            "on server", guild.name,
            "because", error.message);
        // cry
   });

If that does not work:

  1. Check that the highest role of the member is less powerfull than the highest role of your bot
  2. Check that the role your bot is trying to add, is less powerfull than the highest role of your bot
  3. And also: in the role configuration page of your server configuration, in the role list, make sure the role you are trying to attribute, is listed under the role of your bot (you can drag the roles to re-order them). (that one took me hours to figure out !!!)


来源:https://stackoverflow.com/questions/53580392/addrole-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!