Пример #1
0
        public async Task CommandsAsync([Remainder] string name)
        {
            var module = _commandService.GetAllModules().FirstOrDefault(m => m.Name.StartsWith(name, StringComparison.InvariantCultureIgnoreCase));

            if (module is null)
            {
                await ReplyErrorAsync(Localization.HelpModuleNotFound, Context.Prefix);

                return;
            }

            var isOwner = Context.User.Id == Credentials.MasterId;

            var modulesCommands = GetModuleCommands(module, isOwner);

            if (modulesCommands.Count == 0)
            {
                await ReplyErrorAsync(Localization.HelpModuleNotFound, Context.Prefix);

                return;
            }

            var commandsAliases = GetCommandsAliases(modulesCommands, Context.Prefix);

            var embed = new LocalEmbedBuilder
            {
                Color = RiasUtilities.ConfirmColor,
                Title = GetText(module.Parent != null ? Localization.HelpAllCommandsForSubmodule : Localization.HelpAllCommandsForModule, module.Name)
            }.AddField(module.Name, string.Join("\n", commandsAliases), true);

            foreach (var submodule in module.Submodules)
            {
                var submoduleCommands = GetModuleCommands(submodule, isOwner);
                if (submoduleCommands.Count == 0)
                {
                    continue;
                }

                var submoduleCommandsAliases = GetCommandsAliases(submoduleCommands, Context.Prefix);

                embed.AddField(submodule.Name, string.Join("\n", submoduleCommandsAliases), true);
            }

            embed.WithFooter(GetText(Localization.HelpCommandInfo, Context.Prefix));
            embed.WithCurrentTimestamp();
            await ReplyAsync(embed);
        }
Пример #2
0
        public static bool TryParseEmbed(string json, out LocalEmbedBuilder embed)
        {
            embed = new LocalEmbedBuilder();
            try
            {
                var embedDeserialized = JsonConvert.DeserializeObject <JsonEmbed>(json);

                var author      = embedDeserialized.Author;
                var title       = embedDeserialized.Title;
                var description = embedDeserialized.Description;

                var colorString = embedDeserialized.Color;
                var thumbnail   = embedDeserialized.Thumbnail;
                var image       = embedDeserialized.Image;
                var fields      = embedDeserialized.Fields;
                var footer      = embedDeserialized.Footer;
                var timestamp   = embedDeserialized.Timestamp;

                if (author != null)
                {
                    embed.WithAuthor(author);
                }

                if (!string.IsNullOrEmpty(title))
                {
                    embed.WithTitle(title);
                }

                if (!string.IsNullOrEmpty(description))
                {
                    embed.WithDescription(description);
                }

                if (!string.IsNullOrEmpty(colorString))
                {
                    embed.WithColor(HexToInt(colorString) ?? 0xFFFFFF);
                }

                if (!string.IsNullOrEmpty(thumbnail))
                {
                    embed.WithThumbnailUrl(thumbnail);
                }
                if (!string.IsNullOrEmpty(image))
                {
                    embed.WithImageUrl(image);
                }

                if (fields != null)
                {
                    foreach (var field in fields)
                    {
                        var fieldName   = field.Name;
                        var fieldValue  = field.Value;
                        var fieldInline = field.IsInline;

                        if (!string.IsNullOrEmpty(fieldName) && !string.IsNullOrEmpty(fieldValue))
                        {
                            embed.AddField(fieldName, fieldValue, fieldInline);
                        }
                    }
                }

                if (footer != null)
                {
                    embed.WithFooter(footer);
                }

                if (timestamp.HasValue)
                {
                    embed.WithTimestamp(timestamp.Value);
                }
                else if (embedDeserialized.WithCurrentTimestamp)
                {
                    embed.WithCurrentTimestamp();
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Пример #3
0
        public async Task HelpAsync(string alias1, string?alias2 = null)
        {
            var module  = GetModuleByAlias(alias1);
            var command = GetCommand(module, module is null ? alias1 : alias2);

            if (command is null)
            {
                await ReplyErrorAsync(Localization.HelpCommandNotFound, Context.Prefix);

                return;
            }

            var moduleAlias = module != null ? $"{module.Aliases[0]} " : string.Empty;
            var title       = string.Join(" / ", command.Aliases.Select(a => $"{Context.Prefix}{moduleAlias}{a}"));

            if (string.IsNullOrEmpty(title))
            {
                title = $"{Context.Prefix}{moduleAlias}";
            }

            var embed = new LocalEmbedBuilder
            {
                Color = RiasUtilities.ConfirmColor,
                Title = title
            };

            var moduleName = command.Module.Name;

            if (command.Module.Parent != null)
            {
                moduleName = $"{command.Module.Parent.Name} -> {moduleName}";
            }

            var description = new StringBuilder(command.Description)
                              .Append($"\n\n**{GetText(Localization.HelpModule)}**\n{moduleName}")
                              .Replace("[prefix]", Context.Prefix)
                              .Replace("[currency]", Credentials.Currency);

            embed.WithDescription(description.ToString());

            foreach (var attribute in command.Checks)
            {
                switch (attribute)
                {
                case UserPermissionAttribute userPermissionAttribute:
                    var userPermissions = userPermissionAttribute.GuildPermissions
                                          .GetValueOrDefault()
                                          .ToString()
                                          .Split(",", StringSplitOptions.RemoveEmptyEntries)
                                          .Select(x => x.Humanize(LetterCasing.Title))
                                          .ToArray();
                    embed.AddField(GetText(Localization.HelpRequiresUserPermission), string.Join("\n", userPermissions), true);
                    break;

                case BotPermissionAttribute botPermissionAttribute:
                    var botPermissions = botPermissionAttribute.GuildPermissions
                                         .GetValueOrDefault()
                                         .ToString()
                                         .Split(",", StringSplitOptions.RemoveEmptyEntries)
                                         .Select(x => x.Humanize(LetterCasing.Title))
                                         .ToArray();
                    embed.AddField(GetText(Localization.HelpRequiresBotPermission), string.Join("\n", botPermissions), true);
                    break;

                case OwnerOnlyAttribute _:
                    embed.AddField(GetText(Localization.HelpRequiresOwner), GetText(Localization.CommonYes), true);
                    break;
                }
            }

            var commandCooldown = command.Cooldowns.FirstOrDefault();

            if (commandCooldown != null)
            {
                var locale = Localization.GetGuildLocale(Context.Guild !.Id);
                embed.AddField(GetText(Localization.CommonCooldown),
                               $"{GetText(Localization.CommonAmount)}: **{commandCooldown.Amount}**\n" +
                               $"{GetText(Localization.CommonPeriod)}: **{commandCooldown.Per.Humanize(culture: new CultureInfo(locale))}**\n" +
                               $"{GetText(Localization.CommonPer)}: **{GetText(Localization.CommonCooldownBucketType(commandCooldown.BucketType.Humanize(LetterCasing.LowerCase).Underscore()))}**",
                               true);
            }

            embed.AddField(GetText(Localization.CommonExample), string.Format(command.Remarks, Context.Prefix));
            embed.WithCurrentTimestamp();

            await ReplyAsync(embed);
        }