【问题标题】:Discord.JS How to wait for member reactionDiscord.JS 如何等待会员反应
【发布时间】:2021-01-27 19:45:48
【问题描述】:

我正在制作机器人来管理我的多个 Discord 公会。我想做一个确认系统,比如:

  • 用户做了 X 件事,
  • Bot 在足够的频道中发送消息,
  • Bot 等待用户响应 :thumbdsup: 或 :thumbsdown: 最长 60 秒
  • 如果竖起大拇指,做 A,否则 - 做 B。如果时间到了,做 C 动作

我不知道如何构建这样的系统。

【问题讨论】:

标签: javascript discord discord.js


【解决方案1】:

添加和设置事件监听器

首先,我们首先定义 discord.js 并添加一个事件监听器:

const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot

client.on('message', async message => {  //when a user sends a message

});

然后你需要告诉机器人它在那之后做了什么:

const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot

client.on('message', async message => {  //when a user sends a message
    if (message.author.bot) return; //If a bot sent the message we want to ignore it.

    if (message.content.toLowerCase() === 'what message your looking for' {  //what your looking for (make sure this string is all in lower case)
        //what you want the bot to do after the message you are looking for has been sent
    }
});

现在,如果您希望机器人将反应添加到消息中,您将执行以下操作:

const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot

client.on('message', async message => {  //when a user sends a message
    if (message.author.bot) return; //If a bot sent the message we want to ignore it.

    if (message.content.toLowerCase() === 'what message your looking for' {  //what your looking for (make sure this string is all in lower case)
        await message.react('?'); //reacting to the message with a thumbs up emoji
        await message.react('?'); //reacting to the message with a thumbs down emoji
    }
});

如果您希望机器人回复消息,请使用:

const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot

client.on('message', async message => {  //when a user sends a message
    if (message.author.bot) return; //If a bot sent the message we want to ignore it.

    if (message.content.toLowerCase() === 'what message your looking for' {  //what your looking for (make sure this string is all in lower case)
        message.channel.send('The bots message here') //what you want the bot to reply with
    }
});

等待反应

在这里,这一切都取决于您是要等待对机器人消息或用户消息的反应。
如果您想等待机器人消息的反应,请使用:

const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot

client.on('message', async message => {  //when a user sends a message
    if (message.author.bot) return; //If a bot sent the message we want to ignore it.

    if (message.content.toLowerCase() === 'what message your looking for' {  //what your looking for (make sure this string is all in lower case)
        message = await message.channel.send('The bots message here') //waiting for the message to be sent

        const filter = (reaction, user) => { //filtering the reactions from the user
            return (
            ['?', '?'].includes(reaction.emoji.name) && user.id === message.author.id
            );
        }
        message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] }) //awaiting the reactions - remember the time is in milliseconds
        .then((collected) => {
        const reaction = collected.first();

        if (reaction.emoji.name === '?') { //if the reaction was a thumbs up
            //A action
            reaction.users.remove(message.author.id) //If you wanted to remove the reaction
      } else { //if the reaction was a thumbs down
        //B action
        reaction.users.remove(message.author.id) //If you wanted to remove the reaction
      }
    }).catch((collected) => { //when time is up
      //C action
    });
    }
});


如果您想等待来自用户消息的消息,您将执行相同的操作,除了更改:

if (message.content.toLowerCase() === 'what message your looking for' {  //what your looking for (make sure this string is all in lower case)
    message.channel.send('The bots message here') //sending the message but not awaiting reactions from it

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 2015-03-24
    • 2020-12-03
    • 2021-08-12
    • 2021-03-07
    • 2018-02-02
    • 2020-07-02
    • 2020-11-21
    • 2020-12-05
    相关资源
    最近更新 更多