Added loading fouras saved state

This commit is contained in:
Anselme FRANÇOIS 2023-07-27 23:17:34 +02:00
parent c3aad850a7
commit 908543fdb6
4 changed files with 32 additions and 9 deletions

View File

@ -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}")

View File

@ -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:

View File

@ -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))
)

View File

@ -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