From 4fdde9a9dc6d79f818e9a4c49834f35499ad5ebb Mon Sep 17 00:00:00 2001 From: bvn13 Date: Sun, 28 Jun 2026 18:25:02 +0300 Subject: [PATCH] Change !sub list/del/on/off to use line numbers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit !sub list now shows numbered lines; del/on/off operate by line number instead of internal rule id — simpler to use from chat. Co-Authored-By: Claude Sonnet 4.6 --- src/bot/bot.py | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/bot/bot.py b/src/bot/bot.py index 725bea8..4b9db26 100644 --- a/src/bot/bot.py +++ b/src/bot/bot.py @@ -143,17 +143,27 @@ class Bot(): return "Already exists: {0} [{1}] '{2}' -> {3}".format(src, ftype, value, dst) return "Added #{0}: {1}".format(rule['id'], rule.get('name', '')) if cmd in ('del', 'rm', 'delete'): - sub_id = int(parts[2]) - ok = self.__config.remove_subscription(sub_id) - return "Removed #{0}".format(sub_id) if ok else "Not found: #{0}".format(sub_id) + line_no = int(parts[2]) + subs = self.__config.list_subscriptions() + if line_no < 1 or line_no > len(subs): + return "No such line: {0} (total: {1})".format(line_no, len(subs)) + sub_id = subs[line_no - 1]['id'] + self.__config.remove_subscription(sub_id) + return "Removed line {0}".format(line_no) if cmd in ('on', 'enable'): - sub_id = int(parts[2]) - ok = self.__config.set_enabled(sub_id, True) - return "Enabled #{0}".format(sub_id) if ok else "Not found: #{0}".format(sub_id) + line_no = int(parts[2]) + subs = self.__config.list_subscriptions() + if line_no < 1 or line_no > len(subs): + return "No such line: {0} (total: {1})".format(line_no, len(subs)) + self.__config.set_enabled(subs[line_no - 1]['id'], True) + return "Enabled line {0}".format(line_no) if cmd in ('off', 'disable'): - sub_id = int(parts[2]) - ok = self.__config.set_enabled(sub_id, False) - return "Disabled #{0}".format(sub_id) if ok else "Not found: #{0}".format(sub_id) + line_no = int(parts[2]) + subs = self.__config.list_subscriptions() + if line_no < 1 or line_no > len(subs): + return "No such line: {0} (total: {1})".format(line_no, len(subs)) + self.__config.set_enabled(subs[line_no - 1]['id'], False) + return "Disabled line {0}".format(line_no) except (ValueError, IndexError): return self.__sub_help() return self.__sub_help() @@ -170,23 +180,24 @@ class Bot(): if not subs: return "No subscriptions" lines = [] - for s in subs: + for i, s in enumerate(subs, start=1): flt = s.get('filter', {}) act = s.get('action', {}) status = 'on' if s.get('enabled', True) else 'off' name = s.get('name', '') - lines.append("#{0} [{1}] {2} {3}:'{4}' -> {5}{6}".format( - s.get('id'), status, s.get('srcChatId'), - flt.get('type'), flt.get('value'), act.get('chatId'), - " ({0})".format(name) if name else "")) + lines.append("{0}. [{1}] {2}{3}".format( + i, status, + name if name else "{0} {1}:'{2}' -> {3}".format( + s.get('srcChatId'), flt.get('type'), flt.get('value'), act.get('chatId')), + "" if name else "")) return "\n".join(lines) def __sub_help(self) -> str: return ("Commands:\n" "!sub list\n" "!sub add \n" - "!sub del \n" - "!sub on | !sub off ") + "!sub del \n" + "!sub on | !sub off ") async def __send_all_chats_to_owner(self) -> None: text = ""