Skip to content

Commit

Permalink
Fixed concurrent !factorial calls
Browse files Browse the repository at this point in the history
  • Loading branch information
WiredMind2 committed Oct 15, 2024
1 parent abb355a commit d457b23
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions cogs/silly.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from utils.ids import COMMANDS


fact = 1

class Silly(cmds.Cog, name="Silly commands"):
"""
Silly commands
Expand All @@ -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):
Expand All @@ -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 :
Expand Down

0 comments on commit d457b23

Please sign in to comment.