简体   繁体   中英

Discord.py bot is not detecting user input

So I made a command where you need to guess who the impostor is. But the bot doesn't seem to pick the user's response ie their choice for who is the impostor..

The code -

    @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)

If you have a solution please answer. Thanks in advance.

  1. Using else in a try-except statement : else is only used after either an if or an elif , not in try-except . Instead of using else , you could use finally , which would be used after the try or except is complete.
  2. user_response is too long : Towards the end, don't forget to do user_response.content rather than user_response on its own! For example, in an embed title you may get an error such as HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body In embed.title: Must be 256 or fewer in length.

Here is part of the revised code.

    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)

代码工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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