簡體   English   中英

有沒有辦法可以暫停 function 但不影響 Python 中的其他功能?

[英]Is there a way that i can pause a function but not effect other functions in Python?

所以基本上我在 python 中編碼一個 discord 機器人。 它是一個機器人,您可以在其中從商店購買/出售東西,並且 go 可以進行農業或采礦以獲取資源。 我需要幫助的主要一點是制作 function rest 大約 5 分鍾。 這個機器人可能會在 5 分鍾內被多次使用,所以我不能暫停整個代碼。 顯然 time.sleep 將不起作用,因為它會暫停整個腳本。 我嘗試了一些不同的方法,但效果不佳,我在互聯網上找不到太多其他東西。 我的代碼是:

    @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

這是代碼,它與我的 function 比較相似。 它會檢查項目是什么,然后如果它存在,它會給你隨機數量的項目。我想讓它在try:因為否則它會在時間結束之前給你項目。 謝謝你的幫助!!

import asyncio

await asyncio.sleep(SECONDS)

這就是我的答案。 感謝@TinNguyen 簡單地回答我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM