简体   繁体   中英

Discord mute command received TypeError: fn is not a function

I receive the following error:

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

Code related to the error:

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

And my code if it helps. I've dumbed it down to a simple console.log for testing purposes:

\\ 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))) 

It's because .find() accepts a function as an argument but you provided a string. It returns the first item where the given function returns a truthy value. You could find a member by checking if their id property is the same as args[0] :

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

But you should probably use the .get() method, which is faster, to get a member by their ID:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM