man子手代码在中途停止工作

时间:2020-11-03 02:46:50

标签: python discord.py discord.py-rewrite

我要做什么:使用discord.py-rewrite制作Make子手游戏

我的问题:僵尸程序发送(水果类别)或(动物类别)等后,代码停止工作。也没有错误消息。我尝试将def更改为async def以尝试使用await ctx.send,但无济于事。

@client.command()
async def hangman(ctx, category):
    fruits = ['pear', 'banana', 'apple']
    animals = ['cat', 'horse', 'turtle']
    userGuesslist = []
    userGuesses = []
    playGame = True
    category = category
    continueGame = "Y"
    name = ctx.message.author.mention

    await ctx.send(f"Welcome to hangman {name}!")
    time.sleep(2)
    await ctx.send("Your Objective:tm:: Find my secret word from the category you chose")
    time.sleep(2)
    await ctx.send("Don't forget that you can only guess with letters!")
    time.sleep(2)
    await ctx.send(f"Good luck {name}!")

    while True:
        while True:
            if category.upper() == 'F':
                await ctx.send("__**Fruits Category**__")
                secretWord = random.choice(fruits)
                break
            elif category.upper() == 'A':
                await ctx.send("__**Animals Category**__")
                secretWord = random.choice(animals)
                break
            else:
                await ctx.send("__**Random Category**__")
                secretWord = random.choice(fruits, animals)
                break

        if playGame:
            secretWordList = list(secretWord)
            attempts = (len(secretWord) + 2)

            def pGuessedLetter():
                ("Your Secret word is: "+ ''.join(userGuessList))

            for n in secretWordList:
                userGuesslits.append('\_ ')
            pGuessedLetter()

            await ctx.send(f"Number of allowed guesses for this word: {attempts}")

            while True:
                await ctx.send("**Guess a letter:**")
                def check(m):
                    return m.content and user == ctx.message.author
                letter = check

                if letter in userGuesses:
                    await ctx.send("You already guessed this letter, try something else")
                else:
                    attempts -= 1
                    userGuesses.append(letter)
                    if letter in secretWordList:
                        await ctx.send("Nice guess!")
                        if attemps > 0:
                            await ctt.send(f"Attempts left: {attempts}")
                        for i in range(len(secretWordList)):
                            if letter == secretWordList[i]:
                                letterIndex = i
                                userGuesslits[letterIndex] = letter.upper()
                        pGuessedLetter()

                    else:
                        await ctx.send("Nope, try again :eye: :eye:")
                        if attempts > 0:
                            await ctx.send(f"Attempts left: {attempts}")
                        pGuessedLetter()

                joinedList = ''.join(userGuesslist)
                if joinedList.upper() == secretWord.upper():
                    await ctx.send(f"You got it {name}! The word was {secretWord}")
                    break
                elif attempts == 0:
                    await ctx.send(f"Too many Guesses! Better luck next time {name}")
                    await ctx.send(f"The secret word was {secretWord.upper()}")
                    break

@hangman.error
async def hangman_error(ctx, error):
    if isinstance(error, discord.ext.commands.errors.MissingRequiredArgument):
        await ctx.send("""Ensure you include a category:
**Example:** `bl!hangman A`
`F - Fruits`
`A - Animals`
`X - Cancel`
""")

1 个答案:

答案 0 :(得分:0)

我不建议使用while循环!

改为使用await client.wait_for('message')。 (https://discordpy.readthedocs.io/en/latest/api.html?highlight=wait_for#discord.Client.wait_for