首页 > 解决方案 > 在事件中使用客户端与事件处理程序和 ...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

    },
};

标签: javascriptdiscord.jsargs

解决方案


只需添加client到参数!

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

推荐阅读