From d457b233d73cdddb3709abaddee225fee485418b Mon Sep 17 00:00:00 2001 From: WiredMind2 Date: Tue, 15 Oct 2024 22:54:22 +0200 Subject: [PATCH] Fixed concurrent !factorial calls --- cogs/silly.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/cogs/silly.py b/cogs/silly.py index a0eae5b..f326899 100644 --- a/cogs/silly.py +++ b/cogs/silly.py @@ -8,8 +8,6 @@ from utils.ids import COMMANDS -fact = 1 - class Silly(cmds.Cog, name="Silly commands"): """ Silly commands @@ -19,18 +17,19 @@ def __init__(self, bot: CustomBot): self.bot = bot async def __factorial(self, message: discord.Message, nb: int): - global fact if nb < 0 : await message.channel.send("number cannot be negative !") elif nb > 20 : await message.channel.send(f"!factorial {nb-1}\njust joking, I'm not doing that") - elif nb <= 1 : - await message.channel.send(f"result : {fact}") + else: fact = 1 - else : - fact *= nb - await asyncio.sleep(1) - await message.channel.send(f"!factorial {nb-1}") + while nb > 1 : + await asyncio.sleep(1) + await message.channel.send(f"!factorial {nb-1}") + fact *= nb + nb -= 1 + + await message.channel.send(f"result : {fact}") return async def __di_cri(self, message: discord.Message): @@ -47,13 +46,6 @@ async def on_message(self, message: discord.Message) : """ Parse every message to find if there is a silly thing to answer """ - - # Self responding : - - if message.author == self.bot.user: - if re.fullmatch(r"^\!factorial [0-9]+$", message.content) is not None: - nb = int(message.content.split(' ')[-1]) - await self.__factorial(message, nb) # To prevent self response if message.author == self.bot.user :