Example #1
0
        public async Task Help()
        {
            ModuleInfo Root(ModuleInfo module) => module.Parent == null ? module : Root(module.Parent);

            var prefix       = _discord.GetCommandsPrefix(Context.Guild);
            var embedBuilder = new EmbedBuilder()
                               .WithTitle("source code")
                               .WithUrl("https://github.com/erraineon/tadmor");
            var commandsByRoot = _commands.Commands
                                 .Where(c => c.Attributes.OfType <BrowsableAttribute>().All(a => a.Browsable))
                                 .GroupBy(command => Root(command.Module));
            var sb = new StringBuilder();

            foreach (var commands in commandsByRoot)
            {
                foreach (var cmd in commands)
                {
                    var preconditionsResult = await cmd.CheckPreconditionsAsync(Context, _services);

                    if (preconditionsResult.IsSuccess)
                    {
                        sb.Append($"**{prefix}{cmd.Aliases.First()}");
                        foreach (var parameter in cmd.Parameters)
                        {
                            sb.Append(' ');
                            if (parameter.IsOptional || parameter.IsMultiple)
                            {
                                sb.Append("(optional) ");
                            }
                            sb.Append(parameter.Name);
                        }

                        sb.Append("**");
                        if (cmd.Summary != null)
                        {
                            sb.Append($": {cmd.Summary}");
                        }
                        sb.AppendLine();
                    }
                }

                if (sb.Length > 0)
                {
                    embedBuilder.AddField(field => field
                                          .WithName(commands.Key.Summary ?? commands.Key.Name)
                                          .WithValue(sb.ToString()));
                }
                sb.Clear();
            }

            await ReplyAsync(string.Empty, embed : embedBuilder.Build());
        }
Example #2
0
        public async Task Help()
        {
            ModuleInfo Root(ModuleInfo module) => module.Parent == null ? module : Root(module.Parent);

            var prefix       = _discord.GetCommandsPrefix(Context.Guild);
            var embedBuilder = new EmbedBuilder()
                               .WithTitle("source code")
                               .WithUrl("https://github.com/erraineon/tadmor");
            var commandsByRoot = _commands.Commands.GroupBy(command => Root(command.Module).Name);

            foreach (var commands in commandsByRoot)
            {
                var sb = new StringBuilder();
                foreach (var cmd in commands)
                {
                    var resultGroups = await Task.WhenAll(cmd.Preconditions
                                                          .Concat(cmd.Module.Preconditions)
                                                          .GroupBy(p => p.Group, p => p.CheckPermissionsAsync(Context, cmd, _services))
                                                          .Select(Task.WhenAll));

                    var preconditionsOk = resultGroups.All(resultGroup => resultGroup.Any(result => result.IsSuccess));
                    if (preconditionsOk)
                    {
                        var parameters = string.Join(" ", cmd.Parameters.Select(p => p.Name));
                        sb.Append($"{prefix}{cmd.Aliases.First()} {parameters}");
                        sb.AppendLine(cmd.Summary == default ? string.Empty : $": {cmd.Summary}");
                    }
                }

                if (sb.Length > 0)
                {
                    embedBuilder.AddField(field => field
                                          .WithName(commands.Key.Replace("Module", string.Empty))
                                          .WithValue(sb.ToString()));
                }
            }

            await ReplyAsync(string.Empty, embed : embedBuilder.Build());
        }