Skip to content

Commit

Permalink
Add awaitMessage function (PrismarineJS#1865)
Browse files Browse the repository at this point in the history
* Add awaitMessage function

* fix api

* Add regex support (maybe)

* Add typings

* Add docs for regex awaitMessage thing

* Support array of RegExp in typings

* Fix bug

* Made better

* Delete launcher_accounts.json

* add regex tests

* Update chat.js

Co-authored-by: BlueBurgersTDD <[email protected]>
  • Loading branch information
u9g and TheBlueBurger authored May 1, 2021
1 parent feccd02 commit b4e9325
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,20 @@ the event will be called `"chat:name"`, with name being the name passed
* `repeat` - defaults to true, whether to listen for this event after the first match
* `parse` - instead of returning the actual message that was matched, return the capture groups from the regex

#### bot.awaitMessage(...args)

promise that is resolved when one of the messages passed as an arg is resolved

Example:

```js
await bot.awaitMessage('<flatbot> hello world') // resolves on "hello world" in chat by flatbot
await bot.awaitMessage(['<flatbot> hello', '<flatbot> world']) // resolves on "hello" or "world" in chat by flatbot
await bot.awaitMessage(['<flatbot> hello', '<flatbot> world'], ['<flatbot> im', '<flatbot> batman']) //resolves on "hello" or "world" or "im" or "batman" in chat by flatbot
await bot.awaitMessage('<flatbot> hello', '<flatbot> world') // resolves on "hello" or "world" in chat by flatbot
await bot.awaitMessage(/<flatbot> (.+)/) // resolves on first message matching the regex
```

#### bot.setSettings(options)

See the `bot.settings` property.
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ export interface Bot extends TypedEmitter<BotEvents> {
addChatPattern(name: string, pattern: RegExp, options: chatPatternOptions): void;

addChatPatternSet(name: string, patterns: Array<RegExp>, options?: chatPatternOptions): void;

awaitMessage(...args: string | string[] | RegExp | RegExp[]): Promise<string>;
}

export interface chatPatternOptions {
Expand Down
14 changes: 14 additions & 0 deletions lib/plugins/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ function inject (bot, options) {
bot.addChatPattern('whisper', new RegExp(`^\\[${USERNAME_REGEX} -> \\w+\\s?\\] (.*)$`), { deprecated: true })
bot.addChatPattern('chat', new RegExp(`^${USERNAME_REGEX}\\s?[>:\\-»\\]\\)~]+\\s(.*)$`), { deprecated: true })
}

function awaitMessage (...args) {
return new Promise((resolve, reject) => {
const resolveMessages = args.flatMap(x => x)
function messageListener (msg) {
if (resolveMessages.some(x => x instanceof RegExp ? x.test(msg) : msg === x)) {
resolve(msg)
bot.off('messagestr', messageListener)
}
}
bot.on('messagestr', messageListener)
})
}
bot.awaitMessage = awaitMessage
}

function callbackify (f) { // specifically for this function because cb isn't the last parameter
Expand Down
16 changes: 16 additions & 0 deletions test/externalTests/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,21 @@ module.exports = () => {
assert.strictEqual(partTwo, '<U9G> World')
})

addTest('test awaitMessage', async (bot) => {
// let resolves = 0
const p1 = bot.awaitMessage('<flatbot> hello')
bot.chat('hello')
await p1
const p2 = bot.awaitMessage(['<flatbot> hello', '<flatbot> world'])
bot.chat('world')
await p2
const p3 = bot.awaitMessage(/<.+> hello/)
bot.chat('hello')
await p3
const p4 = bot.awaitMessage([/<.+> hello/, /<.+> world/])
bot.chat('world')
await p4
})

return tests
}

0 comments on commit b4e9325

Please sign in to comment.