From a34dcfbe1026f3cab954a8f76af7a3a625ce0681 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Thu, 12 Mar 2026 02:07:57 -0400 Subject: [PATCH] [O] Checks and bold messages --- src/bot.py | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/bot.py b/src/bot.py index a03c43c..22ca828 100644 --- a/src/bot.py +++ b/src/bot.py @@ -1,3 +1,4 @@ +import html import re import time import tomllib @@ -211,6 +212,11 @@ async def handle_water(update: Update, user_id: int, channel: str): if not info: return await update.message.reply_text("这个频道不在树上哦~") + # Block self-watering + owner_id = db.get_channel_owner(channel) + if owner_id == user_id: + return await update.message.reply_text("不能给自己的频道浇水哦~ 🥺") + if db.add_vote(user_id, channel): votes = db.get_votes(channel) await update.message.reply_text(f"💧 浇水成功!这个树枝已经被浇了 {votes} 次水~") @@ -223,19 +229,27 @@ async def handle_treehole(update: Update, user_id: int, channel: str): """Handle the 树洞 action via deep-link.""" logger.info(f"🕳️ Tree hole from {user_id} for {channel}") + # Check if channel has an owner + owner_id = db.get_channel_owner(channel) + if not owner_id: + return await update.message.reply_text("这个频道还没有设置主人哦~") + + # Block sending to oneself + if owner_id == user_id: + return await update.message.reply_text("不能给自己发树洞消息哦~ 🥺") + + # Check if owner walked away + if db.is_treehole_opted_out(owner_id): + return await update.message.reply_text("树洞对面没有人的样子,过一会再试试吧!(对方关闭了树洞)") + # Check rate limit last_time = treehole_rate_limit.get(user_id, 0) if time.time() - last_time < TREEHOLE_COOLDOWN: remaining = int(TREEHOLE_COOLDOWN - (time.time() - last_time)) return await update.message.reply_text(f"发送太频繁了,请 {remaining} 秒后再试~") - # Check if channel has an owner - owner_id = db.get_channel_owner(channel) - if not owner_id: - return await update.message.reply_text("这个频道还没有设置主人哦~") - user_states[user_id] = {"action": "treehole", "channel": channel} - await update.message.reply_text(f"🕳️ 树洞模式\n\n想对频道 @{channel} 的主人说什么呢?(发送文字消息即可,消息将会匿名发送)") + await update.message.reply_html(f"🕳️ 树洞模式\n\n想对频道 @{channel} 的主人说什么呢?(发送文字消息即可,消息将会匿名发送)") async def reply_callback(update: Update, context: ContextTypes.DEFAULT_TYPE): @@ -329,7 +343,8 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): try: await context.bot.send_message( chat_id=owner_id, - text="树洞对面传来消息了!(其实是匿名消息功能啦)\n\n如果不想要听到树洞消息的话可以说 /walkaway" + text="树洞对面传来消息了!(其实是匿名消息功能啦)\n\n如果不想要听到树洞消息的话可以说 /walkaway", + parse_mode="HTML" ) except Exception: pass @@ -342,10 +357,12 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): try: name = anon_name(user_id) + escaped_text = html.escape(update.message.text) await context.bot.send_message( chat_id=owner_id, - text=f"🕳️ 树洞消息\n\n匿名{name}对你的频道 @{channel} 说:\n\n{update.message.text}", - reply_markup=reply_btn + text=f"🕳️ 树洞消息\n\n匿名{name}对你的频道 @{channel} 说:\n\n{escaped_text}", + reply_markup=reply_btn, + parse_mode="HTML" ) await update.message.reply_text("✅ 消息已匿名发送~") except Exception: @@ -357,9 +374,11 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): del user_states[user_id] try: + escaped_text = html.escape(update.message.text) await context.bot.send_message( chat_id=sender_id, - text=f"💬 频道 @{channel} 的主人回复了你的树洞消息:\n\n{update.message.text}" + text=f"💬 频道 @{channel} 的主人回复了你的树洞消息:\n\n{escaped_text}", + parse_mode="HTML" ) await update.message.reply_text("✅ 回复已发送~") except Exception: