perefouras/modules/rhymes.py
2023-06-17 16:20:30 +02:00

51 lines
1.6 KiB
Python

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 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()
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