等待响应,直到

2024-10-03 04:33:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想输入一个命令到我的机器人,等待一个特定的响应,然后再执行该响应命令。如果一分钟内看不到该响应,它将重置

我认为时间模块是必需的,特别是time.sleep()。但是,在玩这个游戏时,发现time.sleep()会在整个时间段内运行,并且在完成之前不能停止。当你仔细想想,事后诸葛亮是有道理的。我试过:

@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,使'!“接受”命令无效。关于如何得到想要的结果有什么想法或步骤吗

那个!接受命令(如果需要):

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']

Tags: topathinselfsendgametimeis