mirror of
https://github.com/xCyanGrizzly/DragonsStash.git
synced 2026-09-21 05:21:43 +00:00
fix(bot): resolve private chat before sending to a user
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
After the bot's TDLib session was rebuilt from scratch (following a disk-full corruption on 2026-08-05), sendMessage started failing with "Chat not found" for every previously-known user — a fresh TDLib database has no cached chat/peer info until createPrivateChat explicitly resolves it. Call it before every send.
This commit is contained in:
@@ -53,6 +53,21 @@ export async function closeBotClient(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure TDLib has resolved this user into a private chat before sending.
|
||||
* A bot's local TDLib database only knows about chats it has seen since its
|
||||
* last (re)authentication — after a state-directory reset it has no chat
|
||||
* history cached, so `sendMessage` fails with "Chat not found" even for
|
||||
* users who messaged the bot long ago. `createPrivateChat` forces TDLib to
|
||||
* resolve/fetch the chat first.
|
||||
*/
|
||||
async function ensurePrivateChat(c: tdl.Client, userId: number): Promise<void> {
|
||||
await withFloodWait(
|
||||
() => c.invoke({ _: "createPrivateChat", user_id: userId, force: false }),
|
||||
"createPrivateChat"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a document from a channel to a user's DM.
|
||||
*
|
||||
@@ -71,6 +86,8 @@ export async function copyMessageToUser(
|
||||
if (!client) throw new Error("Bot client not initialized");
|
||||
const c = client;
|
||||
|
||||
await ensurePrivateChat(c, Number(toUserId));
|
||||
|
||||
log.info(
|
||||
{ fromChatId: fromChatId.toString(), messageId: messageId.toString(), toUserId: toUserId.toString() },
|
||||
"Sending file to user"
|
||||
@@ -233,6 +250,8 @@ export async function sendTextMessage(
|
||||
if (!client) throw new Error("Bot client not initialized");
|
||||
const c = client;
|
||||
|
||||
await ensurePrivateChat(c, Number(chatId));
|
||||
|
||||
// Parse the text first
|
||||
const parsed = await withFloodWait(
|
||||
() =>
|
||||
@@ -269,6 +288,8 @@ export async function sendPhotoMessage(
|
||||
if (!client) throw new Error("Bot client not initialized");
|
||||
const c = client;
|
||||
|
||||
await ensurePrivateChat(c, Number(chatId));
|
||||
|
||||
// Write the photo to a temp file
|
||||
const { writeFile, unlink } = await import("fs/promises");
|
||||
const path = await import("path");
|
||||
|
||||
Reference in New Issue
Block a user