addRole 不是函数

addRole is not a function

我正在创建一个 Discord 机器人。 我正在尝试创建静音命令,但我总是遇到同样的错误。

出了什么问题?

背景资料:

代码:

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}`));
    }
}

我查过MuteUser是这一行的人:

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

所以一定是一个人。为什么它没有 addRole 功能?

我决定从另一个角度来看这个问题,并搜索了 Discord.js 文档以获取更多信息。果然有发现:

我假设您对 msg.guild.member 的调用会导致 GuildMember,因为这就是名称所暗示的。

稳定(大概11.x):https://discord.js.org/#/docs/main/stable/class/GuildMember

请注意 addRole 是方法下方的第一项。

现在,切换到 master(又名 Development 分支 - 你从那里获得 12.0.0-dev)... https://discord.js.org/#/docs/main/master/class/GuildMember

addRole 已经不存在了。

正在单击 roles 的类型... https://discord.js.org/#/docs/main/master/class/GuildMemberRoleStore add是第一种方法。

你或许可以用 MuteUser.roles.add 替换 MuteUser.addRole

注意:这不会使我在评论中的任何文字无效,因为您没有在问题本身中提供足够的信息来说明抛出错误时 MuteUser 是什么类型。

注 2:这只进行了一次 Google 搜索。您甚至在研究上投入了多少工作?

用户没有 addRole : https://discord.js.org/#/docs/main/stable/class/User 但是 GuildMembers 确实如此。

您是否尝试转换为您定义 "let MuteUser" 的行中的成员?如果它是用户,它没有 addRole 方法可能是正常的。

首先:用户没有角色。只有成员才有角色。

用户 是不和谐的用户 = 现实生活中的人(或机器人)

A Member 对象是服务器的用户 "attached"。所以一个成员可以有角色,因为你只在特定的服务器中有角色。

我也试过 someMember.addRole(someRole),得到了同样的 addRole is not a function,错误信息。

只需尝试 someMember.roles.add(someRole) 并且 它有效!!!

假设我有:

  1. guildID: 服务器ID
  2. userID:用户ID
  3. roleID:角色ID
  4. memberID:没有!!!哈哈,懂你!!!成员在 Discord 上没有 ID。会员由公会ID和用户ID定义,但没有自己的ID。

因此,如果我想为成员添加角色,我会这样做:

// 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
   });

如果这不起作用:

  1. 检查成员的最高角色是否比你的机器人的最高角色更强大
  2. 检查您的机器人尝试添加的角色是否没有您的机器人的最高角色强大
  3. 另外:在你的服务器配置的角色配置页面中,在角色列表中,确保你试图归属的角色列在你的机器人角色下(您可以拖动角色重新排序)。 (我花了好几个小时才弄明白!!!)