addRole 不是函数

时间:2023-02-11
本文介绍了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 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:显示所有连接的用户 Discord.js 下一篇:坚持将变量添加到 Discord Client 对象 Typescript

相关文章

最新文章