javascript - Discord 静音命令收到 TypeError : fn is not a function

标签 javascript node.js discord.js

我收到以下错误:

C:\Users\(private)\discord\bots\(private)\node_modules\@discordjs\collection\dist\index.js:161
            if (fn(val, key, this))
                ^

TypeError: fn is not a function

错误相关代码:

// This is in the discord.js module
 find(fn, thisArg) {
        if (typeof thisArg !== 'undefined')
            fn = fn.bind(thisArg);
        for (const [key, val] of this) {
            if (fn(val, key, this))
                return val;
        }
        return undefined;
    }

如果有帮助,还有我的代码。为了测试目的,我将它简化为一个简单的 console.log:

\\ not everything is here, just the important stuff.
const target = message.guild.members.cache.find(args[0])
console.log(target.roles.forEach(role => console.log(role.id))) 

最佳答案

是因为.find()接受一个函数作为参数,但你提供了一个字符串。它返回给定函数返回真值的第一项。您可以通过检查他们的 id 属性是否与 args[0] 相同来找到成员:

message.guild.members.cache.find((member) => member.id == args[0])

但您可能应该使用 .get()通过 ID 获取成员的方法更快:

const target = message.guild.members.cache.get(args[0])

关于javascript - Discord 静音命令收到 TypeError : fn is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66164622/

相关文章:

javascript - 如何使 chrome 扩展热键适用于所有选项卡?

javascript - 使用javascript获取要在网络上打印的页数

node.js - Express 中的回调与等待性能

node.js - 有没有办法在 RichEmbed 中嵌入超链接?

javascript - 获取自动完成中元素的 ID

javascript - vuejs刷新后无法加载二级路由页面

node.js - Node 检查器未显示我的 Node 应用程序

javascript - 在循环内调用函数会破坏循环

node.js - discord.js - 排行榜/顶部命令显示错误顺序

javascript - Discord Api 必须为客户端提供有效意图