在带有事件处理程序和 ...args | 的事件中使用客户端Discord.js@13

Use client in event with event handler and ...args | Discord.js@13

我想在活动中使用client,但我不知道如何在不删除... args

的情况下交出

事件处理程序:

const client = new Client({ intents: [Intents.FLAGS.GUILDS] });



const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));
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));
    }
}

和事件模块:

module.exports = {
    name: 'interactionCreate',
    async execute(client,interaction) {
       
   //... need client from code before here

    },
};

只需将 client 添加到参数中即可!

if (event.once) {
  client.once(event.name, (...args) => event.execute(client, ...args));
} else {
  client.on(event.name, (...args) => event.execute(client, ...args));
}