from .base import BaseModule import random import time import json RHYMES_FILE = "rhymes.txt" class RhymesModule(BaseModule): rhymes: dict = {} cooldown: dict = {} def load(self): with open("rhymes.json", "r") as f: self.rhymes = json.load(f) def save(self): dump = {} for key, value in self.cooldown.items(): dump[key.id] = value - time.time() return dump 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] return truncated 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"]) 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)) ) return last_word = self.get_last_word(message_content) if message.author != self._client.user 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: 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 await message.channel.send(poil) return