我想在我的机器人中输入一个命令,该命令在执行该响应命令之前会等待特定的响应。如果一分钟内未看到该响应,它将重置。
我认为为此需要时间模块,特别是time.sleep()。但是,在进行此操作时,发现time.sleep()在其整个持续时间内运行,并且直到完成后才能停止。考虑到这一点很有意义,后视是20/20。我尝试过的:
@commands.command()
@commands.guild_only()
async def challenge(self, ctx):
# opens and loads in player one's character sheet. This is done in this method solely because I'm unsure if
# loading json files in __init__ is actually a good idea. If I understand things correctly, things in a class's
# __init__ is ran EVERY TIME a method is called within it. If so, then the json files would be opened, loaded,
# and closed multiple times in a single run. Seems inefficient, and bad coding.
if self.game == 0:
player = str(ctx.message.author)
path = os.getcwd()
charFolder = os.path.join(path + "/characters/")
charFile = Path(charFolder + player + ".txt")
if not charFile.is_file():
await ctx.send("You don't even have a character made to fight. What are you planning to do? Emoji the"
"opponent to death? Make a character, first.")
elif self.game == 0.5:
await ctx.send("A challenge has already been issued.")
else:
self.pOneUsername = ctx.message.author
file = open(charFolder + player + ".txt", "r", encoding="utf-8")
charStats = json.load(file)
file.close()
self.pOneInfo = charStats
await ctx.send(self.pOneInfo['name'] + " has issued a challenge. Who accepts? (type !accept)")
self.game = 0.5
time.sleep(30)
await ctx.send("30 seconds to respond to challenge.")
time.sleep(20)
await ctx.send("10 seconds to respond to challenge.")
time.sleep(10)("closing challenge offered.")
self.game = 0
我希望游戏“等待”所需的时间,并且可以在此处运行,但是由于它处于休眠状态,因此直到分钟过去了,它都不会响应任何人输入“!accept”的命令来接受挑战,然后python self.game
已重设为0,从而使'!accept'命令无效。关于如何获得理想结果的任何想法或步骤?
!accept命令,如果需要的话:
player = str(ctx.message.author)
path = os.getcwd()
charFolder = os.path.join(path + "/characters/")
charFile = Path(charFolder + player + ".txt")
if not charFile.is_file():
await ctx.send("You don't even have a character made to fight. What are you planning to do? Emoji the "
"opponent to death? Make a character, first.")
elif ctx.message.author == self.pOneUsername:
await ctx.send("You can't fight yourself. This isn't Street Fighter.")
else:
self.game = 1
self.pTwoUsername = ctx.message.author
file = open(charFolder + player + ".txt", "r", encoding="utf-8")
charStats = json.load(file)
file.close()
self.pTwoInfo = charStats
self.pOneTotalHP = self.pOneInfo['hp']
self.pTwoTotalHP = self.pTwoInfo['hp']
self.pOneCurrentHP = self.pOneInfo['hp']
self.pTwoCurrentHP = self.pTwoInfo['hp']
self.pOneLevel = self.pOneInfo['level']
self.pTwoLevel = self.pTwoInfo['level']