channel.waitForMessage() resolves with the next message that fits, or null when the time is up.
src/commands/capital.ts
Who and what
Waiting for the answer of the person who used the command:
src/commands/nickname.ts
matches that throws ends the wait with its error, thrown where you wait: the person gets a plain answer and you get the error with its line, as for anything else your function does.
Several messages
channel.collectMessages() takes the same options and gathers every message that fits until within passes, or until max came. It resolves with them, in order, maybe none.
src/commands/brainstorm.ts
A click
message.waitForClick() waits for a click on a button or a menu of a message: the message reply() resolves with, for example. The click it gives is answered like in the run of a button, with click.interaction.
src/commands/reset.ts
- A button or a menu that is only waited for needs no
run. Once nothing waits for it any more, a click on it answers the person that it is not available any more. - A click taken by a wait does not run the
runof its button. A click that the wait does not take (another person, withfrom) runs it as usual. click.is(yes)says which one was clicked;click.datais what the button carries,click.valueswhat was picked in a menu.- Without an answer within 2 seconds, the bot changes nothing on screen for you, so the person never sees “This interaction failed”. You can still answer with
click.interaction.followUp()oredit().
A reaction
message.waitForReaction() waits for a reaction added to a message: of a person with from, with one emoji with emoji ('👍', or a custom one by its name).
src/commands/agree.ts
What it needs
Waiting needs nothing to declare: the intents follow your code, like those of your events.- Waiting for messages asks Discord for the messages of servers and of private messages, and for their text: the privileged Message Content Intent. When it is not enabled,
chapterjs devsays so, names your file and links the Developer Portal. - Waiting for a reaction asks for the reactions.
- Waiting for a click needs nothing.
Good to know
- A wait lives while your bot runs: a restart, or
chapterjs devstopping, ends it with nothing (null, or what a collect gathered). A save inchapterjs devkeeps it. - Everything of one server reaches the same process of your bot, so a wait works the same in a large bot.
- The answer to the interaction is still due within 3 seconds: answer first (
reply()), then wait. Waiting before answering is answered for you with “the bot is thinking”, like any slow function.
Testing
In a scenario,bot.use() resolves as soon as your function waits, and what the test does next is what it waits for: bot.say(), bot.click(), bot.select(), bot.react(). Time does not pass in the test server: bot.endWaits() ends every wait as if its time was up.
src/commands/capital.test.ts
Buttons
Buttons with a run of their own, that work for weeks.
Testing
Check what your bot answers, without Discord.