[英]Cannot read properties of undefined (reading 'getTextInputValue') Discord.js
i have a problem when I use my commands modal on my discord bot当我在我的不和谐机器人上使用我的命令模式时遇到问题
I don't know for what but this lines make conflicts :我不知道是为了什么,但这条线会产生冲突:
const firstActionRow = new MessageActionRow().addComponents(favoriteColorInput);
this is the code of my command :这是我的命令代码:
const {
SlashCommandBuilder
} = require('@discordjs/builders');
const { MessageActionRow, Modal, TextInputComponent } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('modal')
.setDescription('Site du serveur'),
async execute(interaction, client) {
// Create the modal
const modal = new Modal()
.setCustomId('myModal')
.setTitle('My Modal');
// Add components to modal
// Create the text input components
const favoriteColorInput = new TextInputComponent()
.setCustomId('favoriteColorInput')
// The label is the prompt the user sees for this input
.setLabel("What's your favorite color?")
// Short means only a single line of text
.setStyle('SHORT');
const hobbiesInput = new TextInputComponent()
.setCustomId('hobbiesInput')
.setLabel("What's some of your favorite hobbies?")
// Paragraph means multiple lines of text.
.setStyle('PARAGRAPH');
// An action row only holds one text input,
// so you need one action row per text input.
const firstActionRow = new MessageActionRow().addComponents(favoriteColorInput);
const secondActionRow = new MessageActionRow().addComponents(hobbiesInput);
// Add inputs to the modal
modal.addComponents(firstActionRow, secondActionRow);
// Show the modal to the user
await interaction.showModal(modal);
if (!interaction.isModalSubmit()) return;
// Get the data entered by the user
const favoriteColor = interaction.fields.getTextInputValue('favoriteColorInput');
const hobbies = interaction.fields.getTextInputValue('hobbiesInput');
console.log({ favoriteColor, hobbies });
const embed = new client.discord.MessageEmbed()
.setColor('#9900ff')
.setTitle(favoriteColor)
.setURL('http://mirano-rp.com')
.setAuthor('Mirano RP ™', 'https://i.imgur.com/uHh71VQ.png', 'http://mirano-rp.com')
.setThumbnail('https://i.imgur.com/uHh71VQ.png')
.setTimestamp()
.setFooter(hobbies, 'https://i.imgur.com/PNK8rlZ.png');
const row = new client.discord.MessageActionRow()
.addComponents(
new client.discord.MessageButton()
.setLabel('Accéder au site !')
.setEmoji('🛒')
.setURL("http://mirano-rp.com")
.setStyle('LINK'),
);
await interaction.reply({
embeds: [embed],
components: [row]
});
},
};
I use : https://discordjs.guide/interactions/modals.html#building-and-responding-with-modals我使用: https ://discordjs.guide/interactions/modals.html#building-and-responding-with-modals
my error :我的错误:
TypeError: Cannot read properties of undefined (reading 'getTextInputValue')
Thanks you for you time and for your help !感谢您的时间和帮助!
PS : Sorry for my bad English PS:对不起我的英语不好
What your code does currently is that it shows the modal, then checks if the interaction which you received when the user ran the slash command was a modal submit.您的代码当前所做的是显示模态,然后检查您在用户运行斜杠命令时收到的交互是否是模态提交。 Instead, you need to create a new event listener for modal submits in the command by using
client.on('interactionCreate')
, then you need to check whether the interaction was a modal or not.相反,您需要使用
client.on('interactionCreate')
在命令中为模态提交创建一个新的事件侦听器,然后您需要检查交互是否为模态。 This might be confusing to understand, so I have added the correct code:这可能会让人难以理解,所以我添加了正确的代码:
const { SlashCommandBuilder } = require("@discordjs/builders");
const { MessageActionRow, Modal, TextInputComponent } = require("discord.js");
module.exports = {
data: new SlashCommandBuilder()
.setName("modal")
.setDescription("Site du serveur"),
async execute(interaction, client) {
// Create the modal
const modal = new Modal().setCustomId("myModal").setTitle("My Modal");
// Add components to modal
// Create the text input components
const favoriteColorInput = new TextInputComponent()
.setCustomId("favoriteColorInput")
// The label is the prompt the user sees for this input
.setLabel("What's your favorite color?")
// Short means only a single line of text
.setStyle("SHORT");
const hobbiesInput = new TextInputComponent()
.setCustomId("hobbiesInput")
.setLabel("What's some of your favorite hobbies?")
// Paragraph means multiple lines of text.
.setStyle("PARAGRAPH");
// An action row only holds one text input,
// so you need one action row per text input.
const firstActionRow = new MessageActionRow().addComponents(
favoriteColorInput
);
const secondActionRow = new MessageActionRow().addComponents(hobbiesInput);
// Add inputs to the modal
modal.addComponents(firstActionRow, secondActionRow);
// Show the modal to the user
await interaction.showModal(modal);
client.on('interactionCreate', (modalSubmit) => {
if (!modalSubmit.isModalSubmit()) return;
// Get the data entered by the user
const favoriteColor = modalSubmit.fields.getTextInputValue("favoriteColorInput");
const hobbies = modalSubmit.fields.getTextInputValue("hobbiesInput");
console.log({ favoriteColor, hobbies });
const embed = new client.discord.MessageEmbed()
.setColor("#9900ff")
.setTitle(favoriteColor)
.setURL("http://mirano-rp.com")
.setAuthor(
"Mirano RP ™",
"https://i.imgur.com/uHh71VQ.png",
"http://mirano-rp.com"
)
.setThumbnail("https://i.imgur.com/uHh71VQ.png")
.setTimestamp()
.setFooter(hobbies, "https://i.imgur.com/PNK8rlZ.png");
const row = new client.discord.MessageActionRow().addComponents(
new client.discord.MessageButton()
.setLabel("Accéder au site !")
.setEmoji("🛒")
.setURL("http://mirano-rp.com")
.setStyle("LINK")
);
await modalSubmit.reply({
embeds: [embed],
components: [row],
});
})
},
};
I test with that but this create another problem:我对此进行了测试,但这会产生另一个问题:
code : -代码 : -
client.on('interactionCreate', (modalSubmit), async interaction => {
error :错误 :
/root/bot_adev/commands/modal.js:37
if (!modalSubmit.isModalSubmit()) return;
^
ReferenceError: modalSubmit is not defined
ReferenceError: modalSubmit 未定义
now the entire code is :现在整个代码是:
const { SlashCommandBuilder } = require("@discordjs/builders");
const { MessageActionRow, Modal, TextInputComponent } = require("discord.js");
module.exports = {
data: new SlashCommandBuilder()
.setName("modal")
.setDescription("Site du serveur"),
async execute(interaction, client) {
// Create the modal
const modal = new Modal().setCustomId("myModal").setTitle("My Modal");
// Add components to modal
// Create the text input components
const favoriteColorInput = new TextInputComponent()
.setCustomId("favoriteColorInput")
// The label is the prompt the user sees for this input
.setLabel("What's your favorite color?")
// Short means only a single line of text
.setStyle("SHORT");
const hobbiesInput = new TextInputComponent()
.setCustomId("hobbiesInput")
.setLabel("What's some of your favorite hobbies?")
// Paragraph means multiple lines of text.
.setStyle("PARAGRAPH");
// An action row only holds one text input,
// so you need one action row per text input.
const firstActionRow = new MessageActionRow().addComponents(
favoriteColorInput
);
const secondActionRow = new MessageActionRow().addComponents(hobbiesInput);
// Add inputs to the modal
modal.addComponents(firstActionRow, secondActionRow);
// Show the modal to the user
await interaction.showModal(modal);
client.on('interactionCreate', async (modalSubmit) => {
if (modalSubmit.isModalSubmit()) return;
// Get the data entered by the user
const favoriteColor = modalSubmit.fields.getTextInputValue("favoriteColorInput");
const hobbies = modalSubmit.fields.getTextInputValue("hobbiesInput");
console.log({ favoriteColor, hobbies });
const embed = new client.discord.MessageEmbed()
.setColor("#9900ff")
.setTitle(favoriteColor)
.setURL("http://mirano-rp.com")
.setAuthor(
"Mirano RP ™",
"https://i.imgur.com/uHh71VQ.png",
"http://mirano-rp.com"
)
.setThumbnail("https://i.imgur.com/uHh71VQ.png")
.setTimestamp()
.setFooter(hobbies, "https://i.imgur.com/PNK8rlZ.png");
const row = new client.discord.MessageActionRow().addComponents(
new client.discord.MessageButton()
.setLabel("Accéder au site !")
.setEmoji("🛒")
.setURL("http://mirano-rp.com")
.setStyle("LINK")
);
await modalSubmit.reply({
embeds: [embed],
components: [row],
});
});
},
};
no error in console but also an error on discord .....控制台没有错误,但不和谐也有错误.....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.