Search code examples
discorddiscord.py

Discord.py IF statement not passing?


I feel like this is an easy fix but I can't figure out why I'm getting this response.

I set each if statement to have a unique error message so I can see where the error is.

Every time I run $setbirthday 05/03 in my server I get the "Invalid month specified!" while the first index of my argument (0) would seemingly pass this.

enter image description here

Here is the code:

class BirthdayCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot


    @commands.command(pass_context=true)
    async def setbirthday(self, ctx, args):
        user = ctx.author
        role = discord.utils.get(ctx.guild.roles, name="Creator")
        if role in user.roles:
            if args != '':
                # If it's an actual month (index 0)
                if args[0] != 0 or args[0] != 1:
                    await ctx.send("Invalid month specified!")
                else:
                    # If it's an actual month (index 1)
                    if args[1] < 0:
                        await ctx.send("That's not a valid month!")
                    elif args[1] <= 9:
                        # If they separated month/day using "/"
                        if args[2] != "/":
                            await ctx.send("You have to specify your birthday like `MM/DD`!")
                        else:
                            # If it's an actual day (index 0)
                            if args[3] > 3:
                                await ctx.send("Invalid day specified!")
                            elif args[3] < 0:
                                await ctx.send("That isn't a day!")
                            else:
                                # If it's an actual day (index 1)
                                if args[4] < 0:
                                    await ctx.send("Incorrect day!")
                                else:
                                    # Make sure there's no extra numbers
                                    if args[5] != '':
                                        await ctx.send("Make sure it's just `MM/DD`!")
                                    else:
                                        birthday = args[0] + '' + args[1] + '' + "/" + args[3] + '' + args[4]
                                        await ctx.send("Your birthday has been registered as: " + "`" + str(birthday) + "`" + "!")
                    else:
                        await ctx.send("Error!")
            else:
                await ctx.send("Set your birthday by doing `$setbirthday MM/DD`")
        else:
            await ctx.send("You don't have the required role to set your birthday!")

Solution

  • There are two main problems in your code :

    • You should use guard clauses instead of nesting if statements, it'll make your code easier to read and understand
    • You can use string.split() instead of having to comment what each index corresponds to, it also simplifies your conditions.

    You then obtain this code:

    @commands.command()
    async def setbirthday(self, ctx, arg):
        user = ctx.author
        role = discord.utils.get(ctx.guild.roles, name="Creator")
    
        if role not in user.roles:
            return await ctx.send("You don't have the required role to set your birthday!")
    
        if len(arg.split('/')) != 2:
            return await ctx.send("You have to specify your birthday like `MM/DD`!")
    
        month, day = arg.split("/")
    
        if not month.isdigit() or not 1 <= int(month) <= 12:
            return await ctx.send("Invalid month specified!")
    
        if not day.isdigit() or not 1 <= int(day) <= 31:
            return await ctx.send("Invalid day specified!")
    
        await ctx.send(f"Your birthday has been registered as: {month}/{day}!")