Reputation: 23
So basically i am coding a discord bot in python. It is a bot where you can buy/sell things from a shop, and go out farming or mining for resources. The main bit i need help with is making the function rest for around 5 minutes. This bot will probably be used multiple times in 5 minutes, so i can't pause the whole code. obviously time.sleep will not work since it pauses the whole script. I have tried a few different things but they didn't work well and i couldn't find much else on the internet. My code is:
@client.command()
async def farm(ctx, item):
fakelocation = 11
await get_bank_data()
users = await get_bank_data()
if users[str(ctx.author.id)]["location"] == "Farm":
item = item.lower()
returnholder = 6
product = "none"
user = ctx.author
users = await get_bank_data()
if item == "potato":
product = "potato"
amount = random.randrange(2, 10)
await ctx.send("Your crops will be given to you at the end of the session!")
returnholder = 5
if item == "carrot":
product = "carrot"
amount = random.randrange(4, 12)
await ctx.send("Your crops will be given to you at the end of the session!")
returnholder = 5
if item == "wheat":
product = "wheat"
amount = random.randrange(7, 15)
await ctx.send("Your crops will be given to you at the end of the session!")
returnholder = 5
if item == "apple":
product = "apple"
amount = random.randrange(2, 13)
await ctx.send("Your crops will be given to you at the end of the session!")
returnholder = 5
if item == "banana":
product = "banana"
amount = random.randrange(4, 10)
await ctx.send("Your crops will be given to you at the end of the session!")
returnholder = 5
if item == "orange":
product = "orange"
amount = random.randrange(3, 8)
await ctx.send("Your crops will be given to you at the end of the session!")
returnholder = 5
if returnholder == 6:
await ctx.send("That crop does not exist here!")
return
try:
index = 0
t = None
for thing in users[str(user.id)]["bag"]:
n = thing["item"]
if n == item:
old_amt = thing["amount"]
new_amt = old_amt + amount
users[str(user.id)]["bag"][index]["amount"] = new_amt
t = 1
break
index += 1
if t == None:
obj = {"item": product, "amount": amount}
users[str(user.id)]["bag"].append(obj)
except:
obj = {"item": product, "amount": amount}
users[str(user.id)]["bag"] = [obj]
with open("mainbank.json", "w") as f:
json.dump(users, f)
fakelocation = 4
if fakelocation == 11:
await ctx.send("Please move to the Farm to farm!")
return
This is the code and it is relatively similar to the Mine function. it checks what the item is and then if it exists it will give you a random amount of items.I want to make it stop first thing in try:
since otherwise it will give you the items before the time is up. Thanks for any help!!
Upvotes: 1
Views: 110
Reputation: 23
import asyncio
await asyncio.sleep(SECONDS)
This was the answer for me. Thanks @TinNguyen for answering my question simply.
Upvotes: 1