from .base import BaseModule import random import time import json import appdirs import os RHYMES_FILE = "rhymes.json" SAVE_FILE = appdirs.user_data_dir() + "/PereFouras/poilau_save.json" # CONFIG_TEXT = """ # Ce bot a été développé par {user} # Code Source : https://git.epicsparrow.com/Anselme/perefouras # Ajouter ce bot à votre serveur : {url} # """ class RhymesModule(BaseModule): rhymes: list = [] guild_config: dict = {} 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) str = 'Loaded poilau save file "{0}"'.format(SAVE_FILE) except FileNotFoundError: str = 'No previous "{0}" save file found'.format(SAVE_FILE) except json.JSONDecodeError: str = '"{0}" is an invalid JSON file.'.format(SAVE_FILE) print(str) return str 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) print('Saved poilau state in file "{0}"'.format(SAVE_FILE)) def get_last_word(self, ch: str) -> str: truncated = ch while True: if len(truncated) < 2 or truncated[-1].isnumeric(): return "" if truncated[-1].isalpha() and truncated[-2].isalpha(): break truncated = truncated[:-1] truncated = truncated.split(" ")[-1] if truncated.isalpha(): return truncated else: return "" def poil_auquel(self, ch: str) -> str: for rhyme in self.rhymes: if ch in rhyme["blacklist"]: return "" if ch.endswith(tuple(rhyme["keys"])): return random.choice(rhyme["rhymes"]) return "" async def handle_message(self, message) -> bool: message_content = message.content.lower() if message_content == "debug poilau": if message.author.id == 151626081458192384: dump = {} for key, value in self.guild_config.items(): channel_name = await self.get_guild_name(key) sleeping_time = "{:.2f} s".format( max(0, value["cooldown"] - time.time()) ) dump[channel_name] = { "cooldown": sleeping_time, "self-control": value["self-control"], } await message.author.send( "```json\n{0}```".format( json.dumps(dump, ensure_ascii=False, indent=2) ) ) 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=2) ) await message.author.send(json_str) return True if message_content == "load poilau": if message.author.id == 151626081458192384: 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 if message_content == "tg fouras" and message.guild: self.guild_config[str(message.guild.id)] = { "cooldown": time.time() + 40000, "self-control": 2.0, } await message.channel.send("ok :'(") return True last_word = self.get_last_word(message_content) if message.author != self._client.user and message.guild and last_word: poil = self.poil_auquel(last_word) 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: self_control = guild_config["self-control"] if random.random() < self_control: guild_config["self-control"] = self_control * 0.9 self.guild_config[guildId] = guild_config return False wait_time = random.randint(0, 900) if bool(random.getrandbits(1)): wait_time = random.randint(900, 10800) self.guild_config[guildId] = { "cooldown": time.time() + wait_time, "self-control": self_control + 1.0, } await message.channel.send(poil) return True return False