Discord.js取消模态会导致崩溃

huangapple go评论68阅读模式
英文:

Discord.js cancellation of modal cause crashes

问题

我使用以下方法来等待执行斜杠命令后的模态回复。

        await interaction.showModal(submissionValidateModal);

        const modalReply = await interaction.awaitModalSubmit({
            time: 60000,
            filter: i => i.user.id === interaction.user.id,
        }).catch(error => {
            console.log(error)
            return null;
        })

        // 处理事务

        modalReply.reply({ embeds: [requestedSentEmbed], ephemeral: true });

然而,如果用户执行命令,机器人将等待模态提交事件。即使用户在这种情况下点击取消,await也不会被取消。当用户快速再次执行命令并提交模态时,最终会导致错误:Interaction has already been acknowledged。我认为这是因为有多个收集器等待模态提交,当模态被提交时,将导致机器人尝试多次回复。有人对这个问题有线索吗?

英文:

I use the following method to wait for the modal reply after executing the slash command.

        await interaction.showModal(submissionValidateModal);

        const modalReply = await interaction.awaitModalSubmit({
            time: 60000,
            filter: i => i.user.id === interaction.user.id,
        }).catch(error => {
            console.log(error)
            return null;
        })

        // Handling Stuff

        modalReply.reply({ embeds: [requestedSentEmbed], ephemeral: true });

However, if the user execute the command, and the bot will be waiting for the modal submit event. The await would not be cancelled even if the user click cancel in this situation. When the user quickly execute the command again, and submit with the modal, it will eventually causing the error: Interaction has already been acknowledged. I believe this happens because there is more than 1 collector waiting for the modal submit, when the modal is submitted, it will cause the bot attempting to reply a more than once.

Anyone have clues on this issue?

答案1

得分: 1

基于DJS文档

您需要使用.then方法。由于awaitModalSubmit已经确认了命令。

// 收集模态提交交互
const filter = (interaction) => interaction.customId === 'modal';
interaction.awaitModalSubmit({ filter, time: 15_000 })
  .then(interaction => console.log(`${interaction.customId}已提交!`))
  .catch(console.error);
英文:

Based off of DJS Documentation

You need the .then method. Since awaitModalSubmit already acknowledges the command

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Collect a modal submit interaction
const filter = (interaction) =&gt; interaction.customId === &#39;modal&#39;;
interaction.awaitModalSubmit({ filter, time: 15_000 })
  .then(interaction =&gt; console.log(`${interaction.customId} was submitted!`))
  .catch(console.error);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月9日 11:55:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定