refactor: use constructor check instead of instanceof where possible
this might just have introduced a bunch of errors, but ill test later
This commit is contained in:
parent
f7e8051a94
commit
97ba7652ff
5 changed files with 42 additions and 60 deletions
|
@ -10,29 +10,7 @@ import {
|
|||
import { TelegramClient } from '../../client'
|
||||
import { MessageEntity } from './message-entity'
|
||||
import { makeInspectable } from '../utils'
|
||||
import {
|
||||
Audio,
|
||||
Contact,
|
||||
Document,
|
||||
Photo,
|
||||
Dice,
|
||||
Video,
|
||||
Location,
|
||||
LiveLocation,
|
||||
Sticker,
|
||||
Voice,
|
||||
InputMediaLike,
|
||||
Venue,
|
||||
Poll,
|
||||
Invoice,
|
||||
Game,
|
||||
WebPage,
|
||||
} from '../media'
|
||||
import { parseDocument } from '../media/document-utils'
|
||||
import {
|
||||
_callDiscardReasonFromTl,
|
||||
CallDiscardReason,
|
||||
} from '../calls/discard-reason'
|
||||
import { InputMediaLike, WebPage } from '../media'
|
||||
import { _messageActionFromTl, MessageAction } from './message-action'
|
||||
import { _messageMediaFromTl, MessageMedia } from './message-media'
|
||||
|
||||
|
@ -694,7 +672,11 @@ export class Message {
|
|||
* @param clearMentions Whether to also clear mentions
|
||||
*/
|
||||
async read(clearMentions = false): Promise<void> {
|
||||
return this.client.readHistory(this.chat.inputPeer, this.raw.id, clearMentions)
|
||||
return this.client.readHistory(
|
||||
this.chat.inputPeer,
|
||||
this.raw.id,
|
||||
clearMentions
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -511,11 +511,11 @@ export class BaseTelegramClient {
|
|||
}
|
||||
|
||||
if (
|
||||
e instanceof FloodWaitError ||
|
||||
e instanceof SlowmodeWaitError ||
|
||||
e instanceof FloodTestPhoneWaitError
|
||||
e.constructor === FloodWaitError ||
|
||||
e.constructor === SlowmodeWaitError ||
|
||||
e.constructor === FloodTestPhoneWaitError
|
||||
) {
|
||||
if (!(e instanceof SlowmodeWaitError)) {
|
||||
if (e.constructor !== SlowmodeWaitError) {
|
||||
// SLOW_MODE_WAIT is chat-specific, not request-specific
|
||||
this._floodWaitedRequests[message._] =
|
||||
Date.now() + e.seconds * 1000
|
||||
|
@ -538,9 +538,9 @@ export class BaseTelegramClient {
|
|||
}
|
||||
if (
|
||||
connection === this.primaryConnection &&
|
||||
(e instanceof PhoneMigrateError ||
|
||||
e instanceof UserMigrateError ||
|
||||
e instanceof NetworkMigrateError)
|
||||
(e.constructor === PhoneMigrateError ||
|
||||
e.constructor === UserMigrateError ||
|
||||
e.constructor === NetworkMigrateError)
|
||||
) {
|
||||
debug('Migrate error, new dc = %d', e.newDc)
|
||||
await this.changeDc(e.newDc)
|
||||
|
|
|
@ -70,9 +70,9 @@ export class MtprotoSession {
|
|||
if (!this._authKey) throw new Error('Keys are not set up!')
|
||||
|
||||
const length =
|
||||
message instanceof Buffer
|
||||
message.constructor === Buffer
|
||||
? message.length
|
||||
: SerializationCounter.countNeededBytes(message)
|
||||
: SerializationCounter.countNeededBytes(message as tl.TlObject)
|
||||
let padding =
|
||||
(32 /* header size */ + length + 12) /* min padding */ % 16
|
||||
padding = 12 + (padding ? 16 - padding : 0)
|
||||
|
@ -83,8 +83,8 @@ export class MtprotoSession {
|
|||
encryptedWriter.long(messageId)
|
||||
encryptedWriter.int32(seqNo)
|
||||
encryptedWriter.uint32(length)
|
||||
if (message instanceof Buffer) encryptedWriter.raw(message)
|
||||
else encryptedWriter.object(message)
|
||||
if (message.constructor === Buffer) encryptedWriter.raw(message)
|
||||
else encryptedWriter.object(message as tl.TlObject)
|
||||
encryptedWriter.raw(randomBytes(padding))
|
||||
|
||||
const innerData = encryptedWriter.result()
|
||||
|
|
|
@ -77,8 +77,8 @@ export class ObfuscatedPacketCodec extends EventEmitter implements PacketCodec {
|
|||
|
||||
feed(data: Buffer): void {
|
||||
const dec = this._decryptor!.decrypt(data)
|
||||
if (dec instanceof Buffer) this._inner.feed(dec)
|
||||
else dec.then((dec) => this._inner.feed(dec))
|
||||
if (dec.constructor === Buffer) this._inner.feed(dec)
|
||||
else (dec as Promise<Buffer>).then((dec) => this._inner.feed(dec))
|
||||
}
|
||||
|
||||
reset(): void {
|
||||
|
|
|
@ -287,13 +287,13 @@ export namespace filters {
|
|||
* Filter messages generated by yourself (including Saved Messages)
|
||||
*/
|
||||
export const me: UpdateFilter<Message, { sender: User }> = (msg) =>
|
||||
(msg.sender instanceof User && msg.sender.isSelf) || msg.isOutgoing
|
||||
(msg.sender.constructor === User && msg.sender.isSelf) || msg.isOutgoing
|
||||
|
||||
/**
|
||||
* Filter messages sent by bots
|
||||
*/
|
||||
export const bot: UpdateFilter<Message, { sender: User }> = (msg) =>
|
||||
msg.sender instanceof User && msg.sender.isBot
|
||||
msg.sender.constructor === User && msg.sender.isBot
|
||||
|
||||
/**
|
||||
* Filter messages by chat type
|
||||
|
@ -365,19 +365,19 @@ export namespace filters {
|
|||
* Filter messages containing a photo
|
||||
*/
|
||||
export const photo: UpdateFilter<Message, { media: Photo }> = (msg) =>
|
||||
msg.media instanceof Photo
|
||||
msg.media?.constructor === Photo
|
||||
|
||||
/**
|
||||
* Filter messages containing a dice
|
||||
*/
|
||||
export const dice: UpdateFilter<Message, { media: Dice }> = (msg) =>
|
||||
msg.media instanceof Dice
|
||||
msg.media?.constructor === Dice
|
||||
|
||||
/**
|
||||
* Filter messages containing a contact
|
||||
*/
|
||||
export const contact: UpdateFilter<Message, { media: Contact }> = (msg) =>
|
||||
msg.media instanceof Contact
|
||||
msg.media?.constructor === Contact
|
||||
|
||||
/**
|
||||
* Filter messages containing a document
|
||||
|
@ -395,25 +395,25 @@ export namespace filters {
|
|||
* This will not match media like audio, video, voice
|
||||
*/
|
||||
export const document: UpdateFilter<Message, { media: Document }> = (msg) =>
|
||||
msg.media instanceof Document
|
||||
msg.media?.constructor === Document
|
||||
|
||||
/**
|
||||
* Filter messages containing an audio file
|
||||
*/
|
||||
export const audio: UpdateFilter<Message, { media: Audio }> = (msg) =>
|
||||
msg.media instanceof Audio
|
||||
msg.media?.constructor === Audio
|
||||
|
||||
/**
|
||||
* Filter messages containing a voice note
|
||||
*/
|
||||
export const voice: UpdateFilter<Message, { media: Voice }> = (msg) =>
|
||||
msg.media instanceof Voice
|
||||
msg.media?.constructor === Voice
|
||||
|
||||
/**
|
||||
* Filter messages containing a sticker
|
||||
*/
|
||||
export const sticker: UpdateFilter<Message, { media: Sticker }> = (msg) =>
|
||||
msg.media instanceof Sticker
|
||||
msg.media?.constructor === Sticker
|
||||
|
||||
/**
|
||||
* Filter messages containing a video.
|
||||
|
@ -421,7 +421,7 @@ export namespace filters {
|
|||
* This includes videos, round messages and animations
|
||||
*/
|
||||
export const rawVideo: UpdateFilter<Message, { media: Video }> = (msg) =>
|
||||
msg.media instanceof Video
|
||||
msg.media?.constructor === Video
|
||||
|
||||
/**
|
||||
* Filter messages containing a simple video.
|
||||
|
@ -440,7 +440,7 @@ export namespace filters {
|
|||
>
|
||||
}
|
||||
> = (msg) =>
|
||||
msg.media instanceof Video &&
|
||||
msg.media?.constructor === Video &&
|
||||
!msg.media.isAnimation &&
|
||||
!msg.media.isRound
|
||||
|
||||
|
@ -462,7 +462,7 @@ export namespace filters {
|
|||
>
|
||||
}
|
||||
> = (msg) =>
|
||||
msg.media instanceof Video &&
|
||||
msg.media?.constructor === Video &&
|
||||
msg.media.isAnimation &&
|
||||
!msg.media.isRound
|
||||
|
||||
|
@ -481,7 +481,7 @@ export namespace filters {
|
|||
>
|
||||
}
|
||||
> = (msg) =>
|
||||
msg.media instanceof Video &&
|
||||
msg.media?.constructor === Video &&
|
||||
!msg.media.isAnimation &&
|
||||
msg.media.isRound
|
||||
|
||||
|
@ -499,37 +499,37 @@ export namespace filters {
|
|||
export const liveLocation: UpdateFilter<
|
||||
Message,
|
||||
{ media: LiveLocation }
|
||||
> = (msg) => msg.media instanceof LiveLocation
|
||||
> = (msg) => msg.media?.constructor === LiveLocation
|
||||
|
||||
/**
|
||||
* Filter messages containing a game.
|
||||
*/
|
||||
export const game: UpdateFilter<Message, { media: Game }> = (msg) =>
|
||||
msg.media instanceof Game
|
||||
msg.media?.constructor === Game
|
||||
|
||||
/**
|
||||
* Filter messages containing a webpage preview.
|
||||
*/
|
||||
export const webpage: UpdateFilter<Message, { media: WebPage }> = (msg) =>
|
||||
msg.media instanceof WebPage
|
||||
msg.media?.constructor === WebPage
|
||||
|
||||
/**
|
||||
* Filter messages containing a venue.
|
||||
*/
|
||||
export const venue: UpdateFilter<Message, { media: Venue }> = (msg) =>
|
||||
msg.media instanceof Venue
|
||||
msg.media?.constructor === Venue
|
||||
|
||||
/**
|
||||
* Filter messages containing a poll.
|
||||
*/
|
||||
export const poll: UpdateFilter<Message, { media: Poll }> = (msg) =>
|
||||
msg.media instanceof Poll
|
||||
msg.media?.constructor === Poll
|
||||
|
||||
/**
|
||||
* Filter messages containing an invoice.
|
||||
*/
|
||||
export const invoice: UpdateFilter<Message, { media: Invoice }> = (msg) =>
|
||||
msg.media instanceof Invoice
|
||||
msg.media?.constructor === Invoice
|
||||
|
||||
/**
|
||||
* Filter objects that match a given regular expression
|
||||
|
@ -550,13 +550,13 @@ export namespace filters {
|
|||
{ match: RegExpMatchArray }
|
||||
> => (obj) => {
|
||||
let m: RegExpMatchArray | null = null
|
||||
if (obj instanceof Message) {
|
||||
if (obj?.constructor === Message) {
|
||||
m = obj.text.match(regex)
|
||||
} else if (obj instanceof InlineQuery) {
|
||||
} else if (obj?.constructor === InlineQuery) {
|
||||
m = obj.query.match(regex)
|
||||
} else if (obj instanceof ChosenInlineResult) {
|
||||
} else if (obj?.constructor === ChosenInlineResult) {
|
||||
m = obj.id.match(regex)
|
||||
} else if (obj instanceof CallbackQuery) {
|
||||
} else if (obj?.constructor === CallbackQuery) {
|
||||
if (obj.raw.data) m = obj.dataStr!.match(regex)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue