Discord.JS事件处理程序命令

时间:2019-10-26 17:54:20

标签: node.js events fs

好吧,所以我终于弄清楚了如何编码fs事件处理程序以使用events文件夹。现在我只需要了解在自己的文件中对evtns进行编码的基本方法。

这是我现在所拥有的[从ready.js文件开始]。这是正确的吗?如果不正确,如何正确编码事件文件?

module.exports = {
    name: 'avatar',
    description: 'Get the avatar URL of the tagged user(s), or your own avatar.',
    execute(message) {
        client.on("ready", () => {
            client.user.setActivity(`on ${client.guilds.size} servers`);
            console.log(`Ready to serve on ${client.guilds.size} servers, for ${client.users.size} users.`);
        }
}};

这是我的index.js文件供参考:

const fs = require('fs');
const Discord = require('discord.js');
const client = new Discord.Client();
const { prefix, token } = require('./config.json');

client.commands = new Discord.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.name, command);
}

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); 
        }
    });
});

client.login(token);

1 个答案:

答案 0 :(得分:0)

上面的代码块不是ready.js,我认为是它的avatar.js。