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 {
|
export class TelegramClient extends BaseTelegramClient {
|
||||||
protected _userId: number | null
|
protected _userId: number | null
|
||||||
protected _isBot: boolean
|
protected _isBot: boolean
|
||||||
|
protected _botUsername: string | null
|
||||||
protected _downloadConnections: Record<number, TelegramConnection>
|
protected _downloadConnections: Record<number, TelegramConnection>
|
||||||
protected _connectionsForInline: Record<number, TelegramConnection>
|
protected _connectionsForInline: Record<number, TelegramConnection>
|
||||||
protected _parseModes: Record<string, IMessageEntityParser>
|
protected _parseModes: Record<string, IMessageEntityParser>
|
||||||
|
@ -3069,6 +3070,7 @@ export class TelegramClient extends BaseTelegramClient {
|
||||||
super(opts)
|
super(opts)
|
||||||
this._userId = null
|
this._userId = null
|
||||||
this._isBot = false
|
this._isBot = false
|
||||||
|
this._botUsername = null
|
||||||
this._downloadConnections = {}
|
this._downloadConnections = {}
|
||||||
this._connectionsForInline = {}
|
this._connectionsForInline = {}
|
||||||
this._parseModes = {}
|
this._parseModes = {}
|
||||||
|
|
|
@ -8,10 +8,12 @@ interface AuthState {
|
||||||
// (see methods/updates)
|
// (see methods/updates)
|
||||||
_userId: number | null
|
_userId: number | null
|
||||||
_isBot: boolean
|
_isBot: boolean
|
||||||
|
_botUsername: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
// @initialize
|
// @initialize
|
||||||
function _initializeAuthState(this: TelegramClient) {
|
function _initializeAuthState(this: TelegramClient) {
|
||||||
this._userId = null
|
this._userId = null
|
||||||
this._isBot = false
|
this._isBot = false
|
||||||
|
this._botUsername = null
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@ export async function signInBot(
|
||||||
|
|
||||||
this._userId = res.user.id
|
this._userId = res.user.id
|
||||||
this._isBot = true
|
this._isBot = true
|
||||||
|
this._botUsername = res.user.username!
|
||||||
this._selfChanged = true
|
this._selfChanged = true
|
||||||
await this._fetchUpdatesState()
|
await this._fetchUpdatesState()
|
||||||
await this._saveStorage()
|
await this._saveStorage()
|
||||||
|
|
|
@ -728,7 +728,7 @@ export namespace filters {
|
||||||
const commandsRe: Record<string, RegExp> = {}
|
const commandsRe: Record<string, RegExp> = {}
|
||||||
commands.forEach((cmd) => {
|
commands.forEach((cmd) => {
|
||||||
commandsRe[cmd] = new RegExp(
|
commandsRe[cmd] = new RegExp(
|
||||||
`^${cmd}(?:\\s|$)`,
|
`^${cmd}(?:\\s|$|@([a-zA-Z0-9_]+?bot)(?:\\s|$))`,
|
||||||
caseSensitive ? '' : 'i'
|
caseSensitive ? '' : 'i'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
@ -736,13 +736,29 @@ export namespace filters {
|
||||||
if (prefixes === null) prefixes = []
|
if (prefixes === null) prefixes = []
|
||||||
if (typeof prefixes === 'string') prefixes = [prefixes]
|
if (typeof prefixes === 'string') prefixes = [prefixes]
|
||||||
|
|
||||||
return (msg) => {
|
const check = (msg: Message): MaybeAsync<boolean> => {
|
||||||
for (const pref of prefixes!) {
|
for (const pref of prefixes!) {
|
||||||
if (!msg.text.startsWith(pref)) continue
|
if (!msg.text.startsWith(pref)) continue
|
||||||
|
|
||||||
const withoutPrefix = msg.text.slice(pref.length)
|
const withoutPrefix = msg.text.slice(pref.length)
|
||||||
for (const cmd of commands) {
|
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]
|
const match = [cmd]
|
||||||
// we use .replace to iterate over global regex, not to replace the text
|
// we use .replace to iterate over global regex, not to replace the text
|
||||||
|
@ -762,6 +778,8 @@ export namespace filters {
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return check
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue