繁体   English   中英

无法读取未定义的属性(读取“getTextInputValue”)Discord.js

[英]Cannot read properties of undefined (reading 'getTextInputValue') Discord.js

当我在我的不和谐机器人上使用我的命令模式时遇到问题

我不知道是为了什么,但这条线会产生冲突:

const firstActionRow = new MessageActionRow().addComponents(favoriteColorInput);

这是我的命令代码:

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]
      });
    },
  };

我使用: https ://discordjs.guide/interactions/modals.html#building-and-responding-with-modals

我的错误:

TypeError: Cannot read properties of undefined (reading 'getTextInputValue')

感谢您的时间和帮助!

PS:对不起我的英语不好

您的代码当前所做的是显示模态,然后检查您在用户运行斜杠命令时收到的交互是否是模态提交。 相反,您需要使用client.on('interactionCreate')在命令中为模态提交创建一个新的事件侦听器,然后您需要检查交互是否为模态。 这可能会让人难以理解,所以我添加了正确的代码:

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],
      });
    })
  },
};

我对此进行了测试,但这会产生另一个问题:

代码 : -

client.on('interactionCreate', (modalSubmit), async interaction => {

错误 :

/root/bot_adev/commands/modal.js:37
      if (!modalSubmit.isModalSubmit()) return;
      ^

ReferenceError: modalSubmit 未定义

现在整个代码是:

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],
      });
      
    });
  },
};

控制台没有错误,但不和谐也有错误.....

在此处输入图像描述

暂无
暂无

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

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