Home > Enterprise >  Why i've this error in my discord.py code
Why i've this error in my discord.py code

Time:12-04

I am developing a bot with discord.py and I have a bug that I cannot resolve. I don't think the latter is from discord.py but from python (I'm a newbie in this area)

My problem: I test a first condition in an "if". If this is true I test other conditions with another "if". On the other hand if the first "if" returns false, with I test other conditions with an "else" then "if" in this one.

In my very first "if", if true, all subsequent "if" statements are executed even when they shouldn't. I have the same problem with the "if" in the else.

a shema:

if ...:
   if ...:
        ...
   if ...:
        ...
else:
   if ...:
        ...
   if ...:
        ...

my real code:

@commands.command()
async def help(self, ctx, categorie = None)
        if categorie == None:

            await ctx.send(f"Mon préfixe sur ce serveur: `{pref}`\n\nSur quelle partie du bot voulez vous de l'aide ?\n`utile` | `fun` | `recherches` | `moderation` | `creator`")

            def checkMessage(message):
                return message.author == ctx.message.author and ctx.message.channel == message.channel

            partie = await self.client.wait_for("message", timeout = 20, check = checkMessage)
            if partie.content == "moderation" or partie.content == "Moderation":
                await ctx.send(embed=modembed)
            if partie.content == "utile" or partie.content == "Utile":
                await ctx.send(embed=utileembed)
            if partie.content == "fun" or partie.content == "Fun":
                await ctx.send(embed=funembed)
            if partie.content == "recherches" or partie.content == "Recherches":
                await ctx.send(embed=recherchesembed)
            if partie.content == "creator" or "Creator":
                creatorembed.set_thumbnail(url="https://zupimages.net/up/20/52/qpa0.png")
                await ctx.send(embed=creatorembed)
        else:
            if categorie == "moderation" or "Moderation" or "mod":
                await ctx.send(embed=modembed)
            if categorie == "utile" or "Utile" or "util":
                await ctx.send(embed=utileembed)
            if categorie == "fun" or "Fun":
                await ctx.send(embed=funembed)
            if categorie == "recherches" or "recherche" or "Recherches" or "Recherche" or "rech" or "Rech":
                await ctx.send(embed=recherchesembed)
            if categorie == "creator" or "Creator":
                    creatorembed.set_thumbnail(url="https://zupimages.net/up/20/52/qpa0.png")
                    await ctx.send(embed=creatorembed)

it return me: result 1

result 2

CodePudding user response:

You have wrong logic here:

if categorie == "moderation" or "Moderation" or "mod":

This condition is always True. Replace it with

if categorie in ("moderation", "Moderation", "mod"):

Also do it for other your if operators.

But I recommend you to create your help command using HelpCommand class in discord.py. It’s more simple and it’s the best way to create a help command. You can read about it here.

  • Related