fix(core): 🔊 增强电报机器人删除请求的日志记录
在 tg-bot.js 中添加了详细的控制台日志,用于记录用户 ID 和目标消息 ID。当消息映射未找到时,新增了调试提示,以便更好地追踪转发失败或数据库记录缺失的问题。
This commit is contained in:
@@ -247,12 +247,14 @@ async function handleUserDelete(msg, u, env) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const targetMsgId = msg.reply_to_message.message_id.toString();
|
const targetMsgId = msg.reply_to_message.message_id.toString();
|
||||||
|
console.log(`Delete request: user=${u.user_id}, target_msg=${targetMsgId}`);
|
||||||
|
|
||||||
// 查询对应的管理员侧消息ID
|
// 查询对应的管理员侧消息ID
|
||||||
const ref = await sql(env, "SELECT topic_message_id FROM messages WHERE user_id=? AND message_id=?", [u.user_id, targetMsgId], 'first');
|
const 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) {
|
if (!ref || !ref.topic_message_id) {
|
||||||
console.log(`Delete failed: No mapping found for user=${u.user_id}, msg=${targetMsgId}`);
|
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`);
|
||||||
return api(env.BOT_TOKEN, "sendMessage", {
|
return api(env.BOT_TOKEN, "sendMessage", {
|
||||||
chat_id: u.user_id,
|
chat_id: u.user_id,
|
||||||
text: "❌ 未找到对应的消息记录,可能该消息未被转发或已被删除",
|
text: "❌ 未找到对应的消息记录,可能该消息未被转发或已被删除",
|
||||||
@@ -260,18 +262,20 @@ async function handleUserDelete(msg, u, env) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`Delete success: Found mapping topic_msg=${ref.topic_message_id}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. 删除被回复的目标消息(先删目标,再删命令)
|
// 1. 删除被回复的目标消息(先删目标,再删命令)
|
||||||
await api(env.BOT_TOKEN, "deleteMessage", {
|
await api(env.BOT_TOKEN, "deleteMessage", {
|
||||||
chat_id: u.user_id,
|
chat_id: u.user_id,
|
||||||
message_id: parseInt(targetMsgId)
|
message_id: parseInt(targetMsgId)
|
||||||
}).catch(() => {});
|
}).catch((e) => console.log("Failed to delete target msg:", e.message));
|
||||||
|
|
||||||
// 2. 删除用户侧的 /del 命令消息
|
// 2. 删除用户侧的 /del 命令消息
|
||||||
await api(env.BOT_TOKEN, "deleteMessage", {
|
await api(env.BOT_TOKEN, "deleteMessage", {
|
||||||
chat_id: u.user_id,
|
chat_id: u.user_id,
|
||||||
message_id: msg.message_id
|
message_id: msg.message_id
|
||||||
}).catch(() => {});
|
}).catch((e) => console.log("Failed to delete /del cmd:", e.message));
|
||||||
|
|
||||||
// 3. 通知管理员(引用原消息)
|
// 3. 通知管理员(引用原消息)
|
||||||
await api(env.BOT_TOKEN, "sendMessage", {
|
await api(env.BOT_TOKEN, "sendMessage", {
|
||||||
@@ -280,10 +284,11 @@ async function handleUserDelete(msg, u, env) {
|
|||||||
text: `🗑️ <b>用户已删除消息</b>`,
|
text: `🗑️ <b>用户已删除消息</b>`,
|
||||||
parse_mode: "HTML",
|
parse_mode: "HTML",
|
||||||
reply_to_message_id: parseInt(ref.topic_message_id)
|
reply_to_message_id: parseInt(ref.topic_message_id)
|
||||||
}).catch(() => {});
|
}).catch((e) => console.log("Failed to notify admin:", e.message));
|
||||||
|
|
||||||
// 4. 清理数据库记录
|
// 4. 清理数据库记录
|
||||||
await sql(env, "DELETE FROM messages WHERE user_id=? AND message_id=?", [u.user_id, targetMsgId]);
|
await sql(env, "DELETE FROM messages WHERE user_id=? AND message_id=?", [u.user_id, targetMsgId]);
|
||||||
|
console.log(`Delete completed: Cleaned up database record`);
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("User Delete Failed:", e);
|
console.error("User Delete Failed:", e);
|
||||||
@@ -583,7 +588,12 @@ async function relayToTopic(msg, u, env) {
|
|||||||
let topicReplyToMsgId = undefined;
|
let topicReplyToMsgId = undefined;
|
||||||
if (msg.reply_to_message) {
|
if (msg.reply_to_message) {
|
||||||
const ref = await sql(env, "SELECT topic_message_id FROM messages WHERE user_id=? AND message_id=?", [uid, msg.reply_to_message.message_id.toString()], 'first');
|
const ref = await sql(env, "SELECT topic_message_id FROM messages WHERE user_id=? AND message_id=?", [uid, msg.reply_to_message.message_id.toString()], 'first');
|
||||||
if (ref?.topic_message_id) topicReplyToMsgId = parseInt(ref.topic_message_id);
|
if (ref?.topic_message_id) {
|
||||||
|
topicReplyToMsgId = parseInt(ref.topic_message_id);
|
||||||
|
console.log(`Found reply mapping: user_msg=${msg.reply_to_message.message_id} -> topic_msg=${topicReplyToMsgId}`);
|
||||||
|
} else {
|
||||||
|
console.log(`No reply mapping found for user_msg=${msg.reply_to_message.message_id}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 特殊引用语法降级渲染支持
|
// 特殊引用语法降级渲染支持
|
||||||
@@ -601,28 +611,27 @@ async function relayToTopic(msg, u, env) {
|
|||||||
reply_to_message_id: topicReplyToMsgId
|
reply_to_message_id: topicReplyToMsgId
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// 标准转发处理尝试,若触碰受限隐私配置则回退到原生复制
|
// 标准转发处理:统一使用 copyMessage 以支持引用关系
|
||||||
try {
|
try {
|
||||||
|
const copyParams = {
|
||||||
|
chat_id: env.ADMIN_GROUP_ID,
|
||||||
|
from_chat_id: uid,
|
||||||
|
message_id: msg.message_id,
|
||||||
|
message_thread_id: tid
|
||||||
|
};
|
||||||
if (topicReplyToMsgId) {
|
if (topicReplyToMsgId) {
|
||||||
forwardedMsg = await api(env.BOT_TOKEN, "copyMessage", {
|
copyParams.reply_to_message_id = topicReplyToMsgId;
|
||||||
chat_id: env.ADMIN_GROUP_ID, from_chat_id: uid, message_id: msg.message_id, message_thread_id: tid, reply_to_message_id: topicReplyToMsgId
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
forwardedMsg = await api(env.BOT_TOKEN, "forwardMessage", {
|
|
||||||
chat_id: env.ADMIN_GROUP_ID, from_chat_id: uid, message_id: msg.message_id, message_thread_id: tid
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
forwardedMsg = await api(env.BOT_TOKEN, "copyMessage", copyParams);
|
||||||
} catch(fwdErr) {
|
} catch(fwdErr) {
|
||||||
console.log("Forward failed, trying copyMessage:", fwdErr.message);
|
console.log("copyMessage failed, trying forwardMessage:", fwdErr.message);
|
||||||
if (topicReplyToMsgId) {
|
// forwardMessage 不支持 reply_to_message_id,只能作为备选
|
||||||
forwardedMsg = await api(env.BOT_TOKEN, "copyMessage", {
|
forwardedMsg = await api(env.BOT_TOKEN, "forwardMessage", {
|
||||||
chat_id: env.ADMIN_GROUP_ID, from_chat_id: uid, message_id: msg.message_id, message_thread_id: tid, reply_to_message_id: topicReplyToMsgId
|
chat_id: env.ADMIN_GROUP_ID,
|
||||||
});
|
from_chat_id: uid,
|
||||||
} else {
|
message_id: msg.message_id,
|
||||||
forwardedMsg = await api(env.BOT_TOKEN, "copyMessage", {
|
message_thread_id: tid
|
||||||
chat_id: env.ADMIN_GROUP_ID, from_chat_id: uid, message_id: msg.message_id, message_thread_id: tid
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user