如何获取Discord中对特定消息作出指定反应的用户ID列表
Discord获取指定消息特定反应用户列表实现方案
可以实现,以下是适配discord.js v14版本的代码,可直接套入你提供的框架使用:
前置要求
- 机器人需要拥有
查看频道、读取消息历史、读取消息反应权限 - Discord开发者后台需要为机器人开启
Server Members Intent、Message Content Intent网关意图 - 实例化bot时需要声明对应意图,示例:
const { Client, GatewayIntentBits } = require('discord.js'); const bot = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildMembers ] });
核心实现代码
bot.on("ready", async () => { try { // 获取目标频道 const targetChannel = await bot.channels.fetch('846414975156092979'); if (!targetChannel?.isTextBased()) { console.log('Reaction_users: []'); return; } // 获取目标消息 const targetMessage = await targetChannel.messages.fetch('315274607072378891'); if (!targetMessage) { console.log('Reaction_users: []'); return; } // 匹配✅反应 const targetReaction = targetMessage.reactions.cache.find(item => item.emoji.name === '✅'); if (!targetReaction) { console.log('Reaction_users: []'); return; } // 拉取所有反应用户并提取ID const reactionUsers = await targetReaction.users.fetch(); // 如果需要过滤机器人,加.filter(user => !user.bot)即可 const userIdList = reactionUsers.map(user => user.id); // 按要求格式输出 console.log('Reaction_users: ['); userIdList.forEach(id => console.log(`${id},`)); console.log(']'); } catch (error) { console.log('Reaction_users: []'); console.error('拉取失败:', error); } });
补充说明
如果指定反应是自定义表情,将匹配条件修改为item.emoji.id === '你的自定义表情ID'即可。
内容的提问来源于stack exchange,提问作者MixMaster Gladiator
相关产品推荐
相关产品推荐

