0 event-handling discord discord.js
好吧。所以我做了一个事件处理程序。
const fs = require('fs');
fs.readdir('./events/', (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
const eventFunction = require(`./events/${file}`);
if (eventFunction.disabled) return;
const event = eventFunction.event || file.split('.')[0];
const emitter = (typeof eventFunction.emitter === 'string' ? client[eventFunction.emitter] : eventFunction.emitter) || client;
const once = eventFunction.once;
try {
emitter[once ? 'once' : 'on'](event, (...args) => eventFunction.run(...args));
}
catch (error) {
console.error(error.stack);
}
});
});
Run Code Online (Sandbox Code Playgroud)
但我不确定应该在文件夹的文件中放入什么。我基本上想做一个自动回复文件。我添加了,但没有显示任何反应。
client.on('message', message => {
if ((message.content === `<@${client.user.id}> <3` || message.content === `<@!${client.user.id}> <3`)) {
message.channel.send('<3');
}
});
Run Code Online (Sandbox Code Playgroud)
那么,我应该在文件中放入什么内容才能使机器人正常工作? 谢谢。
当您复制代码时,您应该始终尝试理解它。这是教程中的原始代码和注释:
const fs = require('fs'); // fs is the package we need to read all files which are in folders
fs.readdir('./events/', (err, files) => { // We use the method readdir to read what is in the events folder
if (err) return console.error(err); // If there is an error during the process to read all contents of the ./events folder, throw an error in the console
files.forEach(file => {
const eventFunction = require(`./events/${file}`); // Here we require the event file of the events folder
if (eventFunction.disabled) return; // Check if the eventFunction is disabled. If yes return without any error
const event = eventFunction.event || file.split('.')[0]; // Get the exact name of the event from the eventFunction variable. If it's not given, the code just uses the name of the file as name of the event
const emitter = (typeof eventFunction.emitter === 'string' ? client[eventFunction.emitter] : eventFunction.emitter) || client; // Here we define our emitter. This is in our case the client (the bot)
const once = eventFunction.once; // A simple variable which returns if the event should run once
// Try catch block to throw an error if the code in try{} doesn't work
try {
emitter[once ? 'once' : 'on'](event, (...args) => eventFunction.run(...args)); // Run the event using the above defined emitter (client)
} catch (error) {
console.error(error.stack); // If there is an error, console log the error stack message
}
});
});
Run Code Online (Sandbox Code Playgroud)
文件夹中的每个文件events都需要作为 JavaScript 文件,因此我们知道它应该导出一个对象。该对象被存储到eventFunction变量中,并查看几个属性:
event属性是它正在侦听的事件,如果未指定,它将默认为文件名。emitter属性是发出事件的属性,默认为bot,这就是我们想要的。once属性表示事件是否应该只触发一次,这不是我们想要的。run属性包含实际将在事件上触发的函数。现在我们知道我们需要/不需要哪些属性,我们可以构建一个文件放入文件夹中events:
// export the object so it can be required
module.exports = {
// we want a message event
event: "message",
// we want it to trigger multiple times
once: false,
// the actual function
run(message) {
if (message.content === "foo") {
message.channel.send("you said foo");
}
}
};
Run Code Online (Sandbox Code Playgroud)
或者,如果您将文件命名为message.js,您也可以删除该event: "message"行,因为它会默认为该行。