这是我的代码:

   async function createTicketModal(interaction, ticketType) {
      if (interaction.deferred || interaction.replied) {
        // Interaction has already been replied to or deferred
        return;
      }
    
      const modal = new ModalBuilder()
        .setCustomId('ticketModal')
        .setTitle(`${ticketType} - Ticket`);
    
      const questionModal = new TextInputBuilder()
        .setCustomId('questionModal')
        .setLabel('Please describe your problem.')
        .setStyle(TextInputStyle.Paragraph);
    
      const firstActionRow = new ActionRowBuilder().addComponents(questionModal);
    
      modal.addComponents(firstActionRow);
    
      const modalMessage = await interaction.showModal(modal);
    
      try {
        const modalResponse = await interaction.awaitModalSubmit({
          filter: (i) =>
            i.customId === "ticketModal" &&
            i.user.id === interaction.user.id,
          time: 60000,
        });
    
        if (modalResponse.isMessageComponent()) {
          const description = modalResponse.values[0];
    
          const channel = await interaction.guild.channels.create(`${interaction.user.username}-ticket`, {
            type: 'text',
            permissionOverwrites: [
              {
                id: interaction.guild.id,
                deny: 'VIEW_CHANNEL',
              },
              {
                id: interaction.user.id,
                allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'ATTACH_FILES'],
              },
            ],
          });
    
          if (!channel.isText()) {
            await modalMessage.editReply({
              content: `Failed to create a text channel for your ${ticketType} ticket.`,
              embeds: [],
              components: [],
            });
            return;
          }
    
          const embed = new EmbedBuilder()
            .setTitle(`${ticketType} Ticket`)
            .setDescription(`Problem Description: ${description}`)
            .setColor('#0099ff')
            .build();
    
          await channel.send(`${interaction.user}`, { embeds: [embed] });
    
          // Close the modal by updating the interaction with an empty array of components
          await modalMessage.editReply({ components: [] });
        }
      } catch (error) {
        console.error(error);
        await modalMessage.editReply({
          content: `An error occurred while creating your ${ticketType} ticket. Please try again later.`,
          embeds: [],
          components: [],
        });
      }
    }      

当您将命令(/ticket)发送到通道时,所有操作都将开始.就在机器人发送带有4个按钮的嵌入消息之后.当我在第一次嵌入时点击一个按钮,它只会发送一个模式,询问‘请描述您的问题’.但问题是,当我提交它时,模式说:

1

注意事项

This question似乎不是复制品,因为它不适合我的案子.

推荐答案

我想这是因为你从来不回复提交的表格.我不认为modalResponse.isMessageComponent()是你需要的,因为你的回答不是MessageComponent,而是ModalSubmit.你需要用modalResponse.isModalSubmit()来代替.

此外,MODEL SUBMIT没有值,所以modalResponse.values不起作用.你应该用modalResponse.fields.要获取描述,您可以使用modalResponse.fields.getTextInputValue('questionModal').

如果您使用的是discord.jsv14,那么您的代码还有其他一些问题.当你创建一个频道时,你应该使用type: ChannelType.GuildText而不是type: 'text'.另外,还有channel name should be included in the options object人.permissionOverwrites也应该使用枚举,比如PermissionFlagsBits.ViewChannel而不是"VIEW_CHANNEL",等等.

另一件事是,interaction.showModal返回空(as I mentioned earlier),所以您不能使用modalMessage.editReply.你可以用modalResponse.reply代替.

虽然我还没有判断过,但这样的东西应该可以用:

const modal = new ModalBuilder()
  .setCustomId('ticketModal')
  .setTitle(`${ticketType} - Ticket`);

const questionModal = new TextInputBuilder()
  .setCustomId('questionModal')
  .setLabel('Please describe your problem.')
  .setStyle(TextInputStyle.Paragraph);

const firstActionRow = new ActionRowBuilder().addComponents(questionModal);

modal.addComponents(firstActionRow);

await interaction.showModal(modal);

try {
  const modalResponse = await interaction.awaitModalSubmit({
    filter: (i) =>
      i.customId === 'ticketModal' && i.user.id === interaction.user.id,
    time: 60000,
  });

  if (modalResponse.isModalSubmit()) {
    const description = modalResponse.fields.getTextInputValue('questionModal');

    const channel = await interaction.guild.channels.create({
      name: `${interaction.user.username}-ticket`,
      type: ChannelType.GuildText,
      permissionOverwrites: [
        {
          id: interaction.guild.id,
          deny: PermissionFlagsBits.ViewChannel,
        },
        {
          id: interaction.user.id,
          allow: [
            PermissionFlagsBits.ViewChannel,
            PermissionFlagsBits.SendMessages,
            PermissionFlagsBits.AttachFiles,
          ],
        },
      ],
    });

    const embed = new EmbedBuilder()
      .setTitle(`${ticketType} Ticket`)
      .setDescription(`Problem Description: ${description}`)
      .setColor('#0099ff');

    await channel.send(`${interaction.user}`, { embeds: [embed] });

    await modalResponse.reply({
      content: `Support channel successfully created. Head over to ${channel}`,
      ephemeral: true,
    });
  }
} catch (error) {
  console.error(error);
  await interaction.reply({
    content: `An error occurred while creating your ${ticketType} ticket. Please try again later.`,
    embeds: [],
    components: [],
  });
}

Javascript相关问答推荐

在JavaScript中,是否有一种方法可以创建自定义thable,在等待某些代码后自动触发它?

禁用从vue.js 2中的循环创建的表的最后td的按钮

对象和数字减法会抵消浏览器js中的数字

Chart.js V4切换图表中的每个条,同时每个条下有不同的标签.怎么做?

Redux查询多个数据库Api reducerPath&

浮动Div的淡出模糊效果

Promise Chain中的第二个useState不更新

如何在输入元素中附加一个属性为checkbox?

编辑文本无响应.onClick(扩展脚本)

在数组中查找重叠对象并仅返回那些重叠对象

在FAQ Accodion应用程序中使用React useState时出现问题

Reaction-SWR-无更新组件

如何防止ionic 输入中的特殊字符.?

传递方法VS泛型对象VS事件特定对象

使用Document.Evaluate() Select 一个包含撇号的HTML元素

如何使本地html页面在重新加载时保持当前可隐藏部分的打开状态?

如何让SVG图标在被点击和访问后改变 colored颜色 ,并在被访问后取消点击时恢复到原来的 colored颜色 ?

如何在TransformControls模式下只保留箭头进行翻译?

为什么这个最小Angular 的Licial.dev设置不起作用?

如何检测当前是否没有按下键盘上的键?