addRole 不是函数

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

问题描述

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

出了什么问题?

背景资料:

  • Discord.js 版本:12.0.0-dev

  • 使用版本0.5.0-dev的Klasa

代码:

const { 命令 } = require('klasa');const { MessageEmbed } = require('discord.js');module.exports = 类扩展命令 {构造函数(...args){super(...args, { description: '静音用户.' })}异步运行(味精,参数){if(!msg.member.hasPermission("MANAGE_MEMBERS")) return msg.channel.send("你不能使用这个命令.");让 MuteUser = msg.guild.member(msg.mentions.users.first() || msg.guild.members.get(args[0]))if(!MuteUser) return msg.channel.send("找不到用户!");让 MuteReason = msg.content.split(" ").slice(2).join(" ");让 MuteRole = msg.guild.roles.find(r => r.name === "Spammer");if(!MuteRole) return msg.channel.send("找不到垃圾邮件发送者角色!");让 MuteChannel = msg.guild.channels.find(guild => guild.name === 'bot-logs');if(!MuteChannel) return msg.channel.send("找不到#bot-logs 频道.");if(MuteUser.roles.has(MuteRole)) return msg.channel.send("那个用户已经被静音了!");MuteUser.addRole(MuteRole.id);return MuteChannel.send(new MessageEmbed().setAuthor("静音"|| '未知', "http://wolfdevelopment.cf/BotSymbols/info.png").setColor("#ff0000").addField("静音用户", `${MuteUser}`).addField("静音者", `<@${msg.author.id}>`).addField("静音", `${msg.channel}`).addField("时间", `${msg.createdAt}`).addField("原因", `${MuteReason}`));}}

我检查了 MuteUser 是这一行的人:

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

所以一定是人.为什么它没有addRole函数?

解决方案

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

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

稳定版(大概是 11.x):

请注意,addRole 是 Methods 下面的第一项.

现在,切换到 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/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?

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)