【问题标题】:How to recieve input from chat again in discord.py?如何在 discord.py 中再次接收来自聊天的输入?
【发布时间】:2021-01-14 20:41:45
【问题描述】:
@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 left

** 如您所见,消息卡在“.play”处。 (我调试了它)问题是我不知道如何重置消息并接收另一个输入。

【问题讨论】:

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


    【解决方案1】:

    你的问题写得很糟糕。但是,如果您要搜索的是如何在此处获取用户输入,那么您是如何做到的。

    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)
          
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 2019-08-17
      相关资源
      最近更新 更多