Added loading fouras saved state
This commit is contained in:
parent
c3aad850a7
commit
908543fdb6
2
main.py
2
main.py
@ -29,7 +29,7 @@ client.modules: list[BaseModule] = []
|
|||||||
async def on_ready():
|
async def on_ready():
|
||||||
client.modules = [m(client) for m in MODULES]
|
client.modules = [m(client) for m in MODULES]
|
||||||
for m in client.modules:
|
for m in client.modules:
|
||||||
m.load()
|
await m.load()
|
||||||
print(f"Logged in as {client.user}")
|
print(f"Logged in as {client.user}")
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,7 +10,10 @@ class BaseModule:
|
|||||||
def __init__(self, client):
|
def __init__(self, client):
|
||||||
self._client = client
|
self._client = client
|
||||||
|
|
||||||
def load(self):
|
async def load(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
async def save(self, save_to_file=True):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
async def handle_message(self, message)-> bool:
|
async def handle_message(self, message)-> bool:
|
||||||
|
@ -46,14 +46,33 @@ ANSWERS_FILE = "answers.txt"
|
|||||||
SAVE_FILE = appdirs.user_data_dir() + "/PereFouras/fouras_riddles.json"
|
SAVE_FILE = appdirs.user_data_dir() + "/PereFouras/fouras_riddles.json"
|
||||||
|
|
||||||
class FourasModule(BaseModule):
|
class FourasModule(BaseModule):
|
||||||
def load(self):
|
async def load(self):
|
||||||
with open(RIDDLES_FILE, "r", encoding=ENCODING) as r_file:
|
with open(RIDDLES_FILE, "r", encoding=ENCODING) as r_file:
|
||||||
self._client.riddles = r_file.read().split("\n\n")
|
self._client.riddles = r_file.read().split("\n\n")
|
||||||
with open(ANSWERS_FILE, "r", encoding=ENCODING) as a_file:
|
with open(ANSWERS_FILE, "r", encoding=ENCODING) as a_file:
|
||||||
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")
|
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 = {}
|
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)
|
||||||
@ -197,7 +216,7 @@ class FourasModule(BaseModule):
|
|||||||
|
|
||||||
if message_content == "load fouras":
|
if message_content == "load fouras":
|
||||||
if(message.author.id == 151626081458192384):
|
if(message.author.id == 151626081458192384):
|
||||||
self.load()
|
await self.load()
|
||||||
await message.author.send(
|
await message.author.send(
|
||||||
"Loaded {0} riddles".format(len(self._client.riddles))
|
"Loaded {0} riddles".format(len(self._client.riddles))
|
||||||
)
|
)
|
||||||
|
@ -18,10 +18,11 @@ class RhymesModule(BaseModule):
|
|||||||
rhymes: list = []
|
rhymes: list = []
|
||||||
guild_config: dict = {}
|
guild_config: dict = {}
|
||||||
|
|
||||||
def load(self):
|
async def load(self):
|
||||||
str = ""
|
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)
|
||||||
@ -33,7 +34,7 @@ class RhymesModule(BaseModule):
|
|||||||
print(str)
|
print(str)
|
||||||
return 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)
|
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, ensure_ascii=False, indent=2)
|
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_content == "load poilau":
|
||||||
if(message.author.id == 151626081458192384):
|
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))
|
json_str = "```json\n{0}```".format(json.dumps(self.guild_config, ensure_ascii=False, indent=2))
|
||||||
await message.author.send(json_str)
|
await message.author.send(json_str)
|
||||||
return True
|
return True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user