简体   繁体   English

discord python bot获取用户输入

[英]discord python bot getting user input

I want to start a hangman game after .hm is called and the user then be able to enter in characters without command prefixes to play the game.我想在调用 .hm 之后开始一个刽子手游戏,然后用户可以输入没有命令前缀的字符来玩游戏。 I've looked at the documentation https://discordpy.readthedocs.io/en/latest/api.html#discord.Client.wait_for_message and tried to mimic user input checking but am unsuccessful.我查看了文档https://discordpy.readthedocs.io/en/latest/api.html#discord.Client.wait_for_message并尝试模仿用户输入检查但没有成功。 The last line before the code yields no output is: print('Running')代码没有输出前的最后一行是:print('Running')

@client.event
async def on_message(message):
   global Hangman, guessed, tries, guessed_letters, channel, word, word_completion, guessed_words

   if message.content.startswith('.hm'):
       Hangman = True
       print("hangman started")
       channel = message.channel
       word = get_word()
       word_completion = "_" * len(word)
       guessed = False
       guessed_letters = []
       guessed_words = []
       tries = 6

       await channel.send("Let's play hangman")
       await channel.send(display_hangman(tries))
       await channel.send(word_completion)

   elif message.content.startswith('.hm quit'):
       Hangman = False

   if Hangman:
       print(str(guessed) + str(tries))
       if not guessed and tries > 0:

           def check(m):
               if any(c.isalpha() for c in m.content) and len(str(m)) == 1:
                   return m.content == message

           print('Running')
           guess = await client.wait_for('message', check=check)
           print(str(guess))

           if len(str(guess)) == 1:
               if guess in guessed_letters:
                   await channel.send("You already guessed the letter", guess)
               elif guess not in word:
                   await channel.send(guess, "is not in the word.")
                   tries -= 1
                   guessed_letters.append(guess)
               else:
                   await channel.send("Good job,", guess, "is in the word!")
                   guessed_letters.append(guess)
                   word_as_list = list(word_completion)
                   indices = [i for i, letter in enumerate(word) if letter == guess]
                   for index in indices:
                       word_as_list[index] = guess
                   word_completion = "".join(word_as_list)
                   if "_" not in word_completion:
                    guessed = True
           elif len(guess) == len(word) and guess.isalpha():
               if guess in guessed_words:
                   await channel.send("You already guessed the word", guess)
               elif guess != word:
                   await channel.send(guess, "is not the word.")
                   tries -= 1
                   guessed_words.append(guess)
               else:
                   guessed = True
                   word_completion = word
           else:
               await channel.send("Not a valid guess.")
           await channel.send(display_hangman(tries))
           await channel.send(word_completion)

       if guessed:
           await channel.send("Congrats, you guessed the word! You win!")
       else:
           await channel.send("Sorry, you ran out of tries. The word was " + word + ". Maybe next time!")

get_word() just gets a random word from another .py file. get_word() 只是从另一个 .py 文件中获取一个随机单词。

guess = await client.wait_for('message', check=check)

it is for using in a command if you are using on_message then you can add in check.它用于在命令中使用,如果您使用on_message那么您可以添加检查。 if如果

def check(m):
  return m.channel = message.channel and m.author == message.author

now, it will work.现在,它将起作用。 you can add more checks if you want.如果需要,您可以添加更多检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM