From 4558fb2e1d4c4811424e9fb59dd5467efb671f94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anselme=20FRAN=C3=87OIS?= Date: Tue, 11 Jul 2023 23:13:20 +0200 Subject: [PATCH] Fouras "poilau" feature is now less frequent, and more accurate --- modules/base.py | 4 + modules/fouras.py | 11 ++- modules/rhymes.py | 76 +++++++++++------- rhymes.json | 198 +++++++++++++++++++++++++++++++--------------- 4 files changed, 197 insertions(+), 92 deletions(-) diff --git a/modules/base.py b/modules/base.py index 5f0e647..2efcb1d 100644 --- a/modules/base.py +++ b/modules/base.py @@ -15,6 +15,10 @@ class BaseModule: async def handle_message(self, message): raise NotImplementedError + async def get_guild_name(self, guildId) -> str: + guild = await self._client.fetch_guild(guildId) + return "[Server={0}]".format(guild.name) + async def get_channel_name(self, channel) -> str: if isinstance(channel, discord.DMChannel): dm_channel = await self._client.fetch_channel(channel.id) diff --git a/modules/fouras.py b/modules/fouras.py index ec10e44..70dc411 100644 --- a/modules/fouras.py +++ b/modules/fouras.py @@ -3,6 +3,7 @@ import random import re import json from unidecode import unidecode +import appdirs API_URL = "".join( [ @@ -32,12 +33,15 @@ INVALID_ID = """ Numéro d'énigme invalide, merci de saisir un numéro entre 1 et {len} """ +RIDDLES_FILE = "riddles.txt" +ANSWERS_FILE = "answers.txt" +SAVE_FILE = appdirs.user_data_dir() + "/fouras_riddles.json" class FourasModule(BaseModule): def load(self): - with open("riddles.txt", "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") - with open("answers.txt", "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()] print(f"Loaded {len(self._client.riddles)} riddles") @@ -47,6 +51,9 @@ class FourasModule(BaseModule): dump_channel = dict(value) dump_channel["message"] = dump_channel["message"].id dump[key.id] = dump_channel + with open(SAVE_FILE, "w") as file: + json.dump(dump, file) + print('Saved fouras riddles state in file "{0}"'.format(SAVE_FILE)) return dump def new_riddle(self, channel, index): diff --git a/modules/rhymes.py b/modules/rhymes.py index 0138b5e..b11b34f 100644 --- a/modules/rhymes.py +++ b/modules/rhymes.py @@ -2,23 +2,32 @@ from .base import BaseModule import random import time import json +import appdirs -RHYMES_FILE = "rhymes.txt" +RHYMES_FILE = "rhymes.json" +SAVE_FILE = appdirs.user_data_dir() + "/poilau_save.json" class RhymesModule(BaseModule): - rhymes: dict = {} - cooldown: dict = {} + rhymes: list = [] + guild_config: dict = {} def load(self): - with open("rhymes.json", "r") as f: + 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) + print('Loaded poilau save file "{0}"'.format(SAVE_FILE)) + except FileNotFoundError: + print('No previous "{0}" save file found'.format(SAVE_FILE)) + except json.JSONDecodeError: + print('"{0}" is an invalid JSON file.'.format(SAVE_FILE)) def save(self): - dump = {} - for key, value in self.cooldown.items(): - dump[key.id] = value - time.time() - return dump + with open(SAVE_FILE, "w") as file: + json.dump(self.guild_config, file) + print('Saved poilau state in file "{0}"'.format(SAVE_FILE)) def get_last_word(self, ch: str) -> str: truncated = ch @@ -28,39 +37,50 @@ class RhymesModule(BaseModule): if truncated[-1].isalpha() and truncated[-2].isalpha(): break truncated = truncated[:-1] - return truncated + truncated = truncated.split(' ')[-1] + if truncated.isalpha(): + return truncated + else: + return "" def poil_auquel(self, ch: str) -> str: - for key in self.rhymes: - if ch.endswith(tuple(self.rhymes[key]["keys"])): - return random.choice(self.rhymes[key]["rhymes"]) + 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): message_content = message.content.lower() - if message_content == "debug": - dump = {} - for key, value in self.cooldown.items(): - channel_name = await self.get_channel_name(key) - dump[channel_name] = value - time.time() - await message.author.send( - "```json\n{0}```".format(json.dumps(dump, ensure_ascii=False, indent=4)) - ) + 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) + dump[channel_name] = value - time.time() + await message.author.send( + "```json\n{0}```".format(json.dumps(dump, ensure_ascii=False, indent=4)) + ) + return + + if message_content == "tg fouras" and message.guild: + self.guild_config[message.guild.id] = {43000,1.0} + await message.channel.send("ok :'(") return last_word = self.get_last_word(message_content) - if message.author != self._client.user and last_word: + if message.author != self._client.user and message.guild and last_word: poil = self.poil_auquel(last_word) - # cooldown = 0 - # if message.channel in self.cooldown: - # cooldown = self.cooldown[message.channel] - cooldown = self.cooldown.get(message.channel, 0) - - if poil and time.time() - cooldown > 0: + guild_config = self.guild_config.get(message.guild.id, {"cooldown": 0, "self-control": 1.0}) + if poil and time.time() - guild_config["cooldown"] > 0: + if random.random() < guild_config["self-control"]: + self.guild_config[message.guild.id]["self-control"] *= 0.9 + return wait_time = random.randint(0, 900) if bool(random.getrandbits(1)): wait_time = random.randint(900, 10800) - self.cooldown[message.channel] = time.time() + wait_time + self.guild_config[message.guild.id] = {"cooldown": time.time() + wait_time, "self-control": 1.0} await message.channel.send(poil) return diff --git a/rhymes.json b/rhymes.json index aa5e3a0..e602493 100644 --- a/rhymes.json +++ b/rhymes.json @@ -1,14 +1,53 @@ -{ - "E": { +[ + { + "sound": "E", + "blacklist": ["cheveux", "cheveu", "queue", "queues"], "keys": [ "eu", + "eus", "eux" ], "rhymes": [ - "Poil aux cheveux." + "Poil aux cheveux.", + "Poil à la queue." ] }, - "OUS": { + { + "sound": "OUB", + "blacklist": ["zboub", "zboubs"], + "keys": [ + "oub", + "oube", + "oob", + "oub" + ], + "rhymes": [ + "Poil aux zboub." + ] + }, + { + "sound": "ETTE", + "blacklist": ["quéquette", "quéquettes"], + "keys": [ + "ette", + "ett", + "èt", + "ète", + "ettes", + "etts", + "èts", + "ètes", + "êtes", + "aite", + "aites" + ], + "rhymes": [ + "Poil à la quéquette." + ] + }, + { + "sound": "OUS", + "blacklist": ["pouce", "pouces"], "keys": [ "ous", "ousse", @@ -20,15 +59,21 @@ "Poil au pouce." ] }, - "US": { + { + "sound": "US", + "blacklist": ["anus"], "keys": [ - "us" + "us", + "usse", + "usses" ], "rhymes": [ "Poil à l'anus." ] }, - "OU": { + { + "sound": "OU", + "blacklist": ["cou", "genou", "genous"], "keys": [ "ou", "où", @@ -39,12 +84,16 @@ "Poil au genou." ] }, - "O": { + { + "sound": "O", + "blacklist": ["dos"], "keys": [ "au", "aux", "o", "os", + "ot", + "ots", "oh", "ho" ], @@ -52,21 +101,26 @@ "Poil au dos." ] }, - "OL": { + { + "sound": "OL", + "blacklist": ["guiboles", "guibole"], "keys": [ "ol", "ols", "ole", "oles", "olle", - "ollesaul", + "olles", + "aul", "auls" ], "rhymes": [ "Poil aux guiboles." ] }, - "OUL": { + { + "sound": "OUL", + "blacklist": ["moule", "moules", "boule", "boules"], "keys": [ "oule", "oules", @@ -78,7 +132,9 @@ "Poil aux boules." ] }, - "ULVE": { + { + "sound": "ULVE", + "blacklist": ["vulve", "vulves"], "keys": [ "ulve", "ulves" @@ -87,85 +143,82 @@ "Poil à la vulve." ] }, - "OR": { + { + "sound": "IER", + "blacklist": ["derrière", "derrières"], "keys": [ - "or", - "ors", - "ore", - "ores", - "aur", - "aurs" - ], - "rhymes": [ - "Poil au corps." - ] - }, - "IER": { - "keys": [ - "ier", - "iers", - "ierre", - "ierres", - "ayé" + "èr", + "èrs", + "ère", + "ères", + "erre", + "erres", + "ert" ], "rhymes": [ "Poil au derrière." ] }, - "IEN": { + { + "sound": "IEN", + "blacklist": ["chien", "chiens"], "keys": [ "ien", - "iens", - "ient" + "iens" ], "rhymes": [ "Poil de chien." ] }, - "ET": { + { + "sound": "ET", + "blacklist": ["nez", "pieds", "pied"], "keys": [ "et", "é", - "è", - "ait", - "ais", - "ai", - "ez", - "ied", - "ieds" + "ez" ], "rhymes": [ "Poil au nez.", "Poil aux pieds." ] }, - "OI": { + { + "sound": "OI", + "blacklist": ["doigts", "doigt"], "keys": [ "oi", "oie", "ois", "oit", + "oix", "oa" ], "rhymes": [ "Poil aux doigts." ] }, - "AN": { + { + "sound": "AN", + "blacklist": ["dents", "dent"], "keys": [ - "an", "ant", "ants", - "en", "ment", - "ments" + "ments", + "rends", + "rents" ], "rhymes": [ "Poil aux dents. " ] }, - "ESSE": { + { + "sound": "ESSE", + "blacklist": ["fesses", "fesse"], "keys": [ + "ès", + "ess", "esse", "esses", "aisse", @@ -175,8 +228,11 @@ "Poil aux fesses." ] }, - "EL": { + { + "sound": "EL", + "blacklist": ["aisselles", "aisselle"], "keys": [ + "èl", "el", "els", "elle", @@ -188,17 +244,22 @@ "Poil aux aisselles." ] }, - "ACHE": { + { + "sound": "ACHE", + "blacklist": ["moustache", "moustaches"], "keys": [ "ache", "ach", - "ash" + "ash", + "asch" ], "rhymes": [ "Poil de moustache." ] }, - "IN": { + { + "sound": "IN", + "blacklist": ["main", "mains"], "keys": [ "ain", "ein" @@ -207,20 +268,23 @@ "Poil aux mains." ] }, - "TON": { + { + "sound": "TON", + "blacklist": ["menton", "mentons"], "keys": [ "ton", "tons", - "thons", + "thon", "thons" ], "rhymes": [ "Poil au menton." ] }, - "ON": { + { + "sound": "ON", + "blacklist": ["fion", "fions"], "keys": [ - "on", "ont", "onts", "ons", @@ -231,7 +295,9 @@ "Poil au fion." ] }, - "OUILLE": { + { + "sound": "OUILLE", + "blacklist": ["couille", "couilles"], "keys": [ "ouille", "ouilles" @@ -240,7 +306,9 @@ "Poil aux couilles." ] }, - "ATTE": { + { + "sound": "ATTE", + "blacklist": ["chatte", "chattes"], "keys": [ "atte", "ate", @@ -254,7 +322,9 @@ "Poil à la chatte." ] }, - "A": { + { + "sound": "A", + "blacklist": ["bras"], "keys": [ "a", "à", @@ -265,7 +335,9 @@ "Poil au bras." ] }, - "I": { + { + "sound": "I", + "blacklist": ["vessie", "zizi", "zizis"], "keys": [ "i", "is", @@ -277,7 +349,9 @@ "Poil au zizi." ] }, - "U": { + { + "sound": "U", + "blacklist": ["cul", "culs"], "keys": [ "u", "hu", @@ -287,4 +361,4 @@ "Poil au cul." ] } -} +]