1

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!!

ACKtbt
  • 23
  • 7
  • https://stackoverflow.com/questions/92928/time-sleep-sleeps-thread-or-process – Niteya Shah Mar 30 '21 at 11:05
  • previous comment is a lot of help. but the concept is that You thread the process and in the definition of that process put in the time to sleep – Matiiss Mar 30 '21 at 11:20
  • @Matiiss How can i thread this process and make it work? sorry i am quite new to Python and i do not understand some of this complex stuff that i am looking at. – ACKtbt Mar 30 '21 at 11:34
  • Welcome to stackoverflow. You have to include multiple threads in your program. Be aware that threading is an advanced topic. – Molitoris Mar 30 '21 at 12:40
  • 2
    He's already using async. @ACKtbt do `import asyncio; await asyncio.sleep(SECONDS)` It is asynchron and does not halt your entire program. – Tin Nguyen Mar 30 '21 at 14:29
  • I suggest You take a look at some tutorials: I recommend this one: https://www.youtube.com/watch?v=fKl2JW_qrso or a similar one about threading (also from Corey Schafer) – Matiiss Mar 30 '21 at 18:03
  • Yes thanks @TinNguyen that worked for me. – ACKtbt Mar 30 '21 at 23:00
  • "making the function rest for around 5 minutes." What does this actually mean? Are you trying to delay the response when the command is used? Or implement a restriction on using the command frequently? In the latter case, if someone tries to use the command before the 5 minutes have elapsed, what should happen? An error message? Silently ignoring it? Remembering the command and running it later? Something else? – Karl Knechtel Mar 30 '21 at 23:07
  • @KarlKnechtel what i want to do is make a function where you farm or mine, and after 5 minutes is up you get the resources in your bag (which is stored in a json file) without putting the whole code to sleep. I have already added a cooldown so you cant do it multiple times in that 5 minutes since that would then make it almost useless making it sleep. Tin Nguyen gave me a simple answer that worked. – ACKtbt Mar 31 '21 at 06:14

1 Answers1

1
import asyncio

await asyncio.sleep(SECONDS)

This was the answer for me. Thanks @TinNguyen for answering my question simply.

ACKtbt
  • 23
  • 7