// thanks BCES00569
        public static async Task <List <DiscordEmbedBuilder> > AsEmbedAsync(this TitlePatch patch, DiscordClient client, string productCode)
        {
            var result       = new List <DiscordEmbedBuilder>();
            var pkgs         = patch?.Tag?.Packages;
            var title        = pkgs?.Select(p => p.ParamSfo?.Title).LastOrDefault(t => !string.IsNullOrEmpty(t)) ?? ThumbnailProvider.GetTitleName(productCode) ?? productCode;
            var thumbnailUrl = await client.GetThumbnailUrlAsync(productCode).ConfigureAwait(false);

            var embedBuilder = new DiscordEmbedBuilder
            {
                Title        = title,
                Color        = Config.Colors.DownloadLinks,
                ThumbnailUrl = thumbnailUrl,
            };

            if (pkgs?.Length > 1)
            {
                var pages = pkgs.Length / EmbedPager.MaxFields + (pkgs.Length % EmbedPager.MaxFields == 0 ? 0 : 1);
                if (pages > 1)
                {
                    embedBuilder.Title = $"{title} [Part 1 of {pages}]".Trim(EmbedPager.MaxFieldTitleLength);
                }
                embedBuilder.Description = $"Total download size of all {pkgs.Length} packages is {pkgs.Sum(p => p.Size).AsStorageUnit()}";
                var i = 0;
                do
                {
                    var pkg = pkgs[i++];
                    embedBuilder.AddField($"Update v{pkg.Version} ({pkg.Size.AsStorageUnit()})", $"⏬ [{GetLinkName(pkg.Url)}]({pkg.Url})");
                    if (i % EmbedPager.MaxFields == 0)
                    {
                        result.Add(embedBuilder);
                        embedBuilder = new DiscordEmbedBuilder
                        {
                            Title        = $"{title} [Part {i/EmbedPager.MaxFields+1} of {pages}]".Trim(EmbedPager.MaxFieldTitleLength),
                            Color        = Config.Colors.DownloadLinks,
                            ThumbnailUrl = thumbnailUrl,
                        };
                    }
                } while (i < pkgs.Length);
            }
            else if (pkgs?.Length == 1)
            {
                embedBuilder.Title       = $"{title} update v{pkgs[0].Version} ({pkgs[0].Size.AsStorageUnit()})";
                embedBuilder.Description = $"⏬ [{Path.GetFileName(GetLinkName(pkgs[0].Url))}]({pkgs[0].Url})";
            }
            else
            {
                embedBuilder.Description = "No updates were found";
            }
            if (!result.Any() || embedBuilder.Fields.Any())
            {
                result.Add(embedBuilder);
            }
            return(result);
        }
Exemple #2
0
        public static DiscordEmbedBuilder AsEmbed(this TitleInfo info, string titleId, string gameTitle = null, bool forLog = false, string thumbnailUrl = null)
        {
            if (info.Status == TitleInfo.Maintenance.Status)
            {
                return new DiscordEmbedBuilder {
                           Description = "API is undergoing maintenance, please try again later.", Color = Config.Colors.Maintenance
                }
            }
            ;

            if (info.Status == TitleInfo.CommunicationError.Status)
            {
                return new DiscordEmbedBuilder {
                           Description = "Error communicating with compatibility API, please try again later.", Color = Config.Colors.Maintenance
                }
            }
            ;

            if (string.IsNullOrWhiteSpace(gameTitle))
            {
                gameTitle = null;
            }

            if (StatusColors.TryGetValue(info.Status, out var color))
            {
                // apparently there's no formatting in the footer, but you need to escape everything in description; ugh
                var productCodePart = string.IsNullOrWhiteSpace(titleId) ? "" : $"[{titleId}] ";
                var pr   = info.ToPrString(null, true);
                var desc = $"{info.Status} since {info.ToUpdated()}";
                if (pr is string _)
                {
                    desc += $" (PR {pr})";
                }
                if (!forLog && !string.IsNullOrEmpty(info.AlternativeTitle))
                {
                    desc = info.AlternativeTitle + Environment.NewLine + desc;
                }
                if (!string.IsNullOrEmpty(info.WikiTitle))
                {
                    desc += $"{(forLog ? ", " : Environment.NewLine)}[Wiki Page](https://wiki.rpcs3.net/index.php?title={info.WikiTitle})";
                }
                return(new DiscordEmbedBuilder
                {
                    Title = $"{productCodePart}{(gameTitle ?? info.Title).Trim(200)}",
                    Url = $"https://forums.rpcs3.net/thread-{info.Thread}.html",
                    Description = desc,
                    Color = color,
                    ThumbnailUrl = thumbnailUrl
                });
            }
            else
            {
                var desc = "";
                if (!string.IsNullOrEmpty(titleId))
                {
                    desc = $"Product code {titleId} was not found in compatibility database";
                }
                var result = new DiscordEmbedBuilder
                {
                    Description  = desc,
                    Color        = Config.Colors.CompatStatusUnknown,
                    ThumbnailUrl = thumbnailUrl,
                };

                if (gameTitle == null && ThumbnailProvider.GetTitleName(titleId) is string titleName && !string.IsNullOrEmpty(titleName))
                {
                    gameTitle = titleName;
                }
                if (!string.IsNullOrEmpty(gameTitle))
                {
                    var productCodePart = string.IsNullOrEmpty(titleId) ? "" : $"[{titleId}] ";
                    result.Title = $"{productCodePart}{gameTitle.Sanitize().Trim(200)}";
                }
                return(result);
            }
        }