How to transfer client to contextmenucommand

when i refer to code before(which button and slashcommand could do that) but it can't transfer client
const { Events } = require('discord.js');
const client = require('../../app');

module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {
// 处理聊天输入命令
if (interaction.isChatInputCommand()) {
const { commands } = client;
const { commandName } = interaction;
const command = commands.get(commandName);
if (!command) {
console.error(`未发现有关 ${commandName} 的斜杠命令`);
return;
}
try {
await command.execute(interaction,client);
} catch (error) {
console.error(`执行 ${commandName} 斜杠失败`);
console.error(error);
}
}
// 处理按钮交互
else if (interaction.isButton()) {
const { buttons } = client;
const { customId } = interaction;
const button = buttons.get(customId);
if (!button) return new Error('没有找到此按钮对应的处理代码');
try {
await button.execute(interaction, client);
} catch (error) {
console.error(`按钮处理失败: ${customId}`);
console.error(error);
}
}
// 处理上下文菜单命令
else if (interaction.isContextMenuCommand()) {
const { commands } = client;
const { commandName } = interaction;
const command = commands.get(commandName);
if (!command) {
console.error(`未发现有关 ${commandName} 的菜单命令`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(`执行 ${commandName} 菜单失败`);
console.error(error);
}
}
const { Events } = require('discord.js');
const client = require('../../app');

module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {
// 处理聊天输入命令
if (interaction.isChatInputCommand()) {
const { commands } = client;
const { commandName } = interaction;
const command = commands.get(commandName);
if (!command) {
console.error(`未发现有关 ${commandName} 的斜杠命令`);
return;
}
try {
await command.execute(interaction,client);
} catch (error) {
console.error(`执行 ${commandName} 斜杠失败`);
console.error(error);
}
}
// 处理按钮交互
else if (interaction.isButton()) {
const { buttons } = client;
const { customId } = interaction;
const button = buttons.get(customId);
if (!button) return new Error('没有找到此按钮对应的处理代码');
try {
await button.execute(interaction, client);
} catch (error) {
console.error(`按钮处理失败: ${customId}`);
console.error(error);
}
}
// 处理上下文菜单命令
else if (interaction.isContextMenuCommand()) {
const { commands } = client;
const { commandName } = interaction;
const command = commands.get(commandName);
if (!command) {
console.error(`未发现有关 ${commandName} 的菜单命令`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(`执行 ${commandName} 菜单失败`);
console.error(error);
}
}
2 Replies
d.js toolkit
d.js toolkit2w ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
monbrey
monbrey2w ago
I don't know what you mean by "transfer" interaction.client exists

Did you find this page helpful?