所以我一直在做一个不和聊天机器人。首先,我将每个事件处理程序放入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 property 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中创建的客户端对象来修复错误,以及要求" client "从discord.js。两者都没有工作,我无法找出事件文件中缺少什么,以便它工作。
当需要事件文件时,您从未定义过客户端,因此当您在事件处理程序中调用client.channels
时,它实际上不知道client
是什么。
要解决这个问题,在执行函数时,在参数之前定义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));
}
}