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
相关产品推荐
相关产品推荐

