Rhymes module state can now be saved and loaded, and fouras module state can be saved

This commit is contained in:
Anselme FRANÇOIS 2023-07-12 01:39:44 +02:00
parent 49af236a1c
commit 86efad97c5
2 changed files with 51 additions and 17 deletions

View File

@ -4,6 +4,7 @@ import re
import json import json
from unidecode import unidecode from unidecode import unidecode
import appdirs import appdirs
import os
API_URL = "".join( API_URL = "".join(
[ [
@ -35,7 +36,7 @@ Numéro d'énigme invalide, merci de saisir un numéro entre 1 et {len}
RIDDLES_FILE = "riddles.txt" RIDDLES_FILE = "riddles.txt"
ANSWERS_FILE = "answers.txt" ANSWERS_FILE = "answers.txt"
SAVE_FILE = appdirs.user_data_dir() + "/fouras_riddles.json" SAVE_FILE = appdirs.user_data_dir() + "/PereFouras/fouras_riddles.json"
class FourasModule(BaseModule): class FourasModule(BaseModule):
def load(self): def load(self):
@ -45,12 +46,14 @@ class FourasModule(BaseModule):
self._client.answers = [line.strip() for line in a_file.readlines()] self._client.answers = [line.strip() for line in a_file.readlines()]
print(f"Loaded {len(self._client.riddles)} riddles") print(f"Loaded {len(self._client.riddles)} riddles")
def save(self): def save(self):
dump = {} dump = {}
for key, value in self._client.ongoing_riddles.items(): for key, value in self._client.ongoing_riddles.items():
dump_channel = dict(value) dump_channel = dict(value)
dump_channel["message"] = dump_channel["message"].id dump_channel["message"] = dump_channel["message"].id
dump[key.id] = dump_channel dump[key.id] = dump_channel
os.makedirs(os.path.dirname(SAVE_FILE), exist_ok=True)
with open(SAVE_FILE, "w") as file: with open(SAVE_FILE, "w") as file:
json.dump(dump, file) json.dump(dump, file)
print('Saved fouras riddles state in file "{0}"'.format(SAVE_FILE)) print('Saved fouras riddles state in file "{0}"'.format(SAVE_FILE))
@ -163,14 +166,6 @@ class FourasModule(BaseModule):
await message.channel.send(f'Invalid channel id : {index}') await message.channel.send(f'Invalid channel id : {index}')
return True return True
# command reload
if message_content == "reload_riddles":
self.load()
await message.channel.send(
"Loaded {0} riddles".format(len(self._client.riddles))
)
return True
if message_content == "about fouras": if message_content == "about fouras":
author_user = await self._client.fetch_user(MAINTAINER_ID) author_user = await self._client.fetch_user(MAINTAINER_ID)
await message.channel.send( await message.channel.send(
@ -178,6 +173,20 @@ class FourasModule(BaseModule):
) )
return True return True
if message_content == "save fouras":
if(message.author.id == 151626081458192384):
json_str = "```json\n{0}```".format(json.dumps(self.save(), ensure_ascii=False, indent=4))
await message.author.send(json_str)
return True
if message_content == "load fouras":
if(message.author.id == 151626081458192384):
self.load()
await message.author.send(
"Loaded {0} riddles".format(len(self._client.riddles))
)
return True
if message_content == "debug fouras": if message_content == "debug fouras":
if(message.author.id == 151626081458192384): if(message.author.id == 151626081458192384):
dump = {} dump = {}

View File

@ -3,28 +3,38 @@ import random
import time import time
import json import json
import appdirs import appdirs
import os
RHYMES_FILE = "rhymes.json" RHYMES_FILE = "rhymes.json"
SAVE_FILE = appdirs.user_data_dir() + "/poilau_save.json" SAVE_FILE = appdirs.user_data_dir() + "/PereFouras/poilau_save.json"
# CONFIG_TEXT = """
# Ce bot a été développé par {user}
# Code Source : https://gitlab.epicsparrow.com/Anselme/perefouras
# Ajouter ce bot à votre serveur : {url}
# """
class RhymesModule(BaseModule): class RhymesModule(BaseModule):
rhymes: list = [] rhymes: list = []
guild_config: dict = {} guild_config: dict = {}
def load(self): def load(self):
str = ""
with open(RHYMES_FILE, "r") as f: with open(RHYMES_FILE, "r") as f:
self.rhymes = json.load(f) self.rhymes = json.load(f)
try: try:
with open(SAVE_FILE, "r") as file: with open(SAVE_FILE, "r") as file:
self.guild_config = json.load(file) self.guild_config = json.load(file)
print('Loaded poilau save file "{0}"'.format(SAVE_FILE)) str = 'Loaded poilau save file "{0}"'.format(SAVE_FILE)
except FileNotFoundError: except FileNotFoundError:
print('No previous "{0}" save file found'.format(SAVE_FILE)) str = 'No previous "{0}" save file found'.format(SAVE_FILE)
except json.JSONDecodeError: except json.JSONDecodeError:
print('"{0}" is an invalid JSON file.'.format(SAVE_FILE)) str = '"{0}" is an invalid JSON file.'.format(SAVE_FILE)
print(str)
return str
def save(self): def save(self):
os.makedirs(os.path.dirname(SAVE_FILE), exist_ok=True)
with open(SAVE_FILE, "w") as file: with open(SAVE_FILE, "w") as file:
json.dump(self.guild_config, file) json.dump(self.guild_config, file)
print('Saved poilau state in file "{0}"'.format(SAVE_FILE)) print('Saved poilau state in file "{0}"'.format(SAVE_FILE))
@ -66,25 +76,40 @@ class RhymesModule(BaseModule):
) )
return True return True
if message_content == "save poilau":
if(message.author.id == 151626081458192384):
self.save()
json_str = "```json\n{0}```".format(json.dumps(self.guild_config, ensure_ascii=False, indent=4))
await message.author.send(json_str)
return True
if message_content == "load poilau":
if(message.author.id == 151626081458192384):
await message.author.send(self.load())
json_str = "```json\n{0}```".format(json.dumps(self.guild_config, ensure_ascii=False, indent=4))
await message.author.send(json_str)
return True
if message_content == "tg fouras" and message.guild: if message_content == "tg fouras" and message.guild:
self.guild_config[message.guild.id] = {"cooldown": time.time() + 40000, "self-control": 2.0} self.guild_config[str(message.guild.id)] = {"cooldown": time.time() + 40000, "self-control": 2.0}
await message.channel.send("ok :'(") await message.channel.send("ok :'(")
return True return True
last_word = self.get_last_word(message_content) last_word = self.get_last_word(message_content)
if message.author != self._client.user and message.guild and last_word: if message.author != self._client.user and message.guild and last_word:
poil = self.poil_auquel(last_word) poil = self.poil_auquel(last_word)
guild_config = self.guild_config.get(message.guild.id, {"cooldown": 0, "self-control": 1.0}) guildId = str(message.guild.id)
guild_config = self.guild_config.get(guildId, {"cooldown": 0, "self-control": 1.0})
if poil and time.time() - guild_config["cooldown"] > 0: if poil and time.time() - guild_config["cooldown"] > 0:
self_control = guild_config["self-control"] self_control = guild_config["self-control"]
if random.random() < self_control: if random.random() < self_control:
guild_config["self-control"] = self_control * 0.9 guild_config["self-control"] = self_control * 0.9
self.guild_config[message.guild.id] = guild_config self.guild_config[guildId] = guild_config
return False return False
wait_time = random.randint(0, 900) wait_time = random.randint(0, 900)
if bool(random.getrandbits(1)): if bool(random.getrandbits(1)):
wait_time = random.randint(900, 10800) wait_time = random.randint(900, 10800)
self.guild_config[message.guild.id] = {"cooldown": time.time() + wait_time, "self-control": self_control + 1.0} self.guild_config[guildId] = {"cooldown": time.time() + wait_time, "self-control": self_control + 1.0}
await message.channel.send(poil) await message.channel.send(poil)
return True return True
return False return False