【问题标题】:discord.js modal giving error something went wrongdiscord.js 模态给出错误出错了
【发布时间】:2022-12-03 10:19:48
【问题描述】:

每当我提交模态时,它都会给我错误,出了点问题再试一次。

这是我的代码 -

const { Events, EmbedBuilder, AttachmentBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder, ButtonBuilder, ButtonStyle, InteractionType} = require('discord.js');
const { Verification } = require('../models/verificationSchema')
const { Captcha } = require('captcha-canvas')

module.exports = {
    name: Events.InteractionCreate,
    async execute(interaction, client) {
        if (interaction.isButton()){
            if (interaction.customId === 'verify') {
                await interaction.deferReply({ephemeral: true});
                const member = await interaction.guild.members.cache.get(interaction.member.user.id) || await interaction.guild.members.fetch(interaction.member.user.id).catch(err => {});

                    const captcha = new Captcha();
                    captcha.async = true;
                    captcha.addDecoy();
                    captcha.drawTrace();
                    captcha.drawCaptcha();

                    const captchaAnswer = captcha.text;

                    const captchaImage = new AttachmentBuilder()
                    .setFile(await captcha.png)
                    .setName('captcha.png')
                
                    const captchaEmbed = new EmbedBuilder()
                    .setTitle('Verification Captcha')
                    .setColor('Yellow')
                    .setImage('attachment://captcha.png')
                    .setDescription(`Please enter the captcha text`)

                    const captchaRow = new ActionRowBuilder()
                    .addComponents([
                        new ButtonBuilder()
                        .setLabel('Answer')
                        .setCustomId('answer')
                        .setStyle(ButtonStyle.Success)
                    ])

                    await interaction.editReply({embeds: [captchaEmbed], files: [captchaImage], components: [captchaRow]});

                }
            }
            if (interaction.customId === 'answer') {
                const modal = new ModalBuilder()
                    .setCustomId('verificationModal')
                    .setTitle('Verification Input')
                    .addComponents([
                        new ActionRowBuilder().addComponents([
                            new TextInputBuilder()
                            .setCustomId('captchaInput')
                            .setLabel("Enter the Captcha.")
                            .setStyle(TextInputStyle.Short),
                        ])
                    ]);
                interaction.showModal(modal);
            }
            if (interaction.isModalSubmit()) {
                console.log(interaction)
                if (interaction.customId === 'verificationModel') {
                  const response = interaction.fields.getTextInputValue('captchaInput');
                    console.log(response)
                }
            }                                    
    }
}

我正在尝试在 discord 中发出验证命令,我通过模式向用户询问验证码文本,但它给了我错误。我不知道如何解决这个错误我只想在提交模态时让用户在模态中输入..

终端没有错误。 提前致谢 :) error image

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    模态显示是因为 Discord 期望对模态交互做出响应。如果您不想在用户提交模式时向用户发送消息(例如,通过使用interaction.reply()),那么您可以使用ModalSubmitInteraction.deferUpdate() 方法简单地推迟对交互的更新。例子:

    if (interaction.isModalSubmit()) {
        interaction.deferUpdate()
        // all the other stuff you want to do with the modal submission here
    }
    

    这里应该发生的是,当用户点击你的模态上的提交按钮时,Discord 将他们的提交发送到你的机器人,你回应说你只是想推迟交互,然后 Discord 为用户关闭模态而不显示错误消息.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-02
      • 2018-06-12
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      相关资源
      最近更新 更多