Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading