检测反应 Discord.js

Posted

技术标签:

【中文标题】检测反应 Discord.js【英文标题】:Detect a reaction Discord.js 【发布时间】:2020-11-08 16:40:41 【问题描述】:

帮助!我正在开发一个 RPG 机器人,它需要检测反应。但是有两个问题:

    它只反应 1 个表情符号而不是 4 个 它只尝试检测一次反应

这是我的代码(有些东西是葡萄牙语):

    if (message.channel.name === "rpg") 
    const HP = 0;
    const atk = 0;
    const def = 0;
    const emoji = message.guild.emojis.cache.find(emoji => emoji.name === 'SurvivalManos');
    message.react(emoji);
    const random = Math.ceil(Math.random() * (3 - 0) + 0);
    const rpgembed = new Discord.MessageEmbed()
    .setColor('#ffd15e')
  .setTitle('RPG')
    .setThumbnail('https://cdn.discordapp.com/attachments/732552438283894824/733256439602086019/New_Project_3.png')
    .setTimestamp()
  .setFooter('Feito por R0dr1G02M4R5 com <3; Desenhos de Kunyk');
  if (random == 1) 
    const HP = 50;
    const atk = 10;
    const def = 5;
    rpgembed.setDescription('Um slime verde selvagem apareceu!').setImage('https://media.discordapp.net/attachments/696805791319064618/733967904890159134/SPOILER_New_Piskel.png?width=585&height=585').addFields(

       name: 'HP', value: HP, inline: true ,
       name: 'Atk', value: atk, inline: true ,
       name: 'Def', value: def, inline: true 
    );
   else if (random == 2) 
    const HP = 100;
    const atk = 5;
    const def = 10;
    rpgembed.setDescription('Um slime vermelho selvagem apareceu!').setImage('https://media.discordapp.net/attachments/696805791319064618/733968480390610955/SPOILER_New_Piskel_1.png?width=585&height=585').addFields(

       name: 'HP', value: HP, inline: true ,
       name: 'Atk', value: atk, inline: true ,
       name: 'Def', value: def, inline: true 
    );
  
  else if (random == 3) 
    const HP = 25;
    const atk = 10;
    const def = 10;
    rpgembed.setDescription('Um feiticeiro fantasma selvagem apareceu!').setImage('https://media.discordapp.net/attachments/696805791319064618/733968482122727494/SPOILER_New_Piskel_2.png?width=585&height=585').addFields(

       name: 'HP', value: HP, inline: true ,
       name: 'Atk', value: atk, inline: true ,
       name: 'Def', value: def, inline: true 
    );
  
message.channel.send(rpgembed).then(function(message) 
message.react('⚔').then(() => message.react('????')).then(() => message.react('????')).then(() => message.react('????'))
const filter = (reaction, user) => 
    return ['⚔', '????', '????', '????'].includes(reaction.emoji.name) && user.id === message.author.id;
;


message.awaitReactions(filter,  max: 1, time: 60000, errors: ['time'] )
    .then(collected => 
    const reaction = collected.first();
    console.log('he logs');
if (Discord.MessageReaction.emoji === '⚔') 
    message.channel.send("He atacs");
    console.log("He ataks");
    act = false;
   else if (Discord.MessageReaction.emoji === '????') 
    message.channel.send("He defens");
    console.log("He defens");
    act = false;
   else if (Discord.MessageReaction.emoji === '????') 
    message.channel.send("He itz");
    console.log("He itz");
    act = false;
   else if (Discord.MessageReaction.emoji === '????') 
    message.channel.send("He rans");
    console.log("He rans");
    act = false;
  
)

);

message.awaitReactions(filter, max: 1, time: 60000, errors: ['time'] ) .then(collected =&gt; const reaction = collected.first();之后的console.log('He logs')中,如果你检查控制台,只有一个“他日志”

请帮忙。谢谢

【问题讨论】:

你为什么将max :1 传递给你的awaitReactions 函数?查看有关 Collectors 和 ReactionCollectors 的 Discord.js 页面。 discordjs.guide/popular-topics/… 【参考方案1】:

它只反应 1 个表情符号而不是 4 个

message.react('⚔');
message.react('?');
message.react('?');
message.react('?');

它只尝试检测一次反应

message.awaitReactions(filter,  max: 1, time: 60000, errors: ['time'] )

传递max: 1 使它只等待1 个反应。更多信息here 和 here

【讨论】:

以上是关于检测反应 Discord.js的主要内容,如果未能解决你的问题,请参考以下文章

如何检测用户是不是放大或缩小反应原生

如何检测组件将使用反应挂钩更新?

使用本机反应检测相机上的活人脸

反应 |如何检测页面刷新 (F5)

如何检测 Angular 2 反应/动态表单的一个元素发生的变化?

如何检测 SKSpriteNode 接触但不反应?