discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

Bug: TypeError: fn is not a function #6676

Closed xMaxximum closed 3 years ago

xMaxximum commented 3 years ago

Issue description

this code is executed on messageCreate. I was unable to figure out when exactly the program terminates but this is the error stack:

C:\code\js\kciw-remake\node_modules\@discordjs\collection\dist\index.js:191
            return fn(value, key, this);
                   ^

TypeError: fn is not a function
    at C:\code\js\kciw-remake\node_modules\@discordjs\collection\dist\index.js:191:20
    at Function.from (<anonymous>)
    at Map.map (C:\code\js\kciw-remake\node_modules\@discordjs\collection\dist\index.js:189:22)
    at Object.execute (C:\code\js\kciw-remake\events\messageCreate.js:18:40)
    at Client.<anonymous> (C:\code\js\kciw-remake\index.js:42:45)
    at Client.emit (node:events:394:28)
    at MessageCreateAction.handle (C:\code\js\kciw-remake\node_modules\discord.js\src\client\actions\MessageCreate.js:23:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\code\js\kciw-remake\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\code\js\kciw-remake\node_modules\discord.js\src\client\websocket\WebSocketManager.js:350:31)
    at WebSocketShard.onPacket (C:\code\js\kciw-remake\node_modules\discord.js\src\client\websocket\WebSocketShard.js:443:22)

the code at at Object.execute (C:\code\js\kciw-remake\events\messageCreate.js:18:40) is this: const afks = message.mentions.users.map();

the code at at Client.<anonymous> (C:\code\js\kciw-remake\index.js:42:45) is this: client.on(event.name, (...args) => event.execute(...args, client)); <-- this is executing normally so the problem isn't there

Codesample

const Discord = require('discord.js');
const db = require('quick.db');

module.exports = {
name: 'messageCreate',
async execute(message, client) {
if(message.mentions.users.map(i => i.id).length > 0) {
            const afks = message.mentions.users.map();
            const embedList = [];
            const afkList = [];

            afks.forEach(i => {
                afkList.push(db.get(`afk.${i.id}`));
            });

            afks.forEach(afkFun);

            function afkFun(value, index) {
                if(Object.keys(db.get('afk').includes(value.id))) {
                    embedList.push(
                        new Discord.MessageEmbed().setTitle(`${value.username} is AFK!`)
                            .setDescription(
                                `Reason: ${afkList[index].reason}\n<t:${Math.round(afkList[index].time / 1000)}:R>`,
                            )
                            .setColor(color.red),
                    );
                }
                db.has(`afk.${value.id}.pings`)
                    ? db.push(`afk.${value.id}.pings`, {
                        time: Date.now(),
                        user: message.author.id,
                        link: message.url,
                        channel: message.channel.id,
                    })
                    : db.set(`afk.${value.id}.pings`, {
                        time: Date.now(),
                        user: message.author.id,
                        link: message.url,
                        channel: message.channel.id,
                    });

                message.channel.send({ embeds: embedList });
            }}
            const pingEmbed = new Discord.MessageEmbed().setDescription(
                db.has(`afk.${message.author.id}.pings`)
                    ? db
                        .get(`afk.${message.author.id}.pings`)
                        .map(
                            i =>
                                `[<t:${Math.round(i.time / 1000)}:R>](${i.link}) <@${
                                    i.user
                                }> - <#${i.channel}>`,
                        )
                        .join('\n')
                    : 'You have no pings!',
            );
            message.channel.send({
                embeds: [mainEmbed, pingEmbed],
            });
            db.delete(`afk.${message.author.id}`);
        }
},
};

discord.js version

13.1.0

Node.js version

16.9.1

Operating system

Windows

Priority this issue should have

Medium (should be fixed soon)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

GUILDS, GUILD_MEMBERS, GUILD_WEBHOOKS, GUILD_PRESENCES, GUILD_MESSAGES, DIRECT_MESSAGES

I have tested this issue on a development release

discord.js@13.2.0-dev.1632312194.92f6471

Jiralite commented 3 years ago

Your error is because you called .map() with no arguments. This is not a discord.js bug - you used the method wrong.


If you're unsure about something, you should ask in the Discord server or create a new discussion as this issue tracker is only for bug reports and enhancement suggestions.