简体   繁体   English

InteractionAlreadyReplied Error while sending Modal, discord.js v14

[英]InteractionAlreadyReplied Error while sending a Modal, discord.js v14

I am developing a bot using discord.js v14, so I was making a modal command but I found an error that I can't solve.我正在使用discord.js v14 开发一个机器人,所以我正在制作一个模态命令,但我发现了一个我无法解决的错误。 Here is the code that I am using.这是我正在使用的代码。

modal.js:模态.js:

const BOT = require("../../../handlers/Client");
const {
    ApplicationCommandType,
    Client,
    CommandInteraction,
    ModalBuilder,
    ActionRowBuilder,
    TextInputBuilder,
    TextInputStyle
} = require("discord.js")

module.exports = {
  name: "modal",
  description: `ModalTest`,
  userPermissions: [],
  botPermissions: [],
  category: "Information",
  cooldown: 10,
  type: ApplicationCommandType.ChatInput,
  /**
   *
   * @param {BOT} client
   * @param {CommandInteraction} interaction
   * @param {String[]} args
   */
  run: async (client, interaction, args) => {


    const myModal = new ModalBuilder()
    .setCustomId("modal_1")
    .setTitle(`Modal Teste`);

    const username = new TextInputBuilder()
    .setCustomId("username")
    .setLabel("Username:")
    .setStyle(2)
    .setPlaceholder("Coloque seu nome")
    .setRequired(true)
    .setMinLength(4)
    .setMaxLength(10);

    const password = new TextInputBuilder()
    .setCustomId("password")
    .setLabel("Password:")
    .setStyle(2)
    .setPlaceholder("Coloque sua senha")
    .setRequired(true)
    .setMinLength(4)
    .setMaxLength(10);

    const username_row = new ActionRowBuilder().addComponents(username);
    const password_row = new ActionRowBuilder().addComponents(password);

    myModal.addComponents(username_row, password_row);

    await interaction.showModal(myModal);



    }
 }

events/modal.js:事件/modal.js:

const { InteractionType } = require("discord.js");
const client = require("../index");

client.on("interactionCreate", async (interaction) => {
    if (interaction.type === InteractionType.ModalSubmit) {
        // code
        if (interaction.customId === "modal_1") {

            const username = interaction.fields.getTextInputValue("username");
            const password = interacrion.fields.getTextInputValue("username");

            if(username && password){
                await interaction.reply({
                    content: `Sua submissão foi enviada. \n\n UserName: ${username} \n Password: ${password}`,
                    ephemeral: true,
                })
            }
        }
    }
})

Here is the error code:这是错误代码:

[Error_Handling] :: Unhandled Rejection/Catch
Error [InteractionAlreadyReplied]: The reply to this interaction has already been sent or deferred.
    at ChatInputCommandInteraction.showModal (C:\Users\Rapha\Downloads\discord js procjet\PROJETINHO DIVINO QUE EU VOU TERMINAR EM NOME DE JESUS AMÉM\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:252:46)
    at Object.run (C:\Users\Rapha\Downloads\discord js procjet\PROJETINHO DIVINO QUE EU VOU TERMINAR EM NOME DE JESUS AMÉM\Commands\Slash\Information\modal.js:56:23)
    at BOT.<anonymous> (C:\Users\Rapha\Downloads\discord js procjet\PROJETINHO DIVINO QUE EU VOU TERMINAR EM NOME DE JESUS AMÉM\events\interactionCreate.js:51:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'InteractionAlreadyReplied'
} Promise {
  <rejected> Error [InteractionAlreadyReplied]: The reply to this interaction has already been sent or deferred.
      at ChatInputCommandInteraction.showModal (C:\Users\Rapha\Downloads\discord js procjet\PROJETINHO DIVINO QUE EU VOU TERMINAR EM NOME DE JESUS AMÉM\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:252:46)
      at Object.run (C:\Users\Rapha\Downloads\discord js procjet\PROJETINHO DIVINO QUE EU VOU TERMINAR EM NOME DE JESUS AMÉM\Commands\Slash\Information\modal.js:56:23)
      at BOT.<anonymous> (C:\Users\Rapha\Downloads\discord js procjet\PROJETINHO DIVINO QUE EU VOU TERMINAR EM NOME DE JESUS AMÉM\events\interactionCreate.js:51:13)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
    code: 'InteractionAlreadyReplied'
  }
}

interactionCreate.js:交互创建.js:

const { ApplicationCommandOptionType } = require("discord.js");
const client = require("..");
const { cooldown } = require("../handlers/functions");
const { emoji } = require("../settings/config");

client.on("interactionCreate", async (interaction) => {
  // Slash Command Handling
  if (interaction.isChatInputCommand()) {
    await interaction.deferReply({ ephemeral: true }).catch((e) => {});
    const cmd = client.commands.get(interaction.commandName);
    if (!cmd)
      return client.embed(
        interaction,
        `${emoji.ERROR} \`${interaction.commandName}\` Command Not Found `
      );
    const args = [];
    for (let option of interaction.options.data) {
      if (option.type === ApplicationCommandOptionType.Subcommand) {
        if (option.name) args.push(option.name);
        option.options?.forEach((x) => {
          if (x.value) args.push(x.value);
        });
      } else if (option.value) args.push(option.value);
    }
    interaction.member = interaction.guild.members.cache.get(
      interaction.user.id
    );
    if (cmd) {
      // checking user perms
      if (!interaction.member.permissions.has(cmd.userPermissions || [])) {
        return client.embed(
          interaction,
          `You Don't Have \`${cmd.userPermissions}\` Permission to Use \`${cmd.name}\` Command!!`
        );
      } else if (
        !interaction.guild.members.me.permissions.has(cmd.botPermissions || [])
      ) {
        return client.embed(
          interaction,
          `I Don't Have \`${cmd.botPermissions}\` Permission to Use \`${cmd.name}\` Command!!`
        );
      } else if (cooldown(interaction, cmd)) {
        return client.embed(
          interaction,
          ` You are On Cooldown , wait \`${cooldown(
            interaction,
            cmd
          ).toFixed()}\` Seconds`
        );
      } else {
        cmd.run(client, interaction, args);
      }
    }
  }

  // Context Menu Handling
  if (interaction.isContextMenuCommand()) {
    await interaction.deferReply({ ephemeral: true }).catch((e) => {});
    const command = client.commands.get(interaction.commandName);
    if (command) command.run(client, interaction);
  }
});

I tried to followup the interaction but that didn't solve it, and I don't know what to do now我尝试跟进互动但没有解决问题,我现在不知道该怎么办

Since you want to show a modal, the truth is that you can't use deferReply() if you need to use showModal() as well (as far as I know).由于您想显示模态,事实是,如果您还需要使用showModal() (据我所知),则不能使用deferReply() )。 Since you used deferReply() in your interactionCreate.js file, this error popped out.由于您在 interactionCreate.js 文件中使用deferReply() ,因此会弹出此错误。 So to fix it, all you need to do is remove that deferReply() and your code should work fine所以要修复它,您需要做的就是删除deferReply()并且您的代码应该可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM