def mention_answer(client: Client, message: Message, aid: int, uid: int, action_type: str) -> str: # Mention abuse try: # Basic data gid = message.chat.id mid = message.message_id # Abuse if action_type == "abuse": message.reply_to_message = message message.reply_to_message.from_user.id = uid message.reply_to_message.from_user.is_self = lang("abuse_mention") text, markup = warn_user(client, message, uid, aid) text += f"{lang('reason')}{lang('colon')}{code(lang('reason_abuse'))}\n" # Edit the report message thread(edit_message_text, (client, gid, mid, text, markup)) delay(180, delete_message, [client, gid, mid]) # Delete elif action_type == "delete": glovar.message_ids[gid] = (0, 0) save("message_ids") delete_message(client, gid, mid) except Exception as e: logger.warning(f"Mention answer error: {e}", exc_info=True) return ""
def unban(client: Client, message: Message) -> bool: # Unban a user if not message or not message.chat: return True # Basic data gid = message.chat.id mid = message.message_id try: # Check permission if not is_class_c(None, None, message): return True aid = message.from_user.id command_type, command_context = get_command_context(message) # Text prefix text = ( f"{lang('admin')}{lang('colon')}{code(aid)}\n" f"{lang('action')}{lang('colon')}{code(lang('action_unban'))}\n") # Check command format if not command_type: text += ( f"{lang('status')}{lang('colon')}{code(lang('status_failed'))}\n" f"{lang('reason')}{lang('colon')}{code(lang('command_usage'))}\n" ) thread(send_report_message, (15, client, gid, text)) return True # Get user id uid = get_int(command_context) if not uid: peer_type, peer_id = resolve_username(client, command_type) if peer_type == "user" and peer_id: uid = peer_id # Proceed if not uid: text += ( f"{lang('status')}{lang('colon')}{code(lang('status_failed'))}\n" f"{lang('reason')}{lang('colon')}{code(lang('command_para'))}\n" ) thread(send_report_message, (15, client, gid, text)) return True # Proceed text = unban_user(client, message, uid, aid) thread(send_report_message, (30, client, gid, text)) return True except Exception as e: logger.warning(f"Unban error: {e}", exc_info=True) finally: delete_message(client, gid, mid) return False
def undo(client: Client, message: Message) -> bool: # Undo operations if not message or not message.chat: return True # Basic data gid = message.chat.id mid = message.message_id try: # Check permission if not is_class_c(None, None, message): return True aid = message.from_user.id r_message = message.reply_to_message # Text prefix text = ( f"{lang('admin')}{lang('colon')}{code(aid)}\n" f"{lang('action')}{lang('colon')}{code(lang('action_undo'))}\n") # Check usage if not r_message: text += ( f"{lang('status')}{lang('colon')}{code(lang('status_failed'))}\n" f"{lang('reason')}{lang('colon')}{code(lang('command_usage'))}\n" ) thread(send_report_message, (15, client, gid, text)) return True # Check message callback_data_list = get_callback_data(r_message) if not (r_message.from_user.is_self and callback_data_list and callback_data_list[0]["a"] == "undo"): text += ( f"{lang('status')}{lang('colon')}{code(lang('status_failed'))}\n" f"{lang('reason')}{lang('colon')}{code(lang('command_reply'))}\n" ) thread(send_report_message, (15, client, gid, text)) return True # Proceed action_type = callback_data_list[0]["t"] uid = callback_data_list[0]["d"] undo_user(client, r_message, aid, uid, action_type) return True except Exception as e: logger.warning(f"Undo error: {e}", exc_info=True) finally: delete_message(client, gid, mid) return False
def warn(client: Client, message: Message) -> bool: # Warn users if not message or not message.chat: return True # Basic data gid = message.chat.id mid = message.message_id try: # Check permission if not is_class_c(None, None, message): return True aid = message.from_user.id # Get user id uid, r_mid = get_class_d_id(message) # Check user status if not uid or uid in glovar.admin_ids[gid]: return True # Warn the user reason = get_command_type(message) text, markup = warn_user(client, message, uid, aid, reason) if markup: secs = 180 else: secs = 15 # Send the report message r_mid and delete_message(client, gid, r_mid) thread(send_report_message, (secs, client, gid, text, None, markup)) return True except Exception as e: logger.warning(f"Warn error: {e}", exc_info=True) finally: delete_message(client, gid, mid) return False
def forgive(client: Client, message: Message) -> bool: # Forgive users if not message or not message.chat: return True # Basic data gid = message.chat.id mid = message.message_id try: # Check permission if not is_class_c(None, None, message): return True # Get user id uid, _ = get_class_d_id(message) # Check user status if not uid or uid in glovar.admin_ids[gid]: return True # Forgive the user reason = get_command_type(message) text, success = forgive_user(client, message, uid, reason) glovar.user_ids[uid]["lock"].discard(gid) save("user_ids") if success: secs = 180 else: secs = 15 # Send the report message thread(send_report_message, (secs, client, gid, text, None)) return True except Exception as e: logger.warning(f"Forgive error: {e}", exc_info=True) finally: delete_message(client, gid, mid) return False
def admin(client: Client, message: Message) -> bool: # Mention admins if not message or not message.chat: return True # Basic data gid = message.chat.id mid = message.message_id try: # Check permission if is_class_c(None, None, message): return True # Check config if not glovar.configs[gid].get("mention"): return True uid = message.from_user.id # Init user data if not init_user_id(uid): return True # Warned user and the user having report status can't mention admins if (gid in glovar.user_ids[uid]["waiting"] or gid in glovar.user_ids[uid]["ban"] or glovar.user_ids[uid]["warn"].get(gid)): return True # Generate report text text = ( f"{lang('from_user')}{lang('colon')}{mention_id(uid)}\n" f"{lang('mention_admins')}{lang('colon')}{get_admin_text(gid)}\n") reason = get_command_type(message) if reason: text += f"{lang('reason')}{lang('colon')}{code(reason)}\n" # Generate report markup button_abuse = button_data("mention", "abuse", uid) button_delete = button_data("mention", "delete", uid) markup = InlineKeyboardMarkup([[ InlineKeyboardButton(text=lang("abuse"), callback_data=button_abuse), InlineKeyboardButton(text=lang("del"), callback_data=button_delete) ]]) # Send the report message if message.reply_to_message: rid = message.reply_to_message.message_id else: rid = None result = send_message(client, gid, text, rid, markup) if not result: return True old_mid, _ = glovar.message_ids.get(gid, (0, 0)) old_mid and thread(delete_message, (client, gid, old_mid)) sent_mid = result.message_id glovar.message_ids[gid] = (sent_mid, get_now()) save("message_ids") return True except Exception as e: logger.warning(f"Admin error: {e}", exc_info=True) finally: delete_message(client, gid, mid) return False
def report(client: Client, message: Message) -> bool: # Report spam messages if not message or not message.chat: return True # Basic data gid = message.chat.id mid = message.message_id try: # Normal user if not is_class_c(None, None, message): # Check config if not glovar.configs[gid]["report"]["manual"]: return True rid = message.from_user.id now = message.date or get_now() # Init user data if not init_user_id(rid): return True # Get user id uid, r_mid = get_class_d_id(message) # Init user data if not uid or not init_user_id(uid): return True # Check user status bad_user = (gid in glovar.user_ids[rid]["lock"] or gid in glovar.user_ids[uid]["lock"] or gid in glovar.user_ids[rid]["waiting"] or gid in glovar.user_ids[uid]["waiting"] or gid in glovar.user_ids[uid]["ban"] or is_watch_user(message.from_user, "ban", now) or is_watch_user(message.from_user, "delete", now) or is_high_score_user(message.from_user)) good_user = (is_class_e_user(message.from_user) and uid not in glovar.admin_ids[gid]) # Users can not self-report if uid == rid or (bad_user and not good_user): return True # Reporter cannot report someone by replying WARN's report r_message = message.reply_to_message if r_message.from_user.is_self: return True # Proceed if r_message.service: name = get_full_name(r_message.from_user) else: name = None reason = get_command_type(message) text, markup, key = report_user(gid, r_message.from_user, rid, r_mid, name, reason) result = send_message(client, gid, text, r_mid, markup) if result: glovar.reports[key]["report_id"] = result.message_id else: glovar.reports.pop(key, {}) save("reports") # Admin else: aid = message.from_user.id action_type, reason = get_command_context(message) # Text prefix text = ( f"{lang('admin')}{lang('colon')}{code(aid)}\n" f"{lang('action')}{lang('colon')}{code(lang('action_answer'))}\n" ) # Check command format if action_type not in {"warn", "ban", "cancel", "abuse" } or not message.reply_to_message: text += ( f"{lang('status')}{lang('colon')}{code(lang('status_failed'))}\n" f"{lang('reason')}{lang('colon')}{code(lang('command_usage'))}\n" ) thread(send_report_message, (15, client, gid, text)) return True # Check the evidence message r_message = get_message(client, gid, message.reply_to_message.message_id) if not r_message or not r_message.reply_to_message: text += ( f"{lang('status')}{lang('colon')}{code(lang('status_failed'))}\n" f"{lang('reason')}{lang('colon')}{code(lang('reason_deleted'))}\n" ) thread(send_report_message, (15, client, gid, text)) return True # Check the report message callback_data_list = get_callback_data(r_message) if not callback_data_list or callback_data_list[0]["a"] != "report": text += ( f"{lang('status')}{lang('colon')}{code(lang('status_failed'))}\n" f"{lang('reason')}{lang('colon')}{code(lang('command_reply'))}\n" ) thread(send_report_message, (15, client, gid, text)) return True # Proceed key = callback_data_list[0]["d"] report_answer(client=client, message=r_message, gid=gid, aid=aid, mid=r_message.message_id, action_type=action_type, key=key, reason=reason) return True except Exception as e: logger.warning(f"Report error: {e}", exc_info=True) finally: delete_message(client, gid, mid) return False
def config_directly(client: Client, message: Message) -> bool: # Config the bot directly if not message or not message.chat: return True # Basic data gid = message.chat.id mid = message.message_id try: # Check permission if not is_class_c(None, None, message): return True aid = message.from_user.id success = True reason = lang("config_updated") new_config = deepcopy(glovar.configs[gid]) text = f"{lang('admin_group')}{lang('colon')}{code(aid)}\n" # Check command format command_type, command_context = get_command_context(message) if command_type: if command_type == "show": text += f"{lang('action')}{lang('colon')}{code(lang('config_show'))}\n" text += get_config_text(new_config) thread(send_report_message, (30, client, gid, text)) return True now = get_now() if now - new_config["lock"] > 310: if command_type == "default": new_config = deepcopy(glovar.default_config) else: if command_context: if command_type in {"delete", "mention"}: if command_context == "off": new_config[command_type] = False elif command_context == "on": new_config[command_type] = True else: success = False reason = lang("command_para") elif command_type == "limit": limit = get_int(command_context) if 2 <= limit <= 5: new_config["limit"] = limit else: success = False reason = lang("command_para") elif command_type == "report": if not new_config.get("report"): new_config["report"] = {} if command_context == "off": new_config["report"]["auto"] = False new_config["report"]["manual"] = False elif command_context == "auto": new_config["report"]["auto"] = True new_config["report"]["manual"] = False elif command_context == "manual": new_config["report"]["auto"] = False new_config["report"]["manual"] = True elif command_context == "both": new_config["report"]["auto"] = True new_config["report"]["manual"] = True else: success = False reason = lang("command_para") else: success = False reason = lang("command_type") else: success = False reason = lang("command_lack") if success: new_config["default"] = False else: success = False reason = lang("config_locked") else: success = False reason = lang("command_usage") if success and new_config != glovar.configs[gid]: # Save new config glovar.configs[gid] = new_config save("configs") # Send debug message debug_text = get_debug_text(client, message.chat) debug_text += ( f"{lang('admin_group')}{lang('colon')}{code(message.from_user.id)}\n" f"{lang('action')}{lang('colon')}{code(lang('config_change'))}\n" f"{lang('more')}{lang('colon')}{code(f'{command_type} {command_context}')}\n" ) thread(send_message, (client, glovar.debug_channel_id, debug_text)) text += ( f"{lang('action')}{lang('colon')}{code(lang('config_change'))}\n" f"{lang('status')}{lang('colon')}{code(reason)}\n") thread(send_report_message, ((lambda x: 10 if x else 5)(success), client, gid, text)) return True except Exception as e: logger.warning(f"Config directly error: {e}", exc_info=True) finally: delete_message(client, gid, mid) return False
def config(client: Client, message: Message) -> bool: # Request CONFIG session if not message or not message.chat: return True # Basic data gid = message.chat.id mid = message.message_id try: # Check permission if not is_class_c(None, None, message): return True # Check command format command_type = get_command_type(message) if not command_type or not re.search(f"^{glovar.sender}$", command_type, re.I): return True now = get_now() # Check the config lock if now - glovar.configs[gid]["lock"] < 310: return True # Set lock glovar.configs[gid]["lock"] = now save("configs") # Ask CONFIG generate a config session group_name, group_link = get_group_info(client, message.chat) share_data(client=client, receivers=["CONFIG"], action="config", action_type="ask", data={ "project_name": glovar.project_name, "project_link": glovar.project_link, "group_id": gid, "group_name": group_name, "group_link": group_link, "user_id": message.from_user.id, "config": glovar.configs[gid], "default": glovar.default_config }) # Send debug message text = get_debug_text(client, message.chat) text += ( f"{lang('admin_group')}{lang('colon')}{code(message.from_user.id)}\n" f"{lang('action')}{lang('colon')}{code(lang('config_create'))}\n") thread(send_message, (client, glovar.debug_channel_id, text)) return True except Exception as e: logger.warning(f"Config error: {e}", exc_info=True) finally: if is_class_c(None, None, message): delay(3, delete_message, [client, gid, mid]) else: delete_message(client, gid, mid) return False
def interval_hour_01(client: Client) -> bool: # Execute every hour result = False glovar.locks["message"].acquire() try: # Clear old calling messages now = get_now() for gid in list(glovar.message_ids): mid, time = glovar.message_ids[gid] if not time: continue if now - time < 86400: continue glovar.message_ids[gid] = (0, 0) delete_message(client, gid, mid) save("message_ids") # Clear old reports now = get_now() for key in list(glovar.reports): report_record = glovar.reports[key] time = report_record["time"] if not time: glovar.reports.pop(key, {}) continue if now - time < 86400: continue gid = report_record["group_id"] mid = report_record["report_id"] thread(delete_message, (client, gid, mid)) glovar.reports.pop(key, {}) save("reports") # Clear user's waiting status reported_users = { glovar.reports[key]["user_id"] for key in glovar.reports } for uid in set(glovar.user_ids) - reported_users: glovar.user_ids[uid]["waiting"] = set() save("user_ids") result = True except Exception as e: logger.warning(f"Interval hour 01 error: {e}", exc_info=True) finally: glovar.locks["message"].release() return result