mtcute/packages/client/src/methods/users/get-me.ts

38 lines
1 KiB
TypeScript
Raw Normal View History

2023-09-22 15:32:28 +03:00
import { assertTypeIs } from '@mtcute/core/utils'
2021-04-08 12:19:38 +03:00
import { TelegramClient } from '../../client'
import { User } from '../../types'
/**
* Get currently authorized user's full information
*
* @internal
*/
export function getMe(this: TelegramClient): Promise<User> {
return this.call({
_: 'users.getUsers',
id: [
{
_: 'inputUserSelf',
},
],
}).then(async ([user]) => {
assertTypeIs('getMe (@ users.getUsers)', user, 'user')
2021-04-08 12:19:38 +03:00
if (this._userId !== user.id) {
// there is such possibility, e.g. when
// using a string session without `self`,
// or logging out and re-logging in
// we need to update the fields accordingly,
// and force-save the session
this._userId = user.id
this._isBot = Boolean(user.bot)
await this._saveStorage()
}
this._selfUsername = user.username ?? null
return new User(this, user)
2021-04-08 12:19:38 +03:00
})
}