Discord机器人报错:getTextInputValue读取未定义属性
Discord机器人命令报错:Cannot read properties of undefined (reading 'getTextInputValue')
我在使用Discord机器人的文本命令时遇到问题,以下是该命令的代码:
const { MessageEmbed, Client, ContextMenuInteraction,MessageActionRow,MessageButton,Formatters } = require("discord.js"); const client = global.client; const craneSchema = require("../schemas/channelSchema"); const { TextInputBuilder, ActionRowBuilder, EmbedBuilder, ButtonBuilder, UserSelectMenuBuilder, ModalBuilder, ButtonStyle, TextInputStyle, ChannelType, TextInputComponent } = require("discord.js"); const raones = require("../schemas/channelSchema"); module.exports = { name:"Yasakla", type: 2, /** * @param {Client} client * @param {ContextMenuInteraction} interaction */ run: async (interaction) => { const bannedMemberIdInput = interaction.fields.getTextInputValue("BANNED_MEMBER_ID"); if(!req.channelId) return interaction.reply({ content: `Bir oda oluşturmalısınız.`, ephemeral: true }); interaction.guild.channels.cache.get(req.channelId).permissionOverwrites.edit(bannedMemberIdInput, { ViewChannel: false, Connect: false }) await craneSchema.findOneAndUpdate({ guildId: interaction.guild.id, channelId: req.channelId }, { $push: { channelBanned: { memberId: bannedMemberIdInput } } }) interaction.reply({ content: `<@${bannedMemberIdInput}> başarıyla kanala banlı olarak eklendi!` }) } };
我不知道问题出在哪里……
问题分析与修复方案
核心报错原因
Cannot read properties of undefined (reading 'getTextInputValue') 是因为你写的是右键菜单命令(type:2),这类命令默认不会关联模态框输入,直接调用interaction.fields.getTextInputValue会导致interaction.fields为undefined,自然无法读取输入值。
其他代码问题
- 未定义
req变量,req.channelId会直接引发新的报错 - 重复引入同一个数据库schema(
craneSchema和raones),属于冗余代码
修复后的完整代码
const { Client, ContextMenuInteraction, ModalBuilder, TextInputBuilder, ActionRowBuilder, TextInputStyle } = require("discord.js"); const client = global.client; const craneSchema = require("../schemas/channelSchema"); module.exports = { name:"Yasakla", type: 2, /** * @param {Client} client * @param {ContextMenuInteraction} interaction */ run: async (interaction) => { // 创建模态框,让用户输入成员ID和目标频道ID const modal = new ModalBuilder() .setCustomId('ban_member_modal') .setTitle('成员频道封禁'); // 成员ID输入框 const memberIdInput = new TextInputBuilder() .setCustomId('BANNED_MEMBER_ID') .setLabel('输入要封禁的成员ID') .setStyle(TextInputStyle.Short) .setRequired(true); // 频道ID输入框 const channelIdInput = new TextInputBuilder() .setCustomId('TARGET_CHANNEL_ID') .setLabel('输入目标频道ID') .setStyle(TextInputStyle.Short) .setRequired(true); // 将输入框添加到模态框行 const firstRow = new ActionRowBuilder().addComponents(memberIdInput); const secondRow = new ActionRowBuilder().addComponents(channelIdInput); modal.addComponents(firstRow, secondRow); // 弹出模态框给用户 await interaction.showModal(modal); // 等待用户提交模态框,设置1分钟超时时间 const submitted = await interaction.awaitModalSubmit({ time: 60000, filter: i => i.user.id === interaction.user.id, }).catch(() => { interaction.reply({ content: '操作超时或已取消', ephemeral: true }); return null; }); if (!submitted) return; // 从提交的模态框中获取输入值 const bannedMemberId = submitted.fields.getTextInputValue('BANNED_MEMBER_ID'); const channelId = submitted.fields.getTextInputValue('TARGET_CHANNEL_ID'); // 验证目标频道是否存在 const targetChannel = interaction.guild.channels.cache.get(channelId); if (!targetChannel) { return submitted.reply({ content: '指定的频道不存在,请检查ID是否正确', ephemeral: true }); } try { // 设置频道权限,封禁指定成员 await targetChannel.permissionOverwrites.edit(bannedMemberId, { ViewChannel: false, Connect: false }); // 更新数据库,若文档不存在则自动创建 await craneSchema.findOneAndUpdate( { guildId: interaction.guild.id, channelId: channelId }, { $push: { channelBanned: { memberId: bannedMemberId } } }, { upsert: true } ); submitted.reply({ content: `<@${bannedMemberId}> 已成功被封禁该频道!`, ephemeral: true }); } catch (error) { console.error(error); submitted.reply({ content: '操作失败,请检查成员ID和频道ID是否合法', ephemeral: true }); } } };
内容的提问来源于stack exchange,提问作者Raonesxd
相关产品推荐
相关产品推荐

