add new module: tag-sub (allows to subscribe to events and get tagged once they happen)
This commit is contained in:
parent
2b8105d2a8
commit
2a9bd28d65
|
@ -0,0 +1,133 @@
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
|
||||||
|
def setup():
|
||||||
|
for i in ["sub-storage"]:
|
||||||
|
if not os.path.exists(module_path + i):
|
||||||
|
os.mkdir(module_path + i)
|
||||||
|
|
||||||
|
def load_group(group_name):
|
||||||
|
if os.path.exists(module_path + f"sub-storage/{group_name}.json"):
|
||||||
|
return json.loads(open(module_path + f"sub-storage/{group_name}.json").read())
|
||||||
|
else:
|
||||||
|
return {"hard": [], "soft": []}
|
||||||
|
|
||||||
|
def save_group(group_name, existing_group):
|
||||||
|
with open(module_path + f"sub-storage/{group_name}.json", "w") as f:
|
||||||
|
f.write(json.dumps(existing_group))
|
||||||
|
|
||||||
|
def sub_to_group(group_name, tag, persistence = "soft"):
|
||||||
|
for i in ["..", "/", "\\"]:
|
||||||
|
if i in group_name:
|
||||||
|
return False
|
||||||
|
|
||||||
|
existing_group = load_group(group_name)
|
||||||
|
|
||||||
|
if tag not in existing_group[persistence]:
|
||||||
|
existing_group[persistence].append(tag)
|
||||||
|
|
||||||
|
save_group(group_name, existing_group)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def unsub_from_group(group_name, tag, persistence = "soft"):
|
||||||
|
for i in ["..", "/", "\\"]:
|
||||||
|
if i in group_name:
|
||||||
|
return False
|
||||||
|
|
||||||
|
existing_group = load_group(group_name)
|
||||||
|
|
||||||
|
if tag in existing_group[persistence]:
|
||||||
|
existing_group[persistence].remove(tag)
|
||||||
|
|
||||||
|
save_group(group_name, existing_group)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def get_group(cmd):
|
||||||
|
if len(cmd) == 1:
|
||||||
|
return "default"
|
||||||
|
else:
|
||||||
|
return cmd[1]
|
||||||
|
|
||||||
|
def clear_subs_in(group_name, category = "soft"):
|
||||||
|
existing_group = load_group(group_name)
|
||||||
|
existing_group[category] = []
|
||||||
|
save_group(group_name, existing_group)
|
||||||
|
|
||||||
|
module_path = ""
|
||||||
|
setup()
|
||||||
|
|
||||||
|
def process(message, path):
|
||||||
|
global module_path
|
||||||
|
module_path = path
|
||||||
|
|
||||||
|
if not message.text[0] == "!":
|
||||||
|
return "", None
|
||||||
|
|
||||||
|
msg = message.text.lower()
|
||||||
|
cmd = [i for i in msg[1:].split(" ", 2) if i]
|
||||||
|
|
||||||
|
chosen_group_name = get_group(cmd)
|
||||||
|
|
||||||
|
if cmd[0] == "sub":
|
||||||
|
if message.from_user.username:
|
||||||
|
if sub_to_group(chosen_group_name, message.from_user.username):
|
||||||
|
return f"Subscribed {message.from_user.username} to the {chosen_group_name} event", None
|
||||||
|
else:
|
||||||
|
return f"Failed to subscribe {message.from_user.username} to the \"{chosen_group_name}\" event (event name violation)", None
|
||||||
|
else:
|
||||||
|
return f"Failed to subscribe {message.from_user.id} to the \"{chosen_group_name}\" event (no username available)", None
|
||||||
|
|
||||||
|
if cmd[0] == "hardsub":
|
||||||
|
if message.from_user.username:
|
||||||
|
if sub_to_group(chosen_group_name, message.from_user.username, persistence = "hard"):
|
||||||
|
return f"Hard-subscribed {message.from_user.username} to the {chosen_group_name} event", None
|
||||||
|
else:
|
||||||
|
return f"Failed to hard-subscribe {message.from_user.username} to the \"{chosen_group_name}\" event (event name violation)", None
|
||||||
|
else:
|
||||||
|
return f"Failed to hard-subscribe {message.from_user.id} to the \"{chosen_group_name}\" event (no username available)", None
|
||||||
|
|
||||||
|
elif cmd[0] == "unsub":
|
||||||
|
if message.from_user.username:
|
||||||
|
if unsub_from_group(chosen_group_name, message.from_user.username):
|
||||||
|
return f"Unsubscribed {message.from_user.username} from the {chosen_group_name} event", None
|
||||||
|
else:
|
||||||
|
return f"Failed to unsubscribe {message.from_user.username} from the \"{chosen_group_name}\" event (event name violation)", None
|
||||||
|
else:
|
||||||
|
return f"Failed to unsubscribe {message.from_user.id} from the \"{chosen_group_name}\" event (no username available)", None
|
||||||
|
|
||||||
|
elif cmd[0] == "hardunsub":
|
||||||
|
if message.from_user.username:
|
||||||
|
if unsub_from_group(chosen_group_name, message.from_user.username, persistence = "hard"):
|
||||||
|
return f"Unsubscribed {message.from_user.username} from the {chosen_group_name} event", None
|
||||||
|
else:
|
||||||
|
return f"Failed to unsubscribe {message.from_user.username} from the \"{chosen_group_name}\" event (event name violation)", None
|
||||||
|
else:
|
||||||
|
return f"Failed to unsubscribe {message.from_user.id} from the \"{chosen_group_name}\" event (no username available)", None
|
||||||
|
|
||||||
|
elif cmd[0] == "call":
|
||||||
|
group_data = load_group(chosen_group_name)
|
||||||
|
tags = set()
|
||||||
|
|
||||||
|
for subs in group_data.values():
|
||||||
|
tags |= set([f"@{i}" for i in subs if i])
|
||||||
|
|
||||||
|
ping_string = " ".join(tags)
|
||||||
|
clear_subs_in(chosen_group_name)
|
||||||
|
|
||||||
|
if len(cmd) == 3:
|
||||||
|
return f"{message.from_user.username} calls \"{chosen_group_name}\": {cmd[2]}\n{ping_string}", None
|
||||||
|
else:
|
||||||
|
return f"{message.from_user.username} calls \"{chosen_group_name}\"\n{ping_string}", None
|
||||||
|
|
||||||
|
|
||||||
|
elif cmd[0] == "sublist":
|
||||||
|
group_data = load_group(chosen_group_name)
|
||||||
|
responce = f"Subscribers for group \"{chosen_group_name}\":\n"
|
||||||
|
|
||||||
|
for name, subs in group_data.items():
|
||||||
|
responce += f"{name}: {', '.join(subs)}\n"
|
||||||
|
|
||||||
|
return responce, None
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"start_on_boot": true,
|
||||||
|
"alias": "tag-sub",
|
||||||
|
"version": 2,
|
||||||
|
"index_file": "index.py"
|
||||||
|
}
|
Loading…
Reference in New Issue