本文介绍了addRole不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建Discord Bot.我正在尝试创建一个静音"命令,但是我总是遇到相同的错误.

出了什么问题?

背景信息:

  • Discord.js版本: 12.0.0-dev

  • 使用版本为 0.5.0-dev 的Klasa

代码:

  const {命令} = require('klasa');const {MessageEmbed} = require('discord.js');module.exports =类扩展命令{构造函数(... args){super(... args,{描述:'将用户静音.'})}异步运行(msg,args){if(!msg.member.hasPermission("MANAGE_MEMBERS"))返回msg.channel.send(您无法使用此命令.");让MuteUser = msg.guild.member(msg.mentions.users.first()|| msg.guild.members.get(args [0]))if(!MuteUser)返回msg.channel.send(找不到用户!");让MuteReason = msg.content.split(").slice(2).join(");让MuteRole = msg.guild.roles.find(r => r.name ===垃圾邮件发送者");if(!MuteRole)返回msg.channel.send(找不到垃圾邮件发送者角色!");让MuteChannel = msg.guild.channels.find(guild => guild.name ==='bot-logs');if(!MuteChannel)返回msg.channel.send(找不到#bot-logs频道.");if(MuteUser.roles.has(MuteRole))返回msg.channel.send(该用户已被静音!.");MuteUser.addRole(MuteRole.id);返回MuteChannel.send(new MessageEmbed().setAuthor(静音" ||'未知',"http://wolfdevelopment.cf/BotSymbols/info.png").setColor(#ff0000").addField(静音用户",`$ {MuteUser}`).addField(静音者",`< @ $ {msg.author.id}>`).addField(静音于",`$ {msg.channel}`).addField("Time",`$ {msg.createdAt}`).addField("Reason",`$ {MuteReason}`)));}} 

我已检查 MuteUser 是此行中的人物:

  if(!MuteUser)返回msg.channel.send(找不到用户!"); 

因此它必须是一个人.为什么没有 addRole 函数?

解决方案

我决定从另一个角度看待这个问题,并在Discord.js文档中搜索了更多信息.果然找到了一些东西:

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

稳定(大概是11.x):

请注意, addRole 是方法"下面的第一项.

现在,切换到master(又名Development分支-从那里获得12.0.0-dev)...

addRole 不再存在.

点击角色的类型... add 是第一种方法.

您可能可以将 MuteUser.addRole 替换为 MuteUser.roles.add .

注意:这不会使我在注释中的任何单词无效,因为您没有在问题本身中提供足够的信息,以了解引发错误时 MuteUser 是哪种类型.

注2:这仅进行了一次Google搜索.您什至投入了多少工作?

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?

解决方案

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/GuildMemberRoleStoreadd 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?

这篇关于addRole不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 16:20