From 49af236a1ccc2dcffb207533e4e398c6636ba249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anselme=20FRAN=C3=87OIS?= Date: Wed, 12 Jul 2023 00:52:42 +0200 Subject: [PATCH] Added "tg fouras", added fouras not being able to both answer with fouras and poilau module, fixed a few bugs with the news poilau cooldown --- main.py | 4 +++- modules/base.py | 2 +- modules/fouras.py | 48 ++++++++++++++++++++++++----------------------- modules/rhymes.py | 24 ++++++++++++++---------- 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/main.py b/main.py index 5cf4619..b6ec64e 100644 --- a/main.py +++ b/main.py @@ -47,8 +47,10 @@ async def on_message(message): if isinstance( message.channel, (discord.DMChannel, discord.TextChannel, discord.Thread) ): + handled = False for m in client.modules: - await m.handle_message(message) + if not handled: + handled = await m.handle_message(message) # Initialise le client diff --git a/modules/base.py b/modules/base.py index 2efcb1d..597b52c 100644 --- a/modules/base.py +++ b/modules/base.py @@ -12,7 +12,7 @@ class BaseModule: def load(self): raise NotImplementedError - async def handle_message(self, message): + async def handle_message(self, message)-> bool: raise NotImplementedError async def get_guild_name(self, guildId) -> str: diff --git a/modules/fouras.py b/modules/fouras.py index 70dc411..644fea3 100644 --- a/modules/fouras.py +++ b/modules/fouras.py @@ -117,9 +117,9 @@ class FourasModule(BaseModule): current_riddle["index"] + 1, formatted_riddle ) - async def handle_message(self, message): + async def handle_message(self, message) -> bool: if message.author == self._client.user: - return + return False message_content = message.content.lower() # command fouras @@ -135,14 +135,14 @@ class FourasModule(BaseModule): await message.channel.send( INVALID_ID.format(len=len(self._client.riddles)) ) - return + return True if message_content == "fouras": if random.random() <= 0.03: await message.channel.send("Non") elif len(self._client.riddles) > 0: index = random.randint(0, len(self._client.riddles) - 1) await message.channel.send(self.new_riddle(message.channel, index)) - return + return True if message_content.startswith("bug"): author_user = await self._client.fetch_user(MAINTAINER_ID) @@ -150,7 +150,7 @@ class FourasModule(BaseModule): BUG_REPORT.format(user=message.author.mention, message=message_content, json=self.save()) ) await message.channel.send(f'Rapport de bug envoyé à {author_user.mention}\nMerci de ton feedback !') - return + return True broadcast_match = re.match(r"^broadcast\s+(\d+) (.*)", message_content) if broadcast_match and message.author.id == MAINTAINER_ID: @@ -161,7 +161,7 @@ class FourasModule(BaseModule): await channel.send(broadcast_message) else: await message.channel.send(f'Invalid channel id : {index}') - return + return True # command reload if message_content == "reload_riddles": @@ -169,27 +169,28 @@ class FourasModule(BaseModule): await message.channel.send( "Loaded {0} riddles".format(len(self._client.riddles)) ) - return + return True if message_content == "about fouras": author_user = await self._client.fetch_user(MAINTAINER_ID) await message.channel.send( ABOUT.format(user=author_user.mention, url=API_URL) ) - return + return True - if message_content == "debug": - dump = {} - for key, value in self._client.ongoing_riddles.items(): - dump_channel = dict(value) - dump_channel.pop("message", None) - dump_channel.pop("answer", None) - channel_name = await self.get_channel_name(key) - dump[channel_name] = dump_channel - await message.author.send( - "```json\n{0}```".format(json.dumps(dump, ensure_ascii=False, indent=4)) - ) - return + if message_content == "debug fouras": + if(message.author.id == 151626081458192384): + dump = {} + for key, value in self._client.ongoing_riddles.items(): + dump_channel = dict(value) + dump_channel.pop("message", None) + dump_channel.pop("answer", None) + channel_name = await self.get_channel_name(key) + dump[channel_name] = dump_channel + await message.author.send( + "```json\n{0}```".format(json.dumps(dump, ensure_ascii=False, indent=4)) + ) + return True # if current channel has ongoing riddle if message.channel in self._client.ongoing_riddles: @@ -207,7 +208,7 @@ class FourasModule(BaseModule): content=self.format_message(current_riddle) ) self.finish_riddle(message.channel) - return + return True if ( message_content == "repete" @@ -216,7 +217,7 @@ class FourasModule(BaseModule): ): current_riddle.pop("message") await message.channel.send(self.format_message(current_riddle)) - return + return True # Commande /clue : révèle une lettre au hasard de la réponse attendue if ( @@ -237,4 +238,5 @@ class FourasModule(BaseModule): ) if nbClues >= len(answer): self.finish_riddle(message.channel) - return + return True + return False diff --git a/modules/rhymes.py b/modules/rhymes.py index b11b34f..9cceab9 100644 --- a/modules/rhymes.py +++ b/modules/rhymes.py @@ -51,7 +51,7 @@ class RhymesModule(BaseModule): return random.choice(rhyme["rhymes"]) return "" - async def handle_message(self, message): + async def handle_message(self, message) -> bool: message_content = message.content.lower() if message_content == "debug poilau": @@ -59,28 +59,32 @@ class RhymesModule(BaseModule): dump = {} for key, value in self.guild_config.items(): channel_name = await self.get_guild_name(key) - dump[channel_name] = value - time.time() + sleeping_time = "{:.2f} s".format(min(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=4)) ) - return + return True if message_content == "tg fouras" and message.guild: - self.guild_config[message.guild.id] = {43000,1.0} + self.guild_config[message.guild.id] = {"cooldown": time.time() + 40000, "self-control": 2.0} await message.channel.send("ok :'(") - return + 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) 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 + self_control = guild_config["self-control"] + if random.random() < self_control: + guild_config["self-control"] = self_control * 0.9 + self.guild_config[message.guild.id] = guild_config + return False wait_time = random.randint(0, 900) if bool(random.getrandbits(1)): wait_time = random.randint(900, 10800) - self.guild_config[message.guild.id] = {"cooldown": time.time() + wait_time, "self-control": 1.0} + self.guild_config[message.guild.id] = {"cooldown": time.time() + wait_time, "self-control": self_control + 1.0} await message.channel.send(poil) - return + return True + return False