fix(dispatcher): handle commands that contain bot username
This commit is contained in:
parent
1371f935cd
commit
f2671d3c0b
4 changed files with 26 additions and 3 deletions
|
@ -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<number, TelegramConnection>
|
||||
protected _connectionsForInline: Record<number, TelegramConnection>
|
||||
protected _parseModes: Record<string, IMessageEntityParser>
|
||||
|
@ -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 = {}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -728,7 +728,7 @@ export namespace filters {
|
|||
const commandsRe: Record<string, RegExp> = {}
|
||||
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<boolean> => {
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue