Itz Chuckburg
Itz Chuckburg

Reputation: 3

How to string together multiple messages in a row for a command discord.js

I'm making a discord bot and I want to make it do a little rock paper scissors game where you enter the command ">rps" and it asks you whether you want to pick rock paper scissors.

The problem is that the way that I'm currently processing commands is that it takes the message that you sent, checks if it starts with a ">", and then splits the command up into arguments. The problem is that I don't really know how to make it do the rock paper scissors and check for an answer because right now it just runs a function if the command is something that I recognize.

How do I make it check for an answer?

Upvotes: 0

Views: 1627

Answers (1)

Hao C.
Hao C.

Reputation: 320

I think you can use .awaitMessages.

Here's the link in the docs: [The docs] (https://discordjs.guide/popular-topics/collectors.html) and (https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=awaitMessages)

An example from the docs:

// 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 is basically the same thing):

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')
   })
})

   })
})

Hope this helps!

Upvotes: 1

Related Questions