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

discord.js v13调用message.createReactionCollector提示不是函数怎么解决?

错误原因
  • 你代码中传入的message参数并非普通消息实例,从打印日志的type: 'APPLICATION_COMMAND'可以判断,它是斜杠命令对应的CommandInteraction交互实例,该类本身没有createReactionCollector方法,只有Message类实例才支持该方法。
  • 你需要监听的是你发送的规则embed消息的反应,而非用户触发命令的交互对象的反应,且原有过滤逻辑错误,仅允许触发命令的管理员触发身份组分配逻辑,不符合实际需求。
  • 参数类型注释错误,实际传入的是CommandInteraction而非MessageReaction。
修复后的完整代码
const { MessageEmbed, CommandInteraction } = require('discord.js');

module.exports = {
    name: 'rules',
    description: 'Server Rules',
    permission: "ADMINISTRATOR",
    /**
     * 
     * @param {CommandInteraction} interaction 
     */
    async execute(interaction) {
        const ruleEmbed = new MessageEmbed()
        .setColor('#ffc7fe')
        .setTitle('Server Rules')
        .setDescription('Please click the check mark ✅ to verfiy that you read the rules and gain access to other channels')
        .setFooter('Abusing loop holes will result in fair punishment ')
        .addFields(
            {name: '#1', value:'...',},
        );
        
        // 发送规则embed,拿到发送后的消息实例
        const sentMessage = await interaction.channel.send({ embeds: [ruleEmbed] });
        await sentMessage.react("✅");

        // 过滤逻辑:仅匹配✅表情,且触发者不是机器人
        const filter = (reaction, user) => {
            return reaction.emoji.name === '✅' && !user.bot;
        };
        
        // 给发送的规则消息绑定收集器,按需调整time参数,永久有效就不要加time
        const collector = sentMessage.createReactionCollector({ filter, time: 15000 });
        
        collector.on('collect', async(reaction, user) => {
            const verifiedRole = await interaction.guild.roles.fetch("916861450281156678");
            const unverifiedRole = "916929706182459392";
            
            const member = await interaction.guild.members.fetch(user.id);
            await member.roles.add(verifiedRole);
            await member.roles.remove(unverifiedRole);
        });

        // 给命令发起者回复,避免交互超时
        await interaction.reply({ content: "规则已发送", ephemeral: true });
    }
}
额外注意事项
  • 如果需要反应收集器永久生效,不需要限制15秒的话,删除createReactionCollector配置中的time: 15000即可。
  • 请确保你的机器人启动时已经注册了GUILD_MESSAGE_REACTIONS意图,否则无法收到反应事件。
  • 请确保机器人的身份组权限高于要分配的两个身份组,且拥有Manage Roles权限,否则会出现角色分配失败的报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 15:54:05