2023-03-30 17:03:17 +00:00
|
|
|
import json
|
|
|
|
import os
|
2023-04-02 11:56:49 +00:00
|
|
|
from typing import Optional, Union
|
2023-03-30 17:03:17 +00:00
|
|
|
|
|
|
|
|
2023-04-24 06:21:04 +00:00
|
|
|
def get_macros_path(bot):
|
2023-04-05 10:10:18 +00:00
|
|
|
return os.path.join(bot.state_dir, "data/macros.json")
|
2023-03-30 17:03:17 +00:00
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
|
|
|
|
def get_macros_dict(bot) -> dict[str, dict[str, Union[list[str], str]]]:
|
2023-04-24 06:21:04 +00:00
|
|
|
if os.path.isfile(get_macros_path(bot)):
|
|
|
|
with open(get_macros_path(bot), "r") as f:
|
2023-04-02 11:56:49 +00:00
|
|
|
macros = json.load(f)
|
2023-03-30 17:03:17 +00:00
|
|
|
|
2023-04-02 11:56:49 +00:00
|
|
|
# Migration code
|
|
|
|
if "aliases" not in macros.keys():
|
|
|
|
new_macros = {"macros": macros, "aliases": {}}
|
|
|
|
unique_macros = set(new_macros["macros"].values())
|
|
|
|
for macro_text in unique_macros:
|
|
|
|
first_macro_key = ""
|
|
|
|
duplicate_num = 0
|
|
|
|
for key, macro in new_macros["macros"].copy().items():
|
|
|
|
if macro == macro_text and duplicate_num == 0:
|
|
|
|
first_macro_key = key
|
|
|
|
duplicate_num += 1
|
|
|
|
continue
|
|
|
|
elif macro == macro_text:
|
|
|
|
if first_macro_key not in new_macros["aliases"].keys():
|
|
|
|
new_macros["aliases"][first_macro_key] = []
|
|
|
|
new_macros["aliases"][first_macro_key].append(key)
|
|
|
|
del new_macros["macros"][key]
|
|
|
|
duplicate_num += 1
|
2023-03-30 17:03:17 +00:00
|
|
|
|
2023-04-24 06:21:04 +00:00
|
|
|
set_macros(bot, new_macros)
|
2023-04-02 11:56:49 +00:00
|
|
|
return new_macros
|
|
|
|
|
|
|
|
return macros
|
|
|
|
return {"macros": {}, "aliases": {}}
|
|
|
|
|
|
|
|
|
|
|
|
def is_macro_key_available(
|
2023-04-05 10:10:18 +00:00
|
|
|
bot, key: str, macros: dict[str, dict[str, Union[list[str], str]]] = None
|
2023-04-02 11:56:49 +00:00
|
|
|
) -> bool:
|
|
|
|
if macros is None:
|
2023-04-05 10:10:18 +00:00
|
|
|
macros = get_macros_dict(bot)
|
2023-04-02 11:56:49 +00:00
|
|
|
if key in macros["macros"].keys():
|
|
|
|
return False
|
|
|
|
for aliases in macros["aliases"].values():
|
|
|
|
if key in aliases:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def set_macros(bot, contents: dict[str, dict[str, Union[list[str], str]]]):
|
2023-04-24 06:21:04 +00:00
|
|
|
with open(get_macros_path(bot), "w") as f:
|
2023-03-30 17:03:17 +00:00
|
|
|
json.dump(contents, f)
|
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def get_macro(bot, key: str) -> Optional[str]:
|
|
|
|
macros = get_macros_dict(bot)
|
2023-03-30 17:03:17 +00:00
|
|
|
key = key.lower()
|
2023-04-02 11:56:49 +00:00
|
|
|
if key in macros["macros"].keys():
|
|
|
|
return macros["macros"][key]
|
|
|
|
for main_key, aliases in macros["aliases"].items():
|
|
|
|
if key in aliases:
|
|
|
|
return macros["macros"][main_key]
|
2023-03-30 17:03:17 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def add_macro(bot, key: str, message: str) -> bool:
|
|
|
|
macros = get_macros_dict(bot)
|
2023-03-30 17:03:17 +00:00
|
|
|
key = key.lower()
|
2023-04-05 10:10:18 +00:00
|
|
|
if is_macro_key_available(bot, key, macros):
|
2023-04-02 11:56:49 +00:00
|
|
|
macros["macros"][key] = message
|
2023-04-05 10:10:18 +00:00
|
|
|
set_macros(bot, macros)
|
2023-03-30 17:03:17 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def add_aliases(bot, key: str, aliases: list[str]) -> bool:
|
|
|
|
macros = get_macros_dict(bot)
|
2023-04-02 11:56:49 +00:00
|
|
|
key = key.lower()
|
|
|
|
success = False
|
|
|
|
if key in macros["macros"].keys():
|
|
|
|
for alias in aliases:
|
|
|
|
alias = alias.lower()
|
2023-04-05 10:10:18 +00:00
|
|
|
if is_macro_key_available(bot, alias, macros):
|
2023-04-26 18:38:52 +00:00
|
|
|
if key not in macros["aliases"].keys():
|
|
|
|
macros["aliases"][key] = []
|
2023-04-02 11:56:49 +00:00
|
|
|
macros["aliases"][key].append(alias)
|
|
|
|
success = True
|
|
|
|
if success:
|
2023-04-05 10:10:18 +00:00
|
|
|
set_macros(bot, macros)
|
2023-04-02 11:56:49 +00:00
|
|
|
return success
|
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def edit_macro(bot, key: str, message: str) -> bool:
|
|
|
|
macros = get_macros_dict(bot)
|
2023-03-30 17:03:17 +00:00
|
|
|
key = key.lower()
|
2023-04-02 11:56:49 +00:00
|
|
|
if key in macros["macros"].keys():
|
|
|
|
macros["macros"][key] = message
|
2023-04-05 10:10:18 +00:00
|
|
|
set_macros(bot, macros)
|
2023-03-30 17:03:17 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def remove_aliases(bot, key: str, aliases: list[str]) -> bool:
|
|
|
|
macros = get_macros_dict(bot)
|
2023-04-02 11:56:49 +00:00
|
|
|
key = key.lower()
|
|
|
|
success = False
|
|
|
|
if key not in macros["aliases"].keys():
|
|
|
|
return False
|
|
|
|
for alias in aliases:
|
|
|
|
alias = alias.lower()
|
|
|
|
if alias in macros["aliases"][key]:
|
|
|
|
macros["aliases"][key].remove(alias)
|
|
|
|
if len(macros["aliases"][key]) == 0:
|
|
|
|
del macros["aliases"][key]
|
|
|
|
success = True
|
|
|
|
if success:
|
2023-04-05 10:10:18 +00:00
|
|
|
set_macros(bot, macros)
|
2023-04-02 11:56:49 +00:00
|
|
|
return success
|
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def remove_macro(bot, key: str) -> bool:
|
|
|
|
macros = get_macros_dict(bot)
|
2023-04-02 11:56:49 +00:00
|
|
|
key = key.lower()
|
|
|
|
if key in macros["macros"].keys():
|
|
|
|
del macros["macros"][key]
|
2023-04-05 10:10:18 +00:00
|
|
|
set_macros(bot, macros)
|
2023-04-02 11:56:49 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def clear_aliases(bot, key: str) -> bool:
|
|
|
|
macros = get_macros_dict(bot)
|
2023-03-30 17:03:17 +00:00
|
|
|
key = key.lower()
|
2023-04-02 11:56:49 +00:00
|
|
|
if key in macros["macros"].keys() and key in macros["aliases"].keys():
|
|
|
|
del macros["aliases"][key]
|
2023-04-05 10:10:18 +00:00
|
|
|
set_macros(bot, macros)
|
2023-03-30 17:03:17 +00:00
|
|
|
return True
|
|
|
|
return False
|