feat(dispatcher): filtering for service messages, by chat type and for text-only msgs

This commit is contained in:
teidesu 2021-05-10 17:30:05 +03:00
parent 9df635ad92
commit db0d5f51e4
2 changed files with 43 additions and 7 deletions

View file

@ -164,6 +164,13 @@ export class Message {
return this.raw.out! return this.raw.out!
} }
/**
* Whether this message is a service message
*/
get isService(): boolean {
return this.raw._ === 'messageService'
}
/** /**
* Multiple media messages with the same grouped ID * Multiple media messages with the same grouped ID
* indicate an album or media group * indicate an album or media group

View file

@ -14,17 +14,19 @@ import {
RawDocument, RawDocument,
Sticker, Sticker,
TelegramClient, TelegramClient,
User, Venue, User,
Venue,
Video, Video,
Voice, Voice,
Poll, Poll,
Invoice, Invoice,
Game, Game,
WebPage WebPage,
} from '@mtcute/client' } from '@mtcute/client'
import { MaybeArray } from '@mtcute/core' import { MaybeArray } from '@mtcute/core'
import { ChatMemberUpdate } from './updates' import { ChatMemberUpdate } from './updates'
import { ChosenInlineResult } from './updates/chosen-inline-result' import { ChosenInlineResult } from './updates/chosen-inline-result'
import { MessageAction } from '@mtcute/client/src/types/messages/message-action'
/** /**
* Type describing a primitive filter, which is a function taking some `Base` * Type describing a primitive filter, which is a function taking some `Base`
@ -294,12 +296,12 @@ export namespace filters {
msg.sender instanceof User && msg.sender.isBot msg.sender instanceof User && msg.sender.isBot
/** /**
* Filter messages sent in broadcast channels * Filter messages by chat type
*/ */
export const channel: UpdateFilter< export const chat = <T extends Chat.Type>(
Message, type: T
{ chat: Modify<Chat, { type: 'channel' }> } ): UpdateFilter<Message, { chat: Modify<Chat, { type: T }> }> => (msg) =>
> = (msg) => msg.chat.type === 'channel' msg.chat.type === type
/** /**
* Filter incoming messages. * Filter incoming messages.
@ -332,6 +334,33 @@ export namespace filters {
{ media: Exclude<Message['media'], null> } { media: Exclude<Message['media'], null> }
> = (msg) => msg.media !== null > = (msg) => msg.media !== null
/**
* Filter text-only messages non-service messages
*/
export const text: UpdateFilter<
Message,
{
media: null
isService: false
}
> = (msg) => msg.media === null && !msg.isService
/**
* Filter service messages
*/
export const service: UpdateFilter<Message, { isService: true }> = (msg) =>
msg.isService
/**
* Filter service messages by action type
*/
export const action = <T extends Exclude<MessageAction, null>['type']>(
type: T
): UpdateFilter<
Message,
{ action: Extract<MessageAction, { type: T }> }
> => (msg) => msg.action?.type === type
/** /**
* Filter messages containing a photo * Filter messages containing a photo
*/ */