user16080338
user16080338

Reputation:

!help [command_name] discord.py

I’m creating a discord bot using python. I did a custom help command (don’t worry, for this part I’m ok), and I want to do a !help [command_name]. Let me explain. If we do !help, the bot will send the basic help command, but if I do !help [command_name], like !help ping, it will send informations a bout the ping command. I tried to do this

@bot.command()
async def help ping(ctx):
    …
    await ctx.send(embed=embed)

@bot.command()
async def help(ctx):
    …
    await ctx.send(embed=embed)

It didn’t worked, so I did this:

@bot.event
async def on_message(message):
    if message.content.startswith(+help ping):
    …
    await message.channel.send(embed=embed)

@bot.command()
async def help(ctx):
    …
    await ctx.send(embed=embed)

There, the !help ping worked, but not the !help. And now, I’m there. Can you tell me a simple solution using @bot.command() or @bot.event?

Upvotes: 0

Views: 3156

Answers (4)

Reality281
Reality281

Reputation: 19

You can do this:

@bot.commands()
async def help(ctx, cmd=None):#if the user doesn't give any arguments for "cmd" then it will count it as None
    if not cmd:#checks if the user gave arguments for "cmd"
        await ctx.send("Help is here!")#the message the bot should send

    elif cmd.lower()=="ping":#checks if the arguments given for "cmd" is ping and I set "cmd.lower()" so that the argument is not case sensitive
        await ctx.send("Help for command `ping` is here!")

    else:#if the arguments doesn't match any of the if statements it will show an error
        await ctx.send(f"No command named `{cmd.lower()}`")#you can set anything for the error message

Hope you find it helpful

And sorry for answering this late

Upvotes: 0

Pixie
Pixie

Reputation: 166

You can also do Grouping on the commands.

Example :

@client.group(invoke_without_command = True) # for this main command (.help)
async def help(ctx):
    await ctx.send("Help! Categories : Moderation, Utils, Fun")

@help.command()   #For this command (.help Moderation)
async def Moderation(ctx):
    await ctx.send("Moderation commands : kick, ban, mute, unban")

@help.command()   #And for this command (.help Utils)
async def Uitls(ctx):
    await ctx.send("Utils : ping, prefix")

@help.command()   #And lastly this command (.help Fun)
async def Fun(ctx): 
    await ctx.send("Fun : 8ball, poll, headsortails")

Thank me later :D

Upvotes: 4

user15308374
user15308374

Reputation:

try it:

...
from discord.ext.commands import Command

... # bot and bot commands

# command example 1
@client.command(help='I am an example')
async def ex1(ctx):
    await ctx.send('I am working')


# command example 2
@client.command()
async def ex2(ctx):
    '''I am an example too'''
    await ctx.send('I am working too')

@client.command(name='help')
async def my_help_command(ctx, command_name: str = 'help')
    await ctx.send(client.get_command(command_name) or 'Command Not Find')

Output with '!' command_perfix:

 me: !help ex1
bot: I am an example
 me: !help ex2
bot: I am an example too
 me: !help i_an_not_command
bot: Command Not Find

Upvotes: 0

user15308374
user15308374

Reputation:

...
from discord.ext.commands import Cog, Group, Command, HelpCommand

... # bot and bot commands

class MyCustomHelpCommand(HelpCommand):
    async def send_bot_help(self, mapping): # Mapping[Optional[Cog], List[Command]]
        ... # write you'r help message here

        self.get_destination().send(message)

    async def send_cog_help(self, cog: Cog):
        ... # write you'r help message here

        self.get_destination().send(message)

    async def send_group_help(self, group: Group):
        ... # write you'r help message here

        self.get_destination().send(message)

    async def command_not_find(self):
        ... # write you'r help message here

        self.get_destination().send(message)


bot.help_command = MyCustomHelpCommand()

docs: HelpCommand also see: DefaultHelpCommand, MinimalHelpCommand

Upvotes: 0

Related Questions