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

Contextmenu模态交互收集器报错:Discord机器人离线问题求助

问题描述

我开发了一款带警告功能的Discord机器人,通过用户上下文菜单命令Warn / Uyarı触发模态框收集信息。但运行一段时间后机器人会离线,并抛出Contextmenu modal interaction collector error。

相关代码如下:

client.on('interactionCreate', async (interaction) => {
  if (interaction.isUserContextMenuCommand()) {
     if (interaction.commandName === 'Warn / Uyarı') {
      const modal = new ModalBuilder()
        .setCustomId('warnUserModal')
        .setTitle('Yetkili Uyarı Formu')
        .setComponents(
        new ActionRowBuilder().setComponents(
            new TextInputBuilder()
              .setLabel('Oyun içi nickname')
              .setCustomId('userid')
              .setStyle(TextInputStyle.Short)
              .setRequired(true)
              .setMaxLength(32)
          )
        );
      await interaction.showModal(modal);
      const modalSubmitInteraction = await interaction.awaitModalSubmit({
        filter: (i) => {
          console.log('Await modal submit');
          console.log(i.fields);
          return true;
        },
        time: 120000,
      }).catch(error => {
        // Catch any Errors that are thrown (e.g. if the awaitModalSubmit times out after 60000 ms)
        console.error(error)
        return null
      })
      const exampleEmbed = new EmbedBuilder()
      .setColor(0x0099FF)
      .setAuthor({ name: 'Uyarı Alan VAR!', iconURL: 'https://cdn.discordapp.com/icons/569642268932309002/a_ebd47f6c9f159d5cd4d0a3ad2804afc2.png', url: 'https://discord.js.org' })
      .setThumbnail('https://cdn-icons-png.flaticon.com/512/3310/3310161.png')
      .addFields(
        { name: `Uyaran Yetkili`, value: `${interaction.user.globalName}\n\n` },
        { name: `Uyarılan`, value: `${interaction.targetMember} / ${interaction.fields.getTextInputValue('userid')}\n**Steam ID**\n*${modalSubmitInteraction.fields.getTextInputValue('email')}*\n**Kaçıncı Uyarı?**\n*${modalSubmitInteraction.fields.getTextInputValue('reason2')}*\n**Uyarı Nedeni**\n*${modalSubmitInteraction.fields.getTextInputValue('reason')}*\n[Ceza kayıt/ss görmek için tıkla](${modalSubmitInteraction.fields.getTextInputValue('reason3')})`, inline: true },
      )
      .setTimestamp()
      .setFooter({ text: 'IMPARATOR GAMING', iconURL: 'https://cdn.discordapp.com/icons/569642268932309002/a_ebd47f6c9f159d5cd4d0a3ad2804afc2.png' });

      modalSubmitInteraction.reply({
        embeds: [exampleEmbed],
        ephemeral: false,
      });
    }
  }
});

报错截图:
Contextmenu模态框收集器错误


解决方案

1. 修复空值引用问题

当awaitModalSubmit超时或出错时,modalSubmitInteraction会被设为null,但后续代码直接调用其属性和方法,会触发空值错误导致机器人崩溃。添加判断逻辑并优化错误提示:

const modalSubmitInteraction = await interaction.awaitModalSubmit({
  filter: (i) => i.user.id === interaction.user.id, // 仅允许触发命令的用户提交,提升安全性
  time: 120000,
}).catch(error => {
  console.error(error);
  interaction.followUp({ content: '模态框提交超时或发生错误,请重新尝试', ephemeral: true });
  return null;
});

// 提前判断,避免空值调用
if (!modalSubmitInteraction) return;

2. 补全模态框缺失的输入组件

代码中的Embed尝试获取email、reason2、reason、reason3字段,但当前模态框仅包含userid输入项,这会导致字段找不到的错误。补全所有必要的输入组件:

const modal = new ModalBuilder()
  .setCustomId('warnUserModal')
  .setTitle('Yetkili Uyarı Formu')
  .setComponents(
    new ActionRowBuilder().setComponents(
      new TextInputBuilder()
        .setLabel('Oyun içi nickname')
        .setCustomId('userid')
        .setStyle(TextInputStyle.Short)
        .setRequired(true)
        .setMaxLength(32)
    ),
    new ActionRowBuilder().setComponents(
      new TextInputBuilder()
        .setLabel('Steam ID')
        .setCustomId('email')
        .setStyle(TextInputStyle.Short)
        .setRequired(true)
    ),
    new ActionRowBuilder().setComponents(
      new TextInputBuilder()
        .setLabel('Kaçıncı Uyarı?')
        .setCustomId('reason2')
        .setStyle(TextInputStyle.Short)
        .setRequired(true)
    ),
    new ActionRowBuilder().setComponents(
      new TextInputBuilder()
        .setLabel('Uyarı Nedeni')
        .setCustomId('reason')
        .setStyle(TextInputStyle.Paragraph)
        .setRequired(true)
    ),
    new ActionRowBuilder().setComponents(
      new TextInputBuilder()
        .setLabel('Ceza kayıt/ss bağlantısı')
        .setCustomId('reason3')
        .setStyle(TextInputStyle.Short)
        .setRequired(true)
    )
  );

3. 添加全局错误捕获

为客户端添加全局错误监听器,防止单个交互错误导致整个机器人离线:

client.on('error', (error) => {
  console.error('客户端全局错误:', error);
});

client.on('shardError', (error) => {
  console.error('分片错误:', error);
});

内容的提问来源于stack exchange,提问作者EFSaNe Geri Döndu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 10:53:21