javascript - addRole is not a function - Stack Overflow

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

I am creating a Discord Bot. I am trying create a Mute mand, 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 mand.");

        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', ".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?

I am creating a Discord Bot. I am trying create a Mute mand, 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 mand.");

        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?

Share Improve this question edited Dec 2, 2018 at 14:00 Ciaran asked Dec 2, 2018 at 12:51 CiaranCiaran 431 silver badge8 bronze badges 23
  • Have you checked the type of MuteUser and what methods are available? – Happypig375 Commented Dec 2, 2018 at 12:59
  • The only way to give a person a role is .addRole() – Ciaran Commented Dec 2, 2018 at 13:01
  • What if some object that is not a person is assigned to MuteUser? Are you sure it is a person? – Happypig375 Commented Dec 2, 2018 at 13:02
  • When I add msg.channel.send(MuteUser = ${MuteUser}) is MuteUser a person. prntscr./lpo5r3 – Ciaran Commented Dec 2, 2018 at 13:06
  • 4 Dude, if you don't try debugging, then you can't fix any bugs! – Happypig375 Commented Dec 2, 2018 at 13:31
 |  Show 18 more ments

3 Answers 3

Reset to default 2

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/#/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/#/docs/main/master/class/GuildMember

addRole isn't there anymore.

Clicking the type of roles... https://discord.js/#/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 ments 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?

User don't have addRole : https://discord.js/#/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.

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 !!!)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745089925a4610633.html

相关推荐

  • javascript - addRole is not a function - Stack Overflow

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

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信