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

Discord.js V14:反应收集器创建频道失败报错求助

问题分析与解决

错误原因

  1. reaction.guild 未定义:Discord.js v14 中,Reaction 对象本身没有 guild 属性,必须通过关联的消息对象(reaction.message.guild)获取服务器信息,直接访问 reaction.guild 会返回 undefined,进而导致读取 channels 属性时抛出错误。
  2. 未定义的 interaction 变量:代码中多处使用 interaction 变量,但在 Reaction Collector 的collect回调上下文里根本不存在这个变量,应该使用回调传入的user参数。

修正后的代码

const { EmbedBuilder, ChannelType, PermissionFlagsBits } = require('discord.js');

module.exports = {
  name: 'channel',
  description: "Create own channel.",
  cooldown: 3000,
  userPerms: [],
  botPerms: [],
  run: async (client, message, args) => {
    const msg = await message.reply('Building embed...');
    const embed = new EmbedBuilder()
      .setColor('#0099ff')
      .setTitle('Click on collector!')
      .setDescription(`Click on the 📩 emoji to create your own channel.`);

    await msg.edit({ content: '\u200B', embeds: [embed] });

    await msg.react('📩');
    const filter = (reaction, user) => reaction.emoji.name === '📩' && !user.bot;
    const collector = msg.createReactionCollector({ filter, time: 60000 });

    collector.on('collect', async (reaction, user) => {
      try {
        await reaction.users.remove(user.id);
        // 使用message.guild确保获取到正确的服务器对象
        await message.guild.channels.create({
            name: user.tag, // 替换为传入的user参数
            type: ChannelType.GuildText,
            permissionOverwrites: [
              {
                id: message.guild.roles.everyone, // 从message.guild获取everyone角色
                deny: [PermissionFlagsBits.ViewChannel],
              },
              {
                id: user.id, // 使用user的ID
                allow: [PermissionFlagsBits.ViewChannel],
              },
              {
                id: client.user.id,
                allow: [PermissionFlagsBits.ViewChannel],
              },
            ],
          });
      } catch (error) {
        console.error(error);
      }
    });

    collector.on('end', async () => {
      await msg.reactions.removeAll();
    });
  }
};

关键修改点

  • 将 reaction.guild 替换为 message.guild(命令触发的消息必然属于某个服务器,所以message.guild一定存在)。
  • 移除所有未定义的 interaction 变量,替换为回调传入的 user 参数。
  • 权限覆盖中的角色和用户ID都改为从正确的上下文获取。

内容的提问来源于stack exchange,提问作者Chewsy Rok

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 21:47:45