【问题标题】:Discord.py bot is not detecting user inputDiscord.py 机器人未检测到用户输入
【发布时间】:2021-09-28 03:06:02
【问题描述】:

所以我做了一个命令,你需要猜测冒名顶替者是谁。 但机器人似乎没有选择用户的响应,即他们选择谁是冒名顶替者..

代码-

    @commands.command(aliases=['gti'])
    async def impostor(self, ctx):
        def check(message):
            return message.author == ctx.author and message.channel == ctx.channel

        impostor_list = ['red', 'yellow', 'white', 'purple', 'pink', 'orange', 'lime', 'green', 'cyan', 'brown', 'blue']
        impostor = random.choice(impostor_list)

        embed = discord.Embed(title="Guess the impostor!", description="Who is sussy? Write their color in chat within 20s to continue!", color = discord.Color.random())
        embed.add_field(name="The people are:", value="```red, yellow, white, purple, pink, orange, lime, green, cyan, blue, brown```")

        send_em = await ctx.send(embed=embed)
        try:
            user_response = await self.client.wait_for("message", timeout=20, check=check)
        except asyncio.TimeoutError:
            return await ctx.send("You took too long to answer.")
        else:
            if user_response.content == impostor:
                correct_em = discord.Embed(title=f"{user_response} was ejected.", description=f"{user_response} was the Impostor. Well done!", color = discord.Color.random())

                return await ctx.send(embed=correct_em)
            else:
                wrong_em = discord.Embed(title=f"{user_response} was ejected.", description=f"{user_response} was not the Impostor.\nYou lose! {impostor} was the Impostor.")
                return await ctx.send(embed=wrong_em)

如果您有解决方案,请回答。 提前致谢。

【问题讨论】:

  • 尽量不要返回等待,而只是等待它们
  • 不...它仍然不起作用。即使我在聊天中写了颜色,它也没有响应,也没有在 CMD 中显示任何错误。
  • 您使用的是哪个 Python 版本?您还定义了send_em,但从未使用它,为什么?
  • 为什么在你的try-except 声明之后有一个else?您之前没有使用任何ifelif,您的意思是改写finally 吗?

标签: python python-3.x discord.py bots


【解决方案1】:
  1. try-except 语句中使用elseelse 仅在ifelif 之后使用,而不在try-except 中使用。您可以使用finally,而不是使用else,这将在tryexcept 完成后使用。
  2. user_response 太长:到最后,别忘了自己做user_response.content 而不是user_response!例如,在嵌入标题中,您可能会收到诸如HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body In embed.title: Must be 256 or fewer in length. 之类的错误

这是修改后的代码的一部分。

    try:
        user_response = await self.client.wait_for("message", timeout=20, check=check)
    except asyncio.TimeoutError:
        return await ctx.send("You took too long to answer.")
    # You can use finally, that way it will always be done despite the try-except
    # (but in your case, it would only be done after the try
    finally:

        # most of the time you did user_response without content, which may
        # raise an error since it would be over 256 characters in an embed title,
        # ergo, don't forget to add .content to them!

        if user_response.content == impostor:
            correct_em = discord.Embed(title=f"{user_response.content} was ejected.", description=f"{user_response.content} was the Impostor. Well done!", color = discord.Color.random())

            return await ctx.send(embed=correct_em)
        else:
            wrong_em = discord.Embed(title=f"{user_response.content} was ejected.", description=f"{user_response.content} was not the Impostor.\nYou lose! {impostor} was the Impostor.")
            return await ctx.send(embed=wrong_em)

【讨论】:

    猜你喜欢
    • 2020-07-29
    • 2021-04-09
    • 2021-04-04
    • 1970-01-01
    • 2021-07-17
    • 2021-10-06
    • 2019-10-03
    • 2020-07-28
    • 2019-03-06
    相关资源
    最近更新 更多