Skip to content

Commit

Permalink
Add removeChatPattern (PrismarineJS#1884)
Browse files Browse the repository at this point in the history
* add removeChatPattern

* (hopefully) add removeChatPattern test

* fix biggest issue ever

* Add API docs for removeChatPattern

* Removed removeChatPatternSet from typings

* remove removeChatPatternSet from API

* Fully remove removeChatPatternSet
  • Loading branch information
TheBlueBurger authored May 14, 2021
1 parent 1ed880e commit 1f6b0aa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,11 @@ 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.removeChatPattern(name)

removes a chat pattern
* `name` the name of the chat pattern

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

promise that is resolved when one of the messages passed as an arg is resolved
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ export interface Bot extends TypedEmitter<BotEvents> {

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

removeChatPattern(name: string): void;

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

Expand Down
6 changes: 5 additions & 1 deletion lib/plugins/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function inject (bot, options) {

const ChatMessage = require('prismarine-chat')(bot.version)
// chat.pattern.type will emit an event for bot.on() of the same type, eg chatType = whisper will trigger bot.on('whisper')
const _chatRegex = []
let _chatRegex = []
// deprecated
bot.chatAddPattern = (patternValue, typeValue) => {
bot.addChatPattern(typeValue, patternValue, { deprecated: true })
Expand Down Expand Up @@ -40,6 +40,10 @@ function inject (bot, options) {
})
}

bot.removeChatPattern = (name) => {
_chatRegex = _chatRegex.filter(pattern => pattern.name !== name)
}

function findMatchingPatterns (msg) {
const found = []
for (const [ix, { patterns, position }] of _chatRegex.entries()) {
Expand Down
13 changes: 13 additions & 0 deletions test/externalTests/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ module.exports = () => {
assert.strictEqual(partTwo, '<U9G> World')
})

addTest('test removeChatPattern', async (bot) => {
await once(bot, 'message') // => starting chat test removeChatPattern
bot.addChatPattern('test', /<.+> Hello/)
bot.removeChatPattern('test')
let triggered = false
const listener = () => { triggered = true }
bot.once('chat:test', listener)
bot.chat('/tellraw @p {"translate":"chat.type.text", "with":["U9G", "Hello"]}')
await once(bot, 'message')
assert.ok(triggered === false)
bot.off('chat:test', listener)
})

addTest('test awaitMessage', async (bot) => {
// let resolves = 0
const p1 = bot.awaitMessage('<flatbot> hello')
Expand Down

0 comments on commit 1f6b0aa

Please sign in to comment.