From a870ebfe182288e1f8a04ed0405211e832284f64 Mon Sep 17 00:00:00 2001 From: Orion Date: Mon, 4 May 2026 02:49:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20=F0=9F=90=9B=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=20Telegram=20=E6=9C=BA=E5=99=A8=E4=BA=BA=E6=B6=88=E6=81=AF=20I?= =?UTF-8?q?D=20=E6=9F=A5=E8=AF=A2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 调整 tg-bot.js 中针对回复消息 ID 的处理逻辑。通过显式转换原始 ID 为字符串,并增加针对不同数据类型的回退查询机制,解决了由于数据库字段类型不匹配导致的删除请求映射失败问题。此外,增强了相关操作的日志输出以便于调试。 --- telegram/tg-bot.js | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/telegram/tg-bot.js b/telegram/tg-bot.js index b3ad5f5..e9b864d 100644 --- a/telegram/tg-bot.js +++ b/telegram/tg-bot.js @@ -246,15 +246,45 @@ async function handleUserDelete(msg, u, env) { }); } - const targetMsgId = msg.reply_to_message.message_id.toString(); - console.log(`Delete request: user=${u.user_id}, target_msg=${targetMsgId}`); + const targetMsgIdRaw = msg.reply_to_message.message_id; + const targetMsgId = targetMsgIdRaw.toString(); + console.log(`Delete request: user=${u.user_id}, target_msg_raw=${targetMsgIdRaw} (type: ${typeof targetMsgIdRaw}), target_msg_str=${targetMsgId}`); - // 查询对应的管理员侧消息ID - const ref = await sql(env, "SELECT topic_message_id FROM messages WHERE user_id=? AND message_id=?", [u.user_id, targetMsgId], 'first'); + // 查询对应的管理员侧消息ID - 尝试多种可能的格式 + let ref = await sql(env, "SELECT topic_message_id FROM messages WHERE user_id=? AND message_id=?", [u.user_id, targetMsgId], 'first'); + + // 如果没找到,尝试用整数查询(以防数据库中存的是数字) + if (!ref || !ref.topic_message_id) { + console.log(`First query failed, trying with integer...`); + ref = await sql(env, "SELECT topic_message_id FROM messages WHERE user_id=? AND message_id=?", [u.user_id, parseInt(targetMsgId)], 'first'); + } if (!ref || !ref.topic_message_id) { console.log(`Delete failed: No mapping found for user=${u.user_id}, msg=${targetMsgId}`); - console.log(`Tip: Check if the message was forwarded successfully and exists in database`); + + // 检查是否是管理员发送的消息(通过反向查询) + const adminRef = await sql(env, "SELECT user_id FROM messages WHERE topic_message_id=?", [targetMsgId], 'first'); + if (adminRef) { + console.log(`Delete blocked: User tried to delete admin's message (topic_msg=${targetMsgId})`); + return api(env.BOT_TOKEN, "sendMessage", { + chat_id: u.user_id, + text: "❌ 您只能删除自己发送的消息,无法删除管理员回复的消息", + reply_to_message_id: msg.message_id + }); + } + + console.log(`Tip: Check database records with: SELECT * FROM messages WHERE user_id='${u.user_id}'`); + + // 帮助用户排查:列出该用户的最近5条消息记录 + try { + const recentMsgs = await sql(env, "SELECT message_id, topic_message_id, text FROM messages WHERE user_id=? ORDER BY date DESC LIMIT 5", [u.user_id], 'all'); + if (recentMsgs && recentMsgs.results) { + console.log(`Recent messages for user ${u.user_id}:`, JSON.stringify(recentMsgs.results)); + } + } catch (e) { + console.log(`Failed to fetch recent messages:`, e.message); + } + return api(env.BOT_TOKEN, "sendMessage", { chat_id: u.user_id, text: "❌ 未找到对应的消息记录,可能该消息未被转发或已被删除",