小编Max*_*hle的帖子

尝试将事件处理程序放入单独的文件时,Discord.js“无法读取未定义的属性”

所以我一直在开发一个不和谐的机器人。首先,我将每个事件处理程序放入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 { …
Run Code Online (Sandbox Code Playgroud)

javascript event-handling node.js discord discord.js

3
推荐指数
1
解决办法
1690
查看次数