@client.event
async def on_message(message):
print(message.author.bot)
content = message.content
author = message.author
print("{}: {}".format(author, content))
if message.content.startswith(".play"):
import random
words = [
"expert",
"error",
"fairies",
"star",
"example",
"pencil",
"friction",
"attraction",
"horse",
"vegetable",
"stove",
"invention"]
word = random.choice(words)
guesses = 7
hint = ""
i = 0
try:
await message.channel.send("Guess the word!")
while guesses > 0:
user_input = await client.wait_for('message', timeout=60.0, check=None)
user_input = message.content
print(user_input)
if user_input == word:
await message.channel.send("You found the word!")
elif user_input != word:
await message.channel.send("Guessed wrong!")
try:
hint += word[i]
except IndexError:
break
i += 1
await message.channel.send(f"Here is hint {hint}")
guesses -= 1
await message.channel.send(f"{guesses} left")
if guesses == 0:
break
except asyncio.TimeoutError:
await message.channel.send("Time out!")
我试图用python将我的猜谜游戏集成到discord bot中,但这是怎么回事:
** ???_?????_?0?#1885:.play
???????#8974:猜一猜!
???_?????_?0?#1885:测试
???????#8974:猜错了!
???????#8974:这是提示e
???????#8974:还剩6个
???_?????_?0?#1885:专家
???????#8974:猜错了!
???????#8974:这是提示
???????#8974:还剩5个
???_?????_?0?#1885:额外
???????#8974:猜错了!
???????#8974:这是提示exa
???????#8974:还剩4个
???_?????_?0?#1885:示例
???????#8974:猜错了!
???????#8974:这是提示考试
???????#8974:还剩3个
???_?????_?0?#1885:asd
???????#8974:猜错了!
???????#8974:这是提示示例
???????#8974:还剩2个
** 如您所见,消息停留在“ .play”上。 (我调试了)问题是我不知道如何重设消息并接收其他输入。
答案 0 :(得分:2)
您的问题写得很不好。但是,即使您要搜索的是如何在此处获取用户输入,您将如何操作。
import asyncio
@bot.command()
async def some_command(ctx):
def check(message): # ALL FILTERS GO HERE
return message.author.id == ctx.author.id and message.channel.id == ctx.channel.id
try:
message = await bot.wait_for('message', check=check, timeout=60.0)
except asyncio.TimeoutError:
await ctx.send('timeout')
return
await ctx.send("Text inputted: "+message.content)