首页 > 解决方案 > 如何为命令 discord.js 将多条消息连续串在一起

问题描述

我正在制作一个不和谐的机器人,我想让它做一个小石头剪刀布游戏,你输入命令“rps”,它会询问你是否要选择石头剪刀布。

问题是我当前处理命令的方式是它接收您发送的消息,检查它是否以“>”开头,然后将命令拆分为参数。问题是我真的不知道如何让它做石头剪刀布并检查答案,因为现在它只是运行一个函数,如果命令是我识别的东西。

如何让它检查答案?

标签: javascriptdiscord.js

解决方案


我认为你可以使用.awaitMessages.

这是文档中的链接:[文档] ( https://discordjs.guide/popular-topics/collectors.html ) 和 ( https://discord.js.org/#/docs/main/stable/class/ TextChannel?scrollTo=awaitMessages )

文档中的一个示例:

// Await !vote messages

const filter = m => m.content.startsWith('!vote');

// Errors: ['time'] treats ending because of the time limit as an error

channel.awaitMessages(filter, { max: 4, time: 60000, errors: ['time'] })
  .then(collected => console.log(collected.size))
  .catch(collected => console.log(`After a minute, only ${collected.size} out of 4 voted.`));

2 awaitMessages(awaitReactions 基本上是一样的东西):

const filterOne = m => m.content === 'firstFilter' 

//This is a filter and the 'm' is 'message' I believe. This is a filter and doesn't have to be m.content, but I will put it in this because its a little easier to understand

message.channel.send('This is the first awaitMessage, so type firstFilter').then(() => {
  message.channel.awaitFilter(filterOne, {max: 1, time: 5000, errors: ['time']}) //Max of 1 messages, time in milliseconds and error if time runs out
  .then(collected => {
    //If the user's message passes through the filter
    const filterTwo = m => m.content === 'secondFilter' //Since the user passed the first filter, this is filter 2

    message.channel.send('Passed through filter one, this is filter two').then(() => {
  message.channel.awaitMessages(filterTwo, {max: 1, time: 5000, errors: ['time']})

  .then(collected => {
   //User passes through second filter too
   message.channel.send('Congratulations, you passed through both filters')
   })
   .catch(collected => {
   //User passed through first filter but not the second
   message.channel.send('You did not pass filter 2')
   })
})

   })
})

希望这可以帮助!


推荐阅读