diff --git a/packages/dispatcher/src/context/message.ts b/packages/dispatcher/src/context/message.ts index ee3a7468..65b7ccb1 100644 --- a/packages/dispatcher/src/context/message.ts +++ b/packages/dispatcher/src/context/message.ts @@ -1,4 +1,4 @@ -import type { OmitInputMessageId, ParametersSkip1, Peer, Sticker } from '@mtcute/core' +import type { Chat, OmitInputMessageId, ParametersSkip1, Peer, Sticker } from '@mtcute/core' import { Message, MtPeerNotFoundError } from '@mtcute/core' import type { TelegramClient } from '@mtcute/core/client.js' import type { @@ -67,6 +67,23 @@ export class MessageContext extends Message implements UpdateContext { return res } + /** + * Get complete information about {@link chat} + * + * Learn more: [Incomplete peers](https://mtcute.dev/guide/topics/peers.html#incomplete-peers) + */ + async getCompleteChat(): Promise { + if (!this.chat.isMin) return this.chat + + const res = await this.client.getChat(this.chat) + + if (!res) throw new MtPeerNotFoundError('Failed to fetch chat') + + Object.defineProperty(this, 'chat', { value: res }) + + return res + } + /** Get a message that this message is a reply to */ getReplyTo(): Promise { return this.client.getReplyTo(this) diff --git a/packages/dispatcher/src/filters/message.ts b/packages/dispatcher/src/filters/message.ts index 4f8e1e27..7164e7fb 100644 --- a/packages/dispatcher/src/filters/message.ts +++ b/packages/dispatcher/src/filters/message.ts @@ -296,3 +296,23 @@ export function withCompleteSender( return filter(msg, state) } } + +/** + * Middleware-like filter that will fetch the chat of the message + * and make it available to further filters, as well as the handler itself. + */ +export function withCompleteChat( + filter?: UpdateFilter, +): UpdateFilter { + return async (msg, state) => { + try { + await msg.getCompleteChat() + } catch { + return false + } + + if (!filter) return true + + return filter(msg, state) + } +}