private static string TranslatePrecondition(PreconditionAttribute precondition, IDocumentFormatter f) { if (precondition is MinPermissionsAttribute minPermissionsAttribute) { return($"Minimum permission: {f.Em($"{minPermissionsAttribute.AccessLevel.Humanize(LetterCasing.Title)}")}"); } else if (precondition is RequireContextAttribute contextAttribute) { return($"Can only be used in a {f.Em(TranslateContext(contextAttribute.Contexts))}"); } else if (precondition is RequireBotPermissionAttribute || precondition is RequireUserPermissionAttribute) { string permission = ""; string prefix = ""; GuildPermission? guildPermission; ChannelPermission?channelPermission; if (precondition is RequireBotPermissionAttribute) { guildPermission = (precondition as RequireBotPermissionAttribute).GuildPermission; channelPermission = (precondition as RequireBotPermissionAttribute).ChannelPermission; prefix = "Bot"; } else { guildPermission = (precondition as RequireUserPermissionAttribute).GuildPermission; channelPermission = (precondition as RequireUserPermissionAttribute).ChannelPermission; prefix = "User"; } if (guildPermission != null && guildPermission.HasValue) { var guildPermissions = guildPermission.Value.ToString().Split(',').Select(flag => (GuildPermission)Enum.Parse(typeof(GuildPermission), flag)).ToList(); permission += $"{prefix} requires guild permission{(guildPermissions.Count() > 1 ? "s" : "")}: {f.Em(string.Join(", ", guildPermissions.Select(gp => gp.Humanize(LetterCasing.Title))))} "; } if (channelPermission != null && channelPermission.HasValue) { var channelPermissions = channelPermission.Value.ToString().Split(',').Select(flag => (ChannelPermission)Enum.Parse(typeof(ChannelPermission), flag)).ToList(); permission += $"{prefix} requires channel permission{(channelPermissions.Count() > 1 ? "s" : "")}: {f.Em(string.Join(", ", channelPermissions.Select(cp => cp.Humanize(LetterCasing.Title))))} "; } return(permission.Trim()); } else { return(precondition.ToString()); } }
private static string BuildDocumentation(CommandService commandService, IDocumentFormatter f) { string prefix = GuildConfig.DefaultPrefix; StringBuilder builder = new StringBuilder(); foreach (var module in commandService.Modules) { builder.AppendLine(f.H3(module.Name)); var modulePreconditions = module.Preconditions?.Select(x => TranslatePrecondition(x, f)).ToList(); if (modulePreconditions != null && modulePreconditions.Count > 0) { builder.AppendLine(f.NewLine($"{f.Strong("Preconditions:")} {string.Join(", ", modulePreconditions)}")); } builder.AppendLine(f.NewLine("")); foreach (var cmd in module.Commands) { string parameters = string.Empty; if (cmd.Parameters != null && cmd.Parameters.Count > 0) { parameters = $"{string.Join(" ", cmd.Parameters.Select(x => $"_{x.Name}"))}"; } builder.AppendLine(f.NewLine(f.InlineCode($"{prefix}{cmd.Aliases[0]} {parameters}"))); var example = cmd.Attributes.OfType <ExampleAttribute>().FirstOrDefault(); if (example != null && !example.ExampleText.IsEmpty()) { builder.AppendLine(f.NewLine($"{f.Em("Example:")} {f.InlineCode(example.ExampleText)}")); } var commandPreconditions = cmd.Preconditions?.Select(x => TranslatePrecondition(x, f)).ToList(); if (commandPreconditions != null && commandPreconditions.Count > 0) { builder.AppendLine(f.NewLine($"{f.Em("Preconditions:")} {string.Join(", ", commandPreconditions)}")); } if (!cmd.Remarks.IsEmpty()) { builder.AppendLine(f.NewLine($"{f.Em("Remarks:")} {cmd.Remarks}")); } builder.AppendLine(f.NewLine("")); } builder.AppendLine(f.NewLine(f.HorizontalRule())); } return(builder.ToString()); }