Change !sub list/del/on/off to use line numbers
!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 <noreply@anthropic.com>
This commit is contained in:
parent
51bd0b4b03
commit
4fdde9a9dc
@ -143,17 +143,27 @@ class Bot():
|
|||||||
return "Already exists: {0} [{1}] '{2}' -> {3}".format(src, ftype, value, dst)
|
return "Already exists: {0} [{1}] '{2}' -> {3}".format(src, ftype, value, dst)
|
||||||
return "Added #{0}: {1}".format(rule['id'], rule.get('name', ''))
|
return "Added #{0}: {1}".format(rule['id'], rule.get('name', ''))
|
||||||
if cmd in ('del', 'rm', 'delete'):
|
if cmd in ('del', 'rm', 'delete'):
|
||||||
sub_id = int(parts[2])
|
line_no = int(parts[2])
|
||||||
ok = self.__config.remove_subscription(sub_id)
|
subs = self.__config.list_subscriptions()
|
||||||
return "Removed #{0}".format(sub_id) if ok else "Not found: #{0}".format(sub_id)
|
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'):
|
if cmd in ('on', 'enable'):
|
||||||
sub_id = int(parts[2])
|
line_no = int(parts[2])
|
||||||
ok = self.__config.set_enabled(sub_id, True)
|
subs = self.__config.list_subscriptions()
|
||||||
return "Enabled #{0}".format(sub_id) if ok else "Not found: #{0}".format(sub_id)
|
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'):
|
if cmd in ('off', 'disable'):
|
||||||
sub_id = int(parts[2])
|
line_no = int(parts[2])
|
||||||
ok = self.__config.set_enabled(sub_id, False)
|
subs = self.__config.list_subscriptions()
|
||||||
return "Disabled #{0}".format(sub_id) if ok else "Not found: #{0}".format(sub_id)
|
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):
|
except (ValueError, IndexError):
|
||||||
return self.__sub_help()
|
return self.__sub_help()
|
||||||
return self.__sub_help()
|
return self.__sub_help()
|
||||||
@ -170,23 +180,24 @@ class Bot():
|
|||||||
if not subs:
|
if not subs:
|
||||||
return "No subscriptions"
|
return "No subscriptions"
|
||||||
lines = []
|
lines = []
|
||||||
for s in subs:
|
for i, s in enumerate(subs, start=1):
|
||||||
flt = s.get('filter', {})
|
flt = s.get('filter', {})
|
||||||
act = s.get('action', {})
|
act = s.get('action', {})
|
||||||
status = 'on' if s.get('enabled', True) else 'off'
|
status = 'on' if s.get('enabled', True) else 'off'
|
||||||
name = s.get('name', '')
|
name = s.get('name', '')
|
||||||
lines.append("#{0} [{1}] {2} {3}:'{4}' -> {5}{6}".format(
|
lines.append("{0}. [{1}] {2}{3}".format(
|
||||||
s.get('id'), status, s.get('srcChatId'),
|
i, status,
|
||||||
flt.get('type'), flt.get('value'), act.get('chatId'),
|
name if name else "{0} {1}:'{2}' -> {3}".format(
|
||||||
" ({0})".format(name) if name else ""))
|
s.get('srcChatId'), flt.get('type'), flt.get('value'), act.get('chatId')),
|
||||||
|
"" if name else ""))
|
||||||
return "\n".join(lines)
|
return "\n".join(lines)
|
||||||
|
|
||||||
def __sub_help(self) -> str:
|
def __sub_help(self) -> str:
|
||||||
return ("Commands:\n"
|
return ("Commands:\n"
|
||||||
"!sub list\n"
|
"!sub list\n"
|
||||||
"!sub add <src_chat_id> <dst_chat_id> <contain|regexp> <value>\n"
|
"!sub add <src_chat_id> <dst_chat_id> <contain|regexp> <value>\n"
|
||||||
"!sub del <id>\n"
|
"!sub del <line>\n"
|
||||||
"!sub on <id> | !sub off <id>")
|
"!sub on <line> | !sub off <line>")
|
||||||
|
|
||||||
async def __send_all_chats_to_owner(self) -> None:
|
async def __send_all_chats_to_owner(self) -> None:
|
||||||
text = ""
|
text = ""
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user