在代码中检查是否有重复调用ButtonInteraction.awaitModalSubmit()的代码,主要是在多个地方初始化模态对话框导致重复注册事件。可以通过全局变量来避免重复调用该函数,如下所示:
// 定义全局变量,表示该函数是否已经被调用过
let modalSubmitted = false;
// 点击按钮触发函数
async function buttonClicked() {
const button = new MessageButton().setLabel('Submit').setStyle('SUCCESS').setID('submit-button');
const row = new MessageActionRow().addComponents([button]);
await interaction.reply({ content: 'Please confirm:', components: [row] });
if (!modalSubmitted) {
// 注册 Submit 按钮点击事件
await interaction.awaitMessageComponent({ componentType: 'BUTTON', time: 10000 }).then(async (buttonInteraction) => {
if (buttonInteraction.customID === 'submit-button') {
modalSubmitted = true;
await buttonInteraction.update({ content: 'Submitted!', components: [] });
// 处理提交操作
handleModalSubmit();
}
}).catch(error => {
console.error(error);
// 处理按钮超时
handleModalTimeout();
});
}
}