我在 discord.js 中制作了一個 Discord 機器人,并在單個檔案中設定了斜杠命令,這樣我就不會迷失在代碼中。
但是其中一個命令中有一個選擇選單,但是由于選擇選單本身就是一種互動,所以它不知道要查找哪個檔案才能找到它的執行。
我使用以下代碼來檢測命令
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'Hmm, we appear to have run into an error. Please try again in a few minutes, if this error persists, please let us know!', ephemeral: true });
}
});
你可以對選擇選單做同樣的事情
client.on('interactionCreate', async interaction => {
if (!interaction.isSelectMenu()) return;
if (interaction.customId === 'helpMenu') {
await interaction.update('Worked!');
}
});
但是由于我的命令在他們自己的檔案夾中,所以這不起作用。
我嘗試將回應互動的代碼移動到我的主檔案中,但它無法識別 customId,因為該檔案中未定義選擇選單。
我該怎么做?還是我錯過了一些非常明顯的東西?
提前致謝,
干杯
uj5u.com熱心網友回復:
您可以執行此操作的一種方法是將main.js檔案中創建的客戶端作為引數傳遞給您在其中創建MessageSelectMenu. 然后,您可以為MessageSelectMenu. 但是您必須檢查用戶 ID 是否與運行斜杠命令的用戶 ID 相同,并且還要檢查公會 ID 是否相同。一個例子是這樣的:
interaction.reply({ components: [row] }); // Send the MessageSelectMenu
client.on("interactionCreate", (click) => {
if (
click.user.id !== interaction.user.id || // Check if the user who clicked the MessageSelectMenu was the same as the one who ran the slash command
click.guildId !== interaction.guildId || // Check if the guild id where the user clicked the MessageSelectMenu was the same as the one where the slash command was run
!click.isSelectMenu() // Check if the interaction was a MessageSelectMenu click
)
return;
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/488168.html
標籤:javascript 不和谐.js
上一篇:從另一個檔案呼叫時JSON為空
下一篇:如何通過處理java減慢精靈影片
