[英]Is there a way that i can pause a function but not effect other functions in Python?
So basically i am coding a discord bot in python.所以基本上我在 python 中编码一个 discord 机器人。 It is a bot where you can buy/sell things from a shop, and go out farming or mining for resources.它是一个机器人,您可以在其中从商店购买/出售东西,并且 go 可以进行农业或采矿以获取资源。 The main bit i need help with is making the function rest for around 5 minutes.我需要帮助的主要一点是制作 function rest 大约 5 分钟。 This bot will probably be used multiple times in 5 minutes, so i can't pause the whole code.这个机器人可能会在 5 分钟内被多次使用,所以我不能暂停整个代码。 obviously time.sleep will not work since it pauses the whole script.显然 time.sleep 将不起作用,因为它会暂停整个脚本。 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.这是代码,它与我的 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.它会检查项目是什么,然后如果它存在,它会给你随机数量的项目。我想让它在try:
因为否则它会在时间结束之前给你项目。 Thanks for any help!!谢谢你的帮助!!
import asyncio
await asyncio.sleep(SECONDS)
This was the answer for me.这就是我的答案。 Thanks @TinNguyen for answering my question simply.感谢@TinNguyen 简单地回答我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.