Friday Discord Bot

Project Type: Personal | 3 Min Read

Friday is an AI like software that I am developing. It is in the very early stages of development and the command code below is the only code that I am proud of, and have completed (although this code will always have something to make better). This whole bot was made in the language C#.

Friday Discord Bot logo

Code

I apoligize for the messy code. I have not updated this code since

This is the code for the help command. It displays all of the commands that have been setup and helpfull info about how to use each command.


public class Help : ModuleBase
{
private readonly CommandService _commands;
private readonly IServiceProvider _map;

public Help(IServiceProvider map, CommandService commands)
{
    _commands = commands;
    _map = map;
}

[Command("help")]
[Alias("?")]
public async Task HelpTask([Remainder]string path = "")
{
    EmbedBuilder hbuilder = new EmbedBuilder();
    if (path == "")
    {
        //the list of commands 
        hbuilder.Title = "Friday - Help";
        hbuilder.Color = Color.Blue;

        foreach (var com in _commands.Modules.Where(m => m.Parent == null))
        {
            GetHelp(com, ref hbuilder);
        }

        hbuilder.AddField(f =>
        {
            f.Name = "How to use Prefixes";
            f.Value = "If there is a prefix you have to type that before the command.";
        });

        hbuilder.Footer = new EmbedFooterBuilder
        {
            Text = "Use 'help ' to get help with a module."
        };
    }
    else
    {
        //if there is something other than the list of commands
        var comm = _commands.Commands.FirstOrDefault(m => m.Name.ToLower() == path.ToLower());
        //Console.WriteLine("'" + path.ToLower() + "'" + "'" + comm.Name.ToLower() +"'");
        if (comm == null) 
        { 
            await ReplyAsync("No command could be found with that name."); 
            return; 
        }

        hbuilder.Title = comm.Name;
        hbuilder.Color = Color.Blue;
        hbuilder.Description = $"Summary: {comm.Summary}\n" +
        (!string.IsNullOrEmpty(comm.Remarks) ? $"({comm.Remarks})\n" : "") +
        (comm.Aliases.Any() ? $"**Aliases:** {string.Join(", ", comm.Aliases.Select(x => $"`{x}`"))}\n" : "") +
        $"**Usage:** `{GetPrefix(comm)} {GetAliases(comm)}`";
        /*(mod.Submodules.Any() ? $"Submodules: {mod.Submodules.Select(m => m.Name)}\n" : "") + " ";*/
    }
    await ReplyAsync("", false,hbuilder.Build());
}

private void GetCommands(CommandInfo com, ref EmbedBuilder hbuilder)
{
    foreach (var command in com.Module.Commands)
    {
        command.CheckPreconditionsAsync(Context, _map).GetAwaiter().GetResult();
        GetCommand(command,ref hbuilder);
    }
}

private void GetCommand(CommandInfo command, ref EmbedBuilder hbuilder)
{
    hbuilder.AddField(f =>
    {
        f.Name = "****";
        f.Value = $"Summary: {command.Summary}\n" +
        (!string.IsNullOrEmpty(command.Remarks) ? $"({command.Remarks})\n" : "") +
        (command.Aliases.Any() ? $"**Aliases:** {string.Join(", ", command.Aliases.Select(x => $"`{x}`"))}\n" : "") +
        $"**Usage:** `{GetPrefix(command)} {GetAliases(command)}`";
    });
}

private string GetPrefix(CommandInfo command)
{
        var output = GetPrefix(command.Module);
        output += $"{command.Aliases.FirstOrDefault()} ";
        return output;

}
    private string GetPrefix(ModuleInfo module)
    {
    string output = "";
    if (module.Parent != null) output = $"{GetPrefix(module.Parent)}{output}";
    if (module.Aliases.Any())
    output += string.Concat(module.Aliases.FirstOrDefault(), " ");
    return output;
}

private string GetAliases(CommandInfo command)
{
    StringBuilder output = new StringBuilder();

    if (!command.Parameters.Any()) 
        return output.ToString();

    foreach (var param in command.Parameters)
    {
        if (param.IsOptional)
            output.Append($"[{param.Name} = {param.DefaultValue}] ");
        else if (param.IsMultiple)
            output.Append($"|{param.Name}| ");
        else if (param.IsRemainder)
            output.Append($"...{param.Name} ");
        else
            output.Append($"<{param.Name}> ");
    }

    return output.ToString();
}

private void GetHelp(ModuleInfo _module, ref EmbedBuilder hbuilder)
{
    string gcommands = "";
    string prefixs = "";

    foreach (var prefix in _module.Aliases)
    {
        prefixs = string.Join(", `", prefix + "`");
    }

    foreach (var command in _module.Commands)
    {
        gcommands = string.Join(", `", command.Name + "`");
    }

    hbuilder.AddField(f =>
    {
        f.Name = _module.Name;
        f.Value = (!string.IsNullOrEmpty(prefixs) ? $"Prefix(s): ({prefixs})\n" : "") +
        $"Commands: {gcommands}";
    });
    }
}