'How to hide Cogs/Commands from custom Help command
I need to exclude certain Cogs/Commands from being embedded in custom help command, though I cannot figure a way to exclude them from the loop.
Any help is appreciated, thanks!
@commands.command(name="help")
async def help(self, context):
"""
List all commands from every Cog the bot has loaded.
"""
prefix = config.BOT_PREFIX
if not isinstance(prefix, str):
prefix = prefix[0]
embed = discord.Embed(title="Help", description="List of available commands:", color=config.success)
for i in self.bot.cogs:
cog = self.bot.get_cog(i.lower())
if cog != "owner":
commands = cog.get_commands()
command_list = [command.name for command in commands]
command_description = [command.help for command in commands]
help_text = '\n'.join(f'{prefix}{n} - {h}' for n, h in zip(command_list, command_description))
embed.add_field(name=i.capitalize(), value=f'```{help_text}```', inline=False)
await context.send(embed=embed)
Solution 1:[1]
If you use @has_permissions() or @has_role() decorators on top of your commande, it will only be shown in the default help menu if you pass that check.
If you want to make your own custom command, you can iterate over all your bot or cog commands via self.get_commands(). Then for each command you can find command.checks that returns a list of all the checks added to your command (has_permissions, has_role, or your custom checks). You can then use those checks (they are a function) to check if the author of the message passes them all
This code sends an embed with all the commands that the author can use
@commands.command()
def help(self, ctx: Context):
embed = Embed()
embed.title = f"Admin commands of {self.qualified_name}"
for command in self.get_commands():
name = command.name
description = command.description
passes_check = True
for check in command.checks:
if not check(ctx.author):
passes_check = False
break
if passes_check:
embed.add_field(name=name, value=description, inline=False)
await ctx.send(embed=embed)
References :
Solution 2:[2]
This should help: can_run (discord.py docs)
go = False
try:
await command.can_run(self.context)
go = True
except:
go = False
Implement this into your code and it should be good to go (it worked for me)
Solution 3:[3]
the error message TypeError implies the datatype you are passing to idxmax() is the wrong type.
idxmax() works on numerical values, if you look at the datatypes for the dataframe df.info() you will find that the dataype for 'Starting Median Salary' is an object, which is pandas speak for a string (usually).
You will need to convert the datatype to a numerical type e.g. float.
You can do this by removing the '$' sign
df['Starting Median Salary'].replace(r'[\$]', '', regex=True)
The remove the commas in the values
df['Starting Median Salary'].replace(r'[,]', '', regex=True)
Finally cast the datatype to a float
df['Starting Median Salary'].astype('float')
Now idxmax() should work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nathan Marotte |
| Solution 2 | Questionable_Duck |
| Solution 3 | RustyPython |
