简体   繁体   中英

How do I put discord.py help command in an embed?

So, I currently have a discord bot running with discord.py, and as you know, discord.py comes with its own help command (so I don't have to make my own). It's very useful, and I have my commands separated into cogs/categories. It really helps with simplicity, because now I don't have to write my own help command.

The problem is, when I run the help command, it comes to me in a giant code block, like so: 在此处输入图片说明 I have heard some complaints from users that this isn't exactly visually appealing, and as I add more commands, it fills up the screen. Is there simple way (without writing my own help command) to move all of this onto an embed? Maybe copy the output of this help command, and move that onto an embed? If not, it's ok, I'll write my own help command, but I'm just trying to look for a simple way to do this without getting my hands dirty coding. As always, thank you in advance.

In case it is needed, here is sample from my code:

import discord
from discord.ext import commands, tasks

TOKEN = "INSERT TOKEN HERE"
client = commands.Bot(command_prefix="wurf ", case_insensitive=True)

#Utility Category
class Utility(commands.Cog):
    def __init__(self, client):
        self.client = client
        
    @commands.command(
        help="Shows the ping/latency of the bot in miliseconds.",
        brief="Shows ping."
    )
    async def ping(self, ctx):
        if round(client.latency * 1000) <= 50:
            embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0x44ff44)
        elif round(client.latency * 1000) <= 100:
            embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0xffd000)
        elif round(client.latency * 1000) <= 200:
            embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0xff6600)
        else:
            embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0x990000)
        await ctx.send(embed=embed)
client.add_cog(Utility(client))
client.run(TOKEN)

You will have to override the default help command, with Bot.help_command

Here is a simple embed implementation I threw together inheriting from MinimalHelpCommand

class MyHelpCommand(commands.MinimalHelpCommand):
    async def send_pages(self):
        destination = self.get_destination()
        e = discord.Embed(color=discord.Color.blurple(), description='')
        for page in self.paginator.pages:
            e.description += page
        await destination.send(embed=e)

client.help_command = MyHelpCommand()

An easy way to do this is by using the "PrettyHelp" python module:

  1. You can install it with pip using pip install -U discord-pretty-help
  2. Then import it from pretty_help import DefaultMenu, PrettyHelp
  3. Then you set the menu buttons and replace the default help menu:
menu = DefaultMenu('◀️', '▶️', '❌') # You can copy-paste any icons you want.
bot.help_command = PrettyHelp(navigation=menu, color=discord.Colour.green()) 
# The color can be whatever you want, including normal color codes, 
# I just like the discord green personally.

I believe this has to be done in the main bot file rather than a cog, and it ends up looking like so: screenshot

As mentioned by the 2 answers before mine, you will have to set the default help command to None. This is done from the beginning as you create your bot.
As for the embed, you will have to do some work by yourself. Here is an example from my bot, but please note that this is not necessarily the best practice - I don't know , but it worked for me.

# When you create your bot, add this in the arguments
bot = commands.Bot(prefix='.', help_command=None)
bot.remove_command('help')

# My sample help command:
@bot.command()
async def help(ctx, args=None):
    help_embed = discord.Embed(title="My Bot's Help!")
    command_names_list = [x.name for x in bot.commands]

    # If there are no arguments, just list the commands:
    if not args:
        help_embed.add_field(
            name="List of supported commands:",
            value="\n".join([str(i+1)+". "+x.name for i,x in enumerate(bot.commands)]),
            inline=False
        )
        help_embed.add_field(
            name="Details",
            value="Type `.help <command name>` for more details about each command.",
            inline=False
        )

    # If the argument is a command, get the help text from that command:
    elif args in command_names_list:
        help_embed.add_field(
            name=args,
            value=bot.get_command(args).help
        )

    # If someone is just trolling:
    else:
        help_embed.add_field(
            name="Nope.",
            value="Don't think I got that command, boss!"
        )

    await ctx.send(embed=help_embed)

You can see the full code from the GitHub repository .

So, I currently have a discord bot running with discord.py, and as you know, discord.py comes with its own help command (so I don't have to make my own). It's very useful, and I have my commands separated into cogs/categories. It really helps with simplicity, because now I don't have to write my own help command.

The problem is, when I run the help command, it comes to me in a giant code block, like so: 在此处输入图片说明 I have heard some complaints from users that this isn't exactly visually appealing, and as I add more commands, it fills up the screen. Is there simple way (without writing my own help command) to move all of this onto an embed? Maybe copy the output of this help command, and move that onto an embed? If not, it's ok, I'll write my own help command, but I'm just trying to look for a simple way to do this without getting my hands dirty coding. As always, thank you in advance.

In case it is needed, here is sample from my code:

import discord
from discord.ext import commands, tasks

TOKEN = "INSERT TOKEN HERE"
client = commands.Bot(command_prefix="wurf ", case_insensitive=True)

#Utility Category
class Utility(commands.Cog):
    def __init__(self, client):
        self.client = client
        
    @commands.command(
        help="Shows the ping/latency of the bot in miliseconds.",
        brief="Shows ping."
    )
    async def ping(self, ctx):
        if round(client.latency * 1000) <= 50:
            embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0x44ff44)
        elif round(client.latency * 1000) <= 100:
            embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0xffd000)
        elif round(client.latency * 1000) <= 200:
            embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0xff6600)
        else:
            embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0x990000)
        await ctx.send(embed=embed)
client.add_cog(Utility(client))
client.run(TOKEN)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM