Discord.js "cannot read properties of undefined" 试图将事件处理程序放入单独的文件时

Discord.js "cannot read properties of undefined" when trying to put event handler into a seperate file

所以我一直在研究一个不和谐的机器人。起初,我将每个事件处理程序放入 index.js,效果非常好。

const { Client, Collection, Intents } = require('discord.js');
const { token } = require('./config.json');
const fs = require('fs');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS],
partials: ['MESSAGE', 'CHANNEL', 'REACTION'], 
});

client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.data.name, command);
}

client.once('ready', () => {
    console.log('Ready!');
});

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    const command = client.commands.get(interaction.commandName);

    if (!command) return;

    try {
        await command.execute(interaction);
    }
    catch (err) {
        console.log(err);
        await interaction.reply('There was an error while executing this command!');
    }
});

client.on('messageReactionAdd', (reaction, user) => {
    const channel = client.channels.cache.find(channel => channel.name === "test");
    let message = 874736592542105640;
    let emotes = [ "kannathinking", ""];
    let roleID = (reaction.emoji.name == emotes[0] ? "874730080486686730" : "874729987310235738")

    if (message == reaction.message.id && (emotes[0] == reaction.emoji.name || emotes[1] == reaction.emoji.name)) {
    channel.send(`${user} was given the <@&${roleID}> role`);
    }
});

client.on('messageReactionRemove', (reaction, user) => {
    const channel = client.channels.cache.find(channel => channel.name === "test");
    let message = 874736592542105640;
    let emotes = [ "kannathinking", ""];
    let roleID = (reaction.emoji.name == emotes[0] ? "874730080486686730" : "874729987310235738")

    if (message == reaction.message.id && (emotes[0] == reaction.emoji.name || emotes[1] == reaction.emoji.name)) {
    channel.send(`${user} was removed from the <@&${roleID}> role`);
    }
});

client.login(token);

然后我尝试将事件处理程序存储在单独的文件中,就像我对命令所做的那样。

const { Client, Collection, Intents } = require('discord.js');
const { token } = require('./config.json');
const fs = require('fs');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS],
partials: ['MESSAGE', 'CHANNEL', 'REACTION'], 
});

client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('js'));
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.data.name, command);
}

for (const file of eventFiles) {
    const event = require(`./events/${file}`);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args));
    } else {
        client.on(event.name, (...args) => event.execute(...args));
    }
}
client.login(token);

然而,这并不奏效。当我启动机器人时,它给了我积极的反馈。然而,当我试图对我的不和谐服务器上的消息做出反应时,它抛出了以下错误:

TypeError: cannot read properties of undefined (reading 'cache')

messageReactionAdd.js 事件文件如下所示:

module.exports = {
    name: 'messageReactionAdd',
    execute(client, reaction, user) {
        const channel = client.channels.cache.find(channel => channel.name === "test");
        let message = 874736592542105640;
        let emotes = ["kannathinking", ""];
        let roleID = (reaction.emoji.name == emotes[0] ? "874730080486686730" : "874729987310235738")

        if (message == reaction.message.id && (emotes[0] == reaction.emoji.name || emotes[1] == reaction.emoji.name)) {
            channel.send(`${user} was given the <@&${roleID}> role`);
        }
    }
}

我尝试通过要求我在 index.js 中创建的客户端对象以及要求来自 discord.js 的“客户端”来修复错误。两者都不起作用,而且我无法弄清楚事件文件中缺少什么才能起作用。

当需要事件文件时,您从未定义客户端,因此当您在事件处理程序中调用 client.channels 时,它实际上并不知道 client 是什么。

要解决此问题,请在执行函数时在 args 之前定义 client。
示例:

for (const file of eventFiles) {
    const event = require(`./events/${file}`);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(client, ...args));
    } else {
        client.on(event.name, (...args) => event.execute(client, ...args));
    }
}