diff --git a/main.py b/main.py index b6ec64e..1eb6d49 100644 --- a/main.py +++ b/main.py @@ -29,7 +29,7 @@ client.modules: list[BaseModule] = [] async def on_ready(): client.modules = [m(client) for m in MODULES] for m in client.modules: - m.load() + await m.load() print(f"Logged in as {client.user}") diff --git a/modules/base.py b/modules/base.py index e520046..c3a4428 100644 --- a/modules/base.py +++ b/modules/base.py @@ -10,7 +10,10 @@ class BaseModule: def __init__(self, client): self._client = client - def load(self): + async def load(self): + raise NotImplementedError + + async def save(self, save_to_file=True): raise NotImplementedError async def handle_message(self, message)-> bool: diff --git a/modules/fouras.py b/modules/fouras.py index 3b9361e..5a5b017 100644 --- a/modules/fouras.py +++ b/modules/fouras.py @@ -46,14 +46,33 @@ ANSWERS_FILE = "answers.txt" SAVE_FILE = appdirs.user_data_dir() + "/PereFouras/fouras_riddles.json" class FourasModule(BaseModule): - def load(self): + async def load(self): with open(RIDDLES_FILE, "r", encoding=ENCODING) as r_file: self._client.riddles = r_file.read().split("\n\n") with open(ANSWERS_FILE, "r", encoding=ENCODING) as a_file: self._client.answers = [line.strip() for line in a_file.readlines()] - print(f"Loaded {len(self._client.riddles)} riddles") + str=f"Loaded {len(self._client.riddles)} riddles" - def save(self, save_to_file=True): + try: + with open(SAVE_FILE, "r") as file: + config = json.load(file) + ongoing_riddles = dict() + for k, v in config.items(): + channel = await self._client.fetch_channel(int(k)) + channel_info = v + channel_info["message"] = await channel.fetch_message(channel_info["message"]) + ongoing_riddles[channel] = channel_info + self._client.ongoing_riddles = ongoing_riddles + str = str + 'Loaded fouras save file "{0}"'.format(SAVE_FILE) + except FileNotFoundError: + str = str + 'No previous "{0}" save file found'.format(SAVE_FILE) + except json.JSONDecodeError: + str = str + '"{0}" is an invalid JSON file.'.format(SAVE_FILE) + + print(str) + return str + + async def save(self, save_to_file=True): dump = {} for key, value in self._client.ongoing_riddles.items(): dump_channel = dict(value) @@ -197,7 +216,7 @@ class FourasModule(BaseModule): if message_content == "load fouras": if(message.author.id == 151626081458192384): - self.load() + await self.load() await message.author.send( "Loaded {0} riddles".format(len(self._client.riddles)) ) diff --git a/modules/rhymes.py b/modules/rhymes.py index d85dcb7..5ac3fb9 100644 --- a/modules/rhymes.py +++ b/modules/rhymes.py @@ -18,10 +18,11 @@ class RhymesModule(BaseModule): rhymes: list = [] guild_config: dict = {} - def load(self): + async def load(self): str = "" with open(RHYMES_FILE, "r") as f: self.rhymes = json.load(f) + try: with open(SAVE_FILE, "r") as file: self.guild_config = json.load(file) @@ -33,7 +34,7 @@ class RhymesModule(BaseModule): print(str) return str - def save(self, save_to_file=True): + async def save(self, save_to_file=True): os.makedirs(os.path.dirname(SAVE_FILE), exist_ok=True) with open(SAVE_FILE, "w") as file: json.dump(self.guild_config, file, ensure_ascii=False, indent=2) @@ -85,7 +86,7 @@ class RhymesModule(BaseModule): if message_content == "load poilau": if(message.author.id == 151626081458192384): - await message.author.send(self.load()) + await message.author.send(await self.load()) json_str = "```json\n{0}```".format(json.dumps(self.guild_config, ensure_ascii=False, indent=2)) await message.author.send(json_str) return True