예제 #1
0
        // 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))
                         ?? await ThumbnailProvider.GetTitleNameAsync(productCode, Config.Cts.Token).ConfigureAwait(false)
                         ?? productCode;

            var thumbnailUrl = await client.GetThumbnailUrlAsync(productCode).ConfigureAwait(false);

            var embedBuilder = new DiscordEmbedBuilder
            {
                Title = title,
                Color = Config.Colors.DownloadLinks,
            }.WithThumbnail(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,
                        }.WithThumbnail(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 if (patch != null)
            {
                embedBuilder.Description = "No updates available";
            }
            else
            {
                embedBuilder.Description = "No update information available";
            }
            if (!result.Any() || embedBuilder.Fields.Any())
            {
                result.Add(embedBuilder);
            }
            return(result);
        }
 public static DiscordEmbedBuilder AsEmbed(this TitleInfo info, string titleId, string gameTitle = null, bool forLog = false, string thumbnailUrl = null)
 {
     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 onlineOnlypart  = info.Network == true ? " 🌐" : "";
         var pr   = info.ToPrString(null, true);
         var desc = $"{info.Status} since {info.ToUpdated() ?? "forever"}";
         if (pr != null)
         {
             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})";
         }
         var cacheTitle = info.Title ?? gameTitle;
         if (!string.IsNullOrEmpty(cacheTitle))
         {
             StatsStorage.GameStatCache.TryGetValue(cacheTitle, out int stat);
             StatsStorage.GameStatCache.Set(cacheTitle, ++stat, StatsStorage.CacheTime);
         }
         var title = $"{productCodePart}{cacheTitle.Trim(200)}{onlineOnlypart}";
         if (string.IsNullOrEmpty(title))
         {
             desc = "";
         }
         return(new DiscordEmbedBuilder
         {
             Title = title,
             Url = info.Thread > 0 ? $"https://forums.rpcs3.net/thread-{info.Thread}.html" : null,
             Description = desc,
             Color = color,
         }.WithThumbnail(thumbnailUrl));
     }
     else
     {
         var desc       = "";
         var embedColor = Config.Colors.Maintenance;
         if (info.Status == TitleInfo.Maintenance.Status)
         {
             desc = "API is undergoing maintenance, please try again later.";
         }
         else if (info.Status == TitleInfo.CommunicationError.Status)
         {
             desc = "Error communicating with compatibility API, please try again later.";
         }
         else
         {
             embedColor = Config.Colors.CompatStatusUnknown;
             if (!string.IsNullOrEmpty(titleId))
             {
                 desc = $"Product code {titleId} was not found in compatibility database";
             }
         }
         var result = new DiscordEmbedBuilder
         {
             Description = desc,
             Color       = embedColor,
         }.WithThumbnail(thumbnailUrl);
         if (gameTitle == null &&
             ThumbnailProvider.GetTitleNameAsync(titleId, Config.Cts.Token).ConfigureAwait(false).GetAwaiter().GetResult() is string titleName &&
             !string.IsNullOrEmpty(titleName))
         {
             gameTitle = titleName;
         }
         if (!string.IsNullOrEmpty(gameTitle))
         {
             StatsStorage.GameStatCache.TryGetValue(gameTitle, out int stat);
             StatsStorage.GameStatCache.Set(gameTitle, ++stat, StatsStorage.CacheTime);
             var productCodePart = string.IsNullOrEmpty(titleId) ? "" : $"[{titleId}] ";
             result.Title = $"{productCodePart}{gameTitle.Sanitize().Trim(200)}";
         }
         return(result);
     }
 }