Raphiel
Raphiel

Reputation: 388

Discord Python Rewrite - help command error (custom)

So, I've made a help that works, but I want it to say something if the category that the user entered is invalid. I got a working code without the error if the category is invalid. The code:

@client.command()
async def help(ctx, *, category = None):

    if category is not None:
        if category == 'mod' or 'moderation' or 'Mod' or 'Moderation':
            modhelpembed = discord.Embed(
                title="Moderation Help",
                timestamp=datetime.datetime.now(),
                colour=discord.Color.green()
                )

            modhelpembed.add_field(name='kick', value="Kicks a member from the server", inline=False)
            modhelpembed.add_field(name='ban', value='bans a member from the server', inline=False)
            modhelpembed.add_field(name='unban', value='unbans a member from the server', inline=False)
            modhelpembed.add_field(name="nuke", value="Nukes a channel :>", inline=False)
            modhelpembed.add_field(name='mute', value="Mute a member", inline=False)
            modhelpembed.add_field(name="purge", value='purges (deletes) a certain number of messages', inline=False)

            await ctx.send(f'{ctx.author.mention}')
            await ctx.send(embed=modhelpembed)

        elif category == 'fun' or 'Fun':
            funembed = discord.Embed(
                title="Fun Help",
                timestamp=datetime.datetime.now(),
                colour=discord.Color.green()
                )
            
            funembed.add_field(name='meme', value='shows a meme from r/memes', inline=False)
            funembed.add_field(name='waifu', value='shows a waifu (pic or link) from r/waifu', inline=False)
            funembed.add_field(name='anime', value='shows a anime (image or link) from r/anime', inline=False)
            funembed.add_field(name='spotify', value='Tells you the targeted user listening on', inline=False)
            funembed.add_field(name="song", value="Tells you the whats the targeted user listening in Spotify", inline=False)
            funembed.add_field(name="album", value="Tells you whats the targeted user album", inline=False)
            funembed.add_field(name="timer", value="Sets a Timer for you.", inline=False)

            await ctx.send(f'{ctx.author.mention}')
            await ctx.send(embed=funembed)

    else:
        nonembed = discord.Embed(
            title="Help list",
            timestamp=datetime.datetime.now(),
            colour=discord.Color.green(),
            description='Category:\nmod\nfun'
            )
        
        await ctx.send(f'{ctx.author.mention}')
        await ctx.send(embed=nonembed)

It works, but when i tried inputing a invalid category it sends Moderation.

Upvotes: 0

Views: 106

Answers (1)

MrSpaar
MrSpaar

Reputation: 3994

You erros comes from your second if statement. You just have to replace it with either:

  • if category == ('mod' or 'moderation' or 'Mod' or 'Moderation'):
    
  • if category in ['mod', 'moderation', 'Mod', 'Moderation']:
    

Here's why your statement is triggered when you input an invalid category:

  • An empty string returns False (eg. "") and a string returns True (eg. "TEST").

  • If you don't put brackets, it will separate each or as a condition (if category == 'mod' / if 'mod' / if 'moderation' / if 'Mod' / if 'Moderation').

  • Since a non empty string returns True, when you input an invalid category, your second if statement gets triggered and it gives you the Moderation help message.

You can also use commands.Command attributes to make some refactoring:

@client.command(description='Say hi to the bot')
async def hello(ctx):
    await ctx.send(f'Hi {ctx.author.mention}')

@client.command(description='Test command')
async def test(ctx):
    await ctx.send('TEST')

@client.command()
async def help(ctx, *, category = None):
    categories = {
        'fun': ['hello',],
        'testing': ['test',],
    }
    if category is None:
       desc = '\n'.join(categories.keys())
       embed = discord.Embed(
            title="Help list",
            timestamp=datetime.datetime.now(),
            colour=discord.Color.green(),
            description=f'Categories:\n{desc}'
        )
    else:
        category = category.lower()
        if not category in categories.keys():
            await ctx.send('Category name is invalid!')
            return
        
        embed = discord.Embed(
            title=f"{category.capitalize()} Help",
            timestamp=datetime.datetime.now(),
            colour=discord.Color.green()
        )

        for cmd in categories[category]:
            cmd = client.get_command(cmd)
            embed.add_field(name=cmd.name, value=cmd.description)

        await ctx.send(ctx.author.mention, embed=embed)
            

Upvotes: 1

Related Questions