Search code examples
c#discorddsharp+

How do I change the help formatter in DSharpPlus


So I'm trying the change the ;help command. Usually, when you type the command it returns a built-in help message. I tried using this code but when I use the command it returns nothing:

public class CustomHelpFormatter: DefaultHelpFormatter 
{
    protected DiscordEmbedBuilder _embed;
    protected StringBuilder _strBuilder;

    public CustomHelpFormatter(CommandContext ctx) : base(ctx) 
    {
        var Rand = new Random();
        _embed = new DiscordEmbedBuilder 
        {
            Color = new DiscordColor((byte) Rand.Next(0, 255), (byte) Rand.Next(0, 255), (byte) Rand.Next(0, 255))
        };
        _strBuilder = new StringBuilder();
    }

    public override BaseHelpFormatter WithCommand(Command command) 
    {
        _embed.Title = "Menu";
        _embed.Description = "```ping```";

        return this;
    }

    public override CommandHelpMessage Build() 
    {
        return new CommandHelpMessage(embed: _embed);
        return new CommandHelpMessage(content: _strBuilder.ToString());
    }
}

I also used this code to register the changes:

Commands.SetHelpFormatter<CustomHelpFormatter>();

Solution

  • In your Bot.cs file you need to disable the built-in help command in your CommandsNextConfiguration like this

                var commandsConfig = new CommandsNextConfiguration
                {
                    StringPrefixes = new string[] { configJson.Prefix },
                    EnableMentionPrefix = true,
                    EnableDms = true,
                    EnableDefaultHelp = false,
                };
    

    By setting EnableDefaultHelp to false, you can then create your own help command without having to go through the stuff you did in your code

    Example of a help command

    [Command("help")]
    public async Task HelpCommand(CommandContext ctx)
    {
        Code whatever you need here. Maybe show an embed message or other
        way to show help information
    }