亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Discord.JS 如何等待會員反應

Discord.JS 如何等待會員反應

夢里花落0921 2023-05-11 14:27:39
我正在制作機器人來管理我的多個 Discord 公會。我想制作確認系統,例如:用戶做了 X 件事,Bot 以足夠的渠道發送消息,Bot 等待用戶使用 :thumbdsup: 或 :thumbsdown: 最多 60 秒做出反應如果豎起大拇指,則執行 A,否則 - 執行 B。如果時間到了,則執行 C 動作由于我不知道,我該如何構建這樣的系統。
查看完整描述

1 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

添加和設置事件監聽器


首先,我們首先定義 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



查看完整回答
反對 回復 2023-05-11
  • 1 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號