Команды обработчика событий Discord.JS - PullRequest
0 голосов
/ 26 октября 2019

Хорошо, так что я наконец-то понял, как кодировать мой обработчик событий fs для использования папки событий. теперь мне просто нужно знать основной метод кодирования 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);

...