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

Discord.js实现多反应收集器根据不同反应返回对应响应

多表情反应功能实现方案

按照以下逻辑修改现有代码,即可实现多表情差异化响应:

  • 预先维护表情与对应回复的映射表,后续增删反应只需要修改这一处配置
  • 遍历映射表给发送的Embed消息依次添加所有预设反应
  • 调整收集过滤器,匹配所有预设表情、过滤机器人账号的操作
  • 收集到有效反应后,根据用户点击的表情类型返回对应预设回复
修改后完整代码
module.exports = {
    name: 'test',
    description: "ping command",
    async execute(message, args, Discord){
        // 配置反应表情和对应回复,可自行增删
        const reactionConfig = new Map([
            ['🐸', 'You reacted with 🐸!'],
            ['🐱', 'You reacted with 🐱!'],
            ['🐶', 'You reacted with 🐶!']
        ])
        const MAX_REACTIONS = 1;

        const newEmbed = new Discord.MessageEmbed()
        .setColor('#A5775C')
        .setTitle('Reactions')
        .setDescription('*React to this!*')
        
        const sentMessage = await message.channel.send({embeds: [newEmbed]});

        // 依次添加所有配置的反应表情
        for (const emoji of reactionConfig.keys()) {
            await sentMessage.react(emoji);
        }

        // 过滤器匹配所有配置内的表情,排除机器人操作
        const filter = (reaction, user) => reactionConfig.has(reaction.emoji.name) && !user.bot;

        const collector = sentMessage.createReactionCollector({
            filter,
            max: MAX_REACTIONS,
            // 可选:添加有效时长,单位毫秒,例如下方是60秒后停止收集
            // time: 60000
        });

        collector.on('end', (collected, reason) => {
            if (reason === 'limit') {
                const targetReaction = collected.first();
                const replyContent = reactionConfig.get(targetReaction.emoji.name);
                return message.channel.send(replyContent);
            }
            // 可选:超时结束时的提示
            // if (reason === 'time') {
            //     return message.channel.send('Reaction collection timed out!');
            // }
        });
    }
}
扩展说明
  • 如果需要支持服务器自定义表情,把配置里的Unicode表情换成自定义表情的ID,同时过滤器判断逻辑改为匹配reaction.emoji.id即可
  • 调整MAX_REACTIONS的值可以控制最多收集多少次有效反应,设置为1即用户点击一次后就停止收集
  • 取消代码中time字段的注释可以给反应收集设置有效时长,超时后自动结束收集

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 23:27:35