I'm trying to build a discord bot where there's a delay in between asking for an upgrade and the upgrade being received. With 2 upgrade slots, you should be able to fill both at the same time. However, it's not letting me add 2 delays at the same time (2 time.sleep()). On discord when I put the command for both functions one after the other, it takes 10 seconds to execute the first one and then another 10 seconds to execute the second, when I would preferably want it to take both simultaneously, meaning overall 10 seconds instead of 20. My code is below. Any ideas?
if message.content.startswith('fd!upgradefw'):
if message.author.id == [id]:
if Slots > 0:
await message.channel.send("Upgrading!")
Slots = Slots - 1
time.sleep(10)
await message.channel.send("Upgrade Complete!")
FWForli = FWForli 1
if message.content.startswith('fd!upgraderw'):
if message.author.id == [id]:
if Slots > 0:
await message.channel.send("Upgrading!")
Slots = Slots - 1
time.sleep(10)
await message.channel.send("Upgrade Complete!")
RWForli = RWForli 1
CodePudding user response:
As mentioned in my comment, I'm not familiar with discord.py, and all I'm seeing from your problem is this:
"I want to have two of my if statements execute at the same time". This means the 10 second delays would overlap, so it would only take 10 seconds in total.
Here's a relevant example:
from threading import Thread
from time import sleep
def check_delay_special(arg):
print("starting special!")
if arg == 'special':
sleep(10)
print("10 seconds have passed (special)!\n")
def check_delay_normal(arg):
print("starting normal!")
if arg == 'normal':
sleep(10)
print("10 seconds have passed (normal)!\n")
norm = Thread(target=check_delay_normal, args=('normal',)) # The args param is for passing args
spec = Thread(target=check_delay_special, args=('special',))
norm.start() # start the threads
spec.start()
norm.join() # block the calling thread (the main program) till these threads finish
spec.join()
# Output:
starting normal! # Both of these execute at roughly the same instant
starting special!
# there's a 10 second delay between these two
10 seconds have passed (special)! # Again, same instant execution
10 seconds have passed (normal)!
If you change the args to not satisfy the if function, the thread would terminate immediately. I recommend experimenting a bit to see how it works!
Of course, this is based on the assumption that you can pass whatever your if statements are checking to a function. If this is not what you were looking for or it does not work with discord, please leave a comment for this answer.
Here are similar/helpful questions on stackoverflow:
Multiprocessing vs Threading Python, How to do parallel programming in Python?, Parallel Programming - Python, How can I use threading in Python?, Python: Understanding Threading Module
Note that many of them use the multiprocessing module. Also, Python doesn't allow you to actually execute code in parallel with threading, but it should work well enough for your example.
CodePudding user response:
Try to use else statement instead of two if such as :
if message.content.startswith('fd!upgradefw'):
if message.author.id == [id]:
if Slots > 0:
await message.channel.send("Upgrading!")
Slots = Slots - 1
time.sleep(10)
await message.channel.send("Upgrade Complete!")
FWForli = FWForli 1
elif message.content.startswith('fd!upgraderw'): --> Here change it to elif
if message.author.id == [id]:
if Slots > 0:
await message.channel.send("Upgrading!")
Slots = Slots - 1
time.sleep(10)
await message.channel.send("Upgrade Complete!")
RWForli = RWForli 1
It should solve problem because if first condition satisfy then code will not reach second one and only 10s delay will be there but if both of them are if then it will check both of the code.
It had worked for me hope same will be for you.
