示例#1
0
        protected virtual async Task RenderAsync(SocketMessageComponent interaction)
        {
            var embed      = BuildEmbed();
            var components = BuildComponents(interaction.Channel, interaction.User);

            await interaction.UpdateAsync(message =>
            {
                message.Embed      = embed;
                message.Components = components;
            }).ConfigureAwait(false);
        }
        // Print a movie to an interaction
        public static async Task PrintToInteraction(SocketMessageComponent interaction)
        {
            var movie = new Movie {
                Url = $"https://www.rottentomatoes.com/m/{interaction.Data.CustomId}"
            };

            // Get the HTML & JSON from the RT page
            var rawHTML = await WebUtils.DownloadString(movie.Url);

            var html = new HtmlDocument();

            html.LoadHtml(rawHTML);

            // Get the JSON from the HTML
            dynamic JSON = JsonConvert.DeserializeObject(rawHTML.CutBeforeAndAfter("<script id=\"score-details-json\" type=\"application/json\">", "</script>"));

            // Title
            movie.Name = HttpUtility.HtmlDecode(JSON.scoreboard.title.ToString());

            // Tomatometer
            movie.CriticScore = JSON.scoreboard.tomatometerScore == null ? "N/A" : $"{JSON.scoreboard.tomatometerScore}%";

            // Year
            movie.Year = JSON.scoreboard.info.ToString();

            switch (JSON.scoreboard.tomatometerState.ToString())
            {
            case "certified-fresh":
                movie.CriticScoreIcon = "<:certified_fresh:737761619375030422>";
                break;

            case "fresh":
                movie.CriticScoreIcon = "<:fresh:737761619299270737>";
                break;

            case "rotten":
                movie.CriticScoreIcon = "<:rotten:737761619299532874>";
                break;

            default:
                movie.CriticScoreIcon = "<:notomatometer:891357892417028127>";
                break;
            }

            // Audience Score
            movie.AudienceScore = JSON.modal.audienceScoreAll.score == null ? "N/A" : $"{JSON.modal.audienceScoreAll.score}%";

            // Audience Score Icon
            switch (JSON.modal.audienceScoreAll.audienceClass.ToString())
            {
            case "upright":
                movie.AudienceIcon = "<:audienceliked:737761619328761967>";
                break;

            case "spilled":
                movie.AudienceIcon = "<:audiencedisliked:737761619416842321>";
                break;

            default:
                movie.AudienceIcon = "<:noaudiencescore:891357892442198047>";
                break;
            }

            // Critic Consensus
            movie.CriticsConsensus = html.Text.Contains("<span data-qa=\"critics-consensus\">") ?
                                     html.DocumentNode.SelectSingleNode("//p[contains(@class, 'what-to-know__section-body')]").InnerText.Trim().Replace("Read critic reviews", "") :
                                     "No consensus yet.";

            movie.Poster = rawHTML.CutBefore("class=\"posterImage js-lazy")
                           .CutBefore("data-src=\"")
                           .CutAfter("\"");

            // Create a pretty embed & send it
            await interaction.UpdateAsync(x =>
            {
                x.Components = new ComponentBuilder().WithButton("View on RottenTomatoes.com", style: ButtonStyle.Link, url: movie.Url).Build();
                x.Content    = null;
                x.Embed      = new EmbedBuilder()
                               .WithTitle($"{movie.Name} - {movie.Year}")
                               .WithColor(EmbedUtils.Red)
                               .WithThumbnailUrl(movie.Poster)
                               .AddField("Tomatometer", $"{movie.CriticScoreIcon} {movie.CriticScore}")
                               .AddField("Audience Score", $"{movie.AudienceIcon} {movie.AudienceScore}")
                               .AddField("Critics Consensus", movie.CriticsConsensus)
                               .Build();
            });
        }