Discord.js 从反应中获取用户**更新**
Posted
技术标签:
【中文标题】Discord.js 从反应中获取用户**更新**【英文标题】:Discord.js Get user from reactions **Updated** 【发布时间】:2021-03-29 12:17:01 【问题描述】:更新
我已经更新了当前为我工作的代码,它现在将创建频道,然后检查频道是否已创建。它只允许 1 个用户创建一个频道,您将无法作为同一用户创建 T5 频道和 T4 频道。
谢谢大家,希望大家圣诞快乐。
Client.on("message", async (message) =>
if (message.author.Client || message.channel.type === "dm") return;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = message.content.substring(message.content.indexOf(" ") + 1);
if (cmd === `$prefixbattle`)
let embed = new Discord.MessageEmbed()
.setTitle("⚔️ 1657 Battles! ⚔️")
.setDescription("React to this message to join the battle.")
.addFields(
name: "????", value: "T5" ,
name: "????", value: "T4" ,
name: "????", value: "T3" ,
name: "????", value: "T2" ,
name: "????", value: "T1"
)
.setColor("#0099ff")
.setTimestamp()
.setFooter("Please pick the troop type you want to battle with!");
let msgEmbed = await message.channel.send(embed);
msgEmbed.react("????"); // T5
msgEmbed.react("????"); // T4
msgEmbed.react("????"); // T3
msgEmbed.react("????"); // T2
msgEmbed.react("????"); // T1
);
Client.on("messageReactionAdd", async (reaction, user, message) =>
//Add an event listener
if (reaction.message.partial) await reaction.message.fetch();
if (user.id === Client.user.id) return; //If the reaction was from the bot, return
if (!reaction.message.guild) return; //If the reaction was not in a guild
const guild = Client.guilds.cache.get("644295524404428832");
if (
guild.channels.cache.find((channel) => channel.name === "t5-battle-channel")
)
return;
if (reaction.emoji.name === "????")
let guild = reaction.message.guild;
guild.channels.create("T5 Battle Channel",
//Creating the channel
type: "text", //Make sure the channel type is text
permissionOverwrites: [
//Set overwrites
id: guild.id,
deny: "VIEW_CHANNEL",
,
id: "788400016736780338",
allow: ["VIEW_CHANNEL"],
,
],
);
if (
guild.channels.cache.find((channel) => channel.name === "t4-battle-channel")
)
return;
if (reaction.emoji.name === "????")
let guild = reaction.message.guild;
guild.channels.create("T4 Battle Channel",
//Creating the channel
type: "text", //Make sure the channel type is text
permissionOverwrites: [
//Set overwrites
id: guild.id,
deny: "VIEW_CHANNEL",
,
id: "788400619114463275",
allow: ["VIEW_CHANNEL"],
,
],
);
if (
guild.channels.cache.find((channel) => channel.name === "t3-battle-channel")
)
return;
if (reaction.emoji.name === "????")
let guild = reaction.message.guild;
guild.channels.create("T3 Battle Channel",
//Creating the channel
type: "text", //Make sure the channel type is text
permissionOverwrites: [
//Set overwrites
id: guild.id,
deny: "VIEW_CHANNEL",
,
id: "788400701130670110",
allow: ["VIEW_CHANNEL"],
,
],
);
if (
guild.channels.cache.find((channel) => channel.name === "t2-battle-channel")
)
return;
if (reaction.emoji.name === "????")
let guild = reaction.message.guild;
guild.channels.create("T2 Battle Channel",
//Creating the channel
type: "text", //Make sure the channel type is text
permissionOverwrites: [
//Set overwrites
id: guild.id,
deny: "VIEW_CHANNEL",
,
id: "788400738727624704",
allow: ["VIEW_CHANNEL"],
,
],
);
if (
guild.channels.cache.find((channel) => channel.name === "t1-battle-channel")
)
return;
if (reaction.emoji.name === "????")
let guild = reaction.message.guild;
guild.channels.create("T1 Battle Channel",
//Creating the channel
type: "text", //Make sure the channel type is text
permissionOverwrites: [
//Set overwrites
id: guild.id,
deny: "VIEW_CHANNEL",
,
id: "788400784420372490",
allow: ["VIEW_CHANNEL"],
,
],
);
);
【问题讨论】:
【参考方案1】:您应该使用reaction collector
。
这些工作与消息收集器非常相似,除了你 将它们应用于消息而不是通道。以下是一个 取自文档的示例,变量稍好一些 用于澄清的名称。过滤器将检查 ? 表情符号 - in 特别是默认肤色,所以要小心。它还将 检查做出反应的人是否与作者共享相同的 id 收集器被分配到的原始消息。
const filter = (reaction, user) =>
return reaction.emoji.name === '?' && user.id === message.author.id;
;
const collector = message.createReactionCollector(filter, time: 15000 );
collector.on('collect', (reaction, user) =>
console.log(`Collected $reaction.emoji.name from $user.tag`);
);
collector.on('end', collected =>
console.log(`Collected $collected.size items`);
);
阅读它的文档here。
我还想知道是否可以将每个对特定角色做出反应的用户随机配对。
collected
属性对于这项任务应该很方便。
至于向GuildMember
添加角色/角色,请使用add
方法。了解如何将角色添加到成员 here。
【讨论】:
【参考方案2】:如果您想获得消息的所有反应,如果您已经拥有消息对象,则可以使用message
上的reactions
属性。这将返回一个您可以使用的ReactionManager。
【讨论】:
感谢您的帮助以上是关于Discord.js 从反应中获取用户**更新**的主要内容,如果未能解决你的问题,请参考以下文章
discord.js 如何从 fetchMessage 事件中获取“反应用户”列表?