You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js V14获取模态框输入值报错问题求助

问题:Discord.js v14模态框获取用户输入值时报错

错误信息

TypeError: modal.getTextInputValue is not a function
    at Object.execute (C:\Users\rorol\Desktop\Dev\Bot Avocat Stalya\events\CreateTicket.js:64:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

相关代码

const { InteractionType, ActionRowBuilder, Events, ModalBuilder, TextInputBuilder, TextInputStyle, ChannelType, PermissionsBitField } = require("discord.js");
const interactionCreate = require("./interactionCreate");


const chalk = require("chalk")

 module.exports = {
    name: Events.InteractionCreate,
    execute: async(interaction, modal) => {
        
        if(!interaction.isButton()) return;
        
        if(!interaction.customId !== "candidature" ) {

            
            const modal = new ModalBuilder()
            .setCustomId('candidaturemodal')
            .setTitle('Candidature')

            const PrenomInput = new TextInputBuilder()
                .setCustomId('PresentationInput')
                .setLabel('Présente toi IRL :')
                .setStyle(TextInputStyle.Paragraph);
            
            const CandidatureInput = new TextInputBuilder()
                .setCustomId('NomPrenomInput')
                .setLabel('Nom Prénom RP :')
                .setStyle(TextInputStyle.Short);

            const AgeInput = new TextInputBuilder()
                .setCustomId('AgeInput')
                .setLabel('Âge de ton personnage :')
                .setStyle(TextInputStyle.Short);

            const DiplomeInput = new TextInputBuilder()
                .setCustomId('DiplomeInput')
                .setLabel('Diplômes: ')
                .setStyle(TextInputStyle.Short);

            const MotivationInput = new TextInputBuilder()
                .setCustomId('MotivationsInput')
                .setLabel('Motivations :')
                .setStyle(TextInputStyle.Paragraph);


            
            const ActionRow1 = new ActionRowBuilder().addComponents(PrenomInput);
            const ActionRow2 = new ActionRowBuilder().addComponents(CandidatureInput);
            const ActionRow3 = new ActionRowBuilder().addComponents(AgeInput);
            const ActionRow4 = new ActionRowBuilder().addComponents(MotivationInput);
            const ActionRow5 = new ActionRowBuilder().addComponents(DiplomeInput);


            modal.addComponents(ActionRow1, ActionRow2, ActionRow3,ActionRow5, ActionRow4)
            
            await interaction.showModal(modal);


            if (interaction.customId === 'candidaturemodal') {
            await interaction.reply({ content: 'Your submission was received successfully!' });
            }

            const Prenomirl = interaction.fields.getTextInputValue('PrenomInput')
            const NomPrenom = interaction.fields.getTextInputValue('CandidatureInput')
            const Age = interaction.fields.getTextInputValue('AgeInput')
            const Motivation = interaction.fields.getTextInputValue('MotivationInput')
            const Diplomes = interaction.fields.getTextInputValue('DiplomeInput')

            guild.channels.create({
                
                name: `Candidature-${user.interaction.tag}`, 
                type: ChannelType.GuildText,
                permissionOverwrites: [
                    
                    {
                        id: interaction.guild.id,
                        deny: [PermissionsBitField.Flags.ViewChannel],
                    },
                    {
                        id: interaction.user.id,
                        allow: [PermissionsBitField.Flags.ViewChannel],
                    },
                ],
            });
        };

    }
}

解决方案

你的代码存在4个核心问题,修正后即可正常运行:

1. 交互逻辑分离错误

模态框提交是独立的交互事件,不能和按钮交互写在同一段逻辑里。需要分别用interaction.isButton()和interaction.isModalSubmit()区分处理。

2. CustomId不匹配

创建TextInput时设置的customId和后续获取输入时用的ID不一致,比如PrenomInput的customId是PresentationInput,但你用PrenomInput去获取,导致找不到对应值。

3. 变量未定义

代码中使用的guild、user.interaction都是未定义的,需要替换为interaction.guild和interaction.user。

4. 条件判断逻辑错误

if(!interaction.customId !== "candidature")逻辑混乱,应该改为if(interaction.customId === "candidature"),确保只有点击指定按钮时才弹出模态框。


修正后的完整代码

const { InteractionType, ActionRowBuilder, Events, ModalBuilder, TextInputBuilder, TextInputStyle, ChannelType, PermissionsBitField } = require("discord.js");
const chalk = require("chalk")

module.exports = {
    name: Events.InteractionCreate,
    execute: async(interaction) => {
        // 处理按钮交互,弹出模态框
        if(interaction.isButton()) {
            if(interaction.customId === "candidature") {
                const modal = new ModalBuilder()
                    .setCustomId('candidaturemodal')
                    .setTitle('Candidature');

                const PrenomInput = new TextInputBuilder()
                    .setCustomId('PresentationInput')
                    .setLabel('Présente toi IRL :')
                    .setStyle(TextInputStyle.Paragraph);
                
                const CandidatureInput = new TextInputBuilder()
                    .setCustomId('NomPrenomInput')
                    .setLabel('Nom Prénom RP :')
                    .setStyle(TextInputStyle.Short);

                const AgeInput = new TextInputBuilder()
                    .setCustomId('AgeInput')
                    .setLabel('Âge de ton personnage :')
                    .setStyle(TextInputStyle.Short);

                const DiplomeInput = new TextInputBuilder()
                    .setCustomId('DiplomeInput')
                    .setLabel('Diplômes: ')
                    .setStyle(TextInputStyle.Short);

                const MotivationInput = new TextInputBuilder()
                    .setCustomId('MotivationsInput')
                    .setLabel('Motivations :')
                    .setStyle(TextInputStyle.Paragraph);

                const ActionRow1 = new ActionRowBuilder().addComponents(PrenomInput);
                const ActionRow2 = new ActionRowBuilder().addComponents(CandidatureInput);
                const ActionRow3 = new ActionRowBuilder().addComponents(AgeInput);
                const ActionRow4 = new ActionRowBuilder().addComponents(MotivationInput);
                const ActionRow5 = new ActionRowBuilder().addComponents(DiplomeInput);

                modal.addComponents(ActionRow1, ActionRow2, ActionRow3, ActionRow5, ActionRow4);
                await interaction.showModal(modal);
            }
            return;
        }

        // 处理模态框提交交互,获取输入值
        if(interaction.isModalSubmit()) {
            if(interaction.customId === 'candidaturemodal') {
                await interaction.reply({ content: 'Your submission was received successfully!' });

                // 确保customId和创建TextInput时完全一致
                const Prenomirl = interaction.fields.getTextInputValue('PresentationInput');
                const NomPrenom = interaction.fields.getTextInputValue('NomPrenomInput');
                const Age = interaction.fields.getTextInputValue('AgeInput');
                const Motivation = interaction.fields.getTextInputValue('MotivationsInput');
                const Diplomes = interaction.fields.getTextInputValue('DiplomeInput');

                // 创建频道,修复变量未定义问题
                await interaction.guild.channels.create({
                    name: `Candidature-${interaction.user.tag}`, 
                    type: ChannelType.GuildText,
                    permissionOverwrites: [
                        {
                            id: interaction.guild.id,
                            deny: [PermissionsBitField.Flags.ViewChannel],
                        },
                        {
                            id: interaction.user.id,
                            allow: [PermissionsBitField.Flags.ViewChannel],
                        },
                    ],
                });
            }
            return;
        }
    }
}

内容的提问来源于stack exchange,提问作者Romain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 17:01:01