From f2671d3c0b970d52579ab879e6fdd61893c220c8 Mon Sep 17 00:00:00 2001 From: teidesu Date: Fri, 18 Jun 2021 19:21:22 +0300 Subject: [PATCH] fix(dispatcher): handle commands that contain bot username --- packages/client/src/client.ts | 2 ++ .../client/src/methods/auth/_initialize.ts | 2 ++ .../client/src/methods/auth/sign-in-bot.ts | 1 + packages/dispatcher/src/filters.ts | 24 ++++++++++++++++--- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 2316f72c..59222832 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -3050,6 +3050,7 @@ export interface TelegramClient extends BaseTelegramClient { export class TelegramClient extends BaseTelegramClient { protected _userId: number | null protected _isBot: boolean + protected _botUsername: string | null protected _downloadConnections: Record protected _connectionsForInline: Record protected _parseModes: Record @@ -3069,6 +3070,7 @@ export class TelegramClient extends BaseTelegramClient { super(opts) this._userId = null this._isBot = false + this._botUsername = null this._downloadConnections = {} this._connectionsForInline = {} this._parseModes = {} diff --git a/packages/client/src/methods/auth/_initialize.ts b/packages/client/src/methods/auth/_initialize.ts index f56524bf..54aec93b 100644 --- a/packages/client/src/methods/auth/_initialize.ts +++ b/packages/client/src/methods/auth/_initialize.ts @@ -8,10 +8,12 @@ interface AuthState { // (see methods/updates) _userId: number | null _isBot: boolean + _botUsername: string | null } // @initialize function _initializeAuthState(this: TelegramClient) { this._userId = null this._isBot = false + this._botUsername = null } diff --git a/packages/client/src/methods/auth/sign-in-bot.ts b/packages/client/src/methods/auth/sign-in-bot.ts index 3220dbe8..3a55787b 100644 --- a/packages/client/src/methods/auth/sign-in-bot.ts +++ b/packages/client/src/methods/auth/sign-in-bot.ts @@ -35,6 +35,7 @@ export async function signInBot( this._userId = res.user.id this._isBot = true + this._botUsername = res.user.username! this._selfChanged = true await this._fetchUpdatesState() await this._saveStorage() diff --git a/packages/dispatcher/src/filters.ts b/packages/dispatcher/src/filters.ts index 53dd9026..7a2d93ae 100644 --- a/packages/dispatcher/src/filters.ts +++ b/packages/dispatcher/src/filters.ts @@ -728,7 +728,7 @@ export namespace filters { const commandsRe: Record = {} commands.forEach((cmd) => { commandsRe[cmd] = new RegExp( - `^${cmd}(?:\\s|$)`, + `^${cmd}(?:\\s|$|@([a-zA-Z0-9_]+?bot)(?:\\s|$))`, caseSensitive ? '' : 'i' ) }) @@ -736,13 +736,29 @@ export namespace filters { if (prefixes === null) prefixes = [] if (typeof prefixes === 'string') prefixes = [prefixes] - return (msg) => { + const check = (msg: Message): MaybeAsync => { for (const pref of prefixes!) { if (!msg.text.startsWith(pref)) continue const withoutPrefix = msg.text.slice(pref.length) for (const cmd of commands) { - if (!withoutPrefix.match(commandsRe[cmd])) continue + const m = withoutPrefix.match(commandsRe[cmd]) + if (!m) continue + + if (m[1] && msg.client['_isBot']) { + // check bot username + if (!msg.client['_botUsername']) { + // need to fetch it first + + return msg.client.getUsers('self') + .then((self) => { + msg.client['_botUsername'] = self.username! + return check(msg) + }) + } + + if (m[1] !== msg.client['_botUsername']) return false + } const match = [cmd] // we use .replace to iterate over global regex, not to replace the text @@ -762,6 +778,8 @@ export namespace filters { return false } + + return check } /**