public async Task <RootWorkshop> GetWorkshopGames()
        {
            if (_workshopJsonGameData != null)
            {
                return(_workshopJsonGameData);
            }

            // So basically the only way to get game name from appid is to get a list of a user's owned games, then match our appid from the workshop item with their game (and yoink the name)
            using (var clientGame = new HttpClient())
            {
                await _log.LogMessage("Getting games from SteamAPI", color : LOG_COLOR);

                clientGame.BaseAddress = new Uri("https://api.steampowered.com/ISteamApps/GetAppList/v2/");
                var responseGame = clientGame.GetAsync("").Result;
                responseGame.EnsureSuccessStatusCode();
                var resultGame = responseGame.Content.ReadAsStringAsync().Result;

                if (resultGame == "{}")
                {
                    return(null);
                }

                _workshopJsonGameData = JsonConvert.DeserializeObject <RootWorkshop>(resultGame);
            }

            return(_workshopJsonGameData);
        }
Example #2
0
        public async Task HandleWorkshopEmbeds(SocketMessage message, DataService _data, string images = null, string testType = null)
        {
            // Cut down the message to grab just the first URL
            Match  regMatch     = Regex.Match(message.Content, @"\b((https?|ftp|file)://|(www|ftp)\.)[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]", RegexOptions.IgnoreCase);
            string workshopLink = regMatch.ToString();
            string apiKey       = _data.RSettings.ProgramSettings.SteamworksAPI;

            // Send the POST request for item info
            using (var clientItem = new HttpClient())
            {
                clientItem.BaseAddress = new Uri("https://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/v1/");
                var contentItem = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("itemcount", "1"),
                    new KeyValuePair <string, string>("publishedfileids[0]", _data.GetWorkshopIdFromFqdn(workshopLink)),
                });
                var resultItem = await clientItem.PostAsync("", contentItem);

                string resultContentItem = await resultItem.Content.ReadAsStringAsync();

                //Check if response is empty
                if (resultContentItem == "{}")
                {
                    return;
                }

                // Build workshop item embed, and set up author and game data embeds here for scoping reasons
                RootWorkshop workshopJsonItem = JsonConvert.DeserializeObject <RootWorkshop>(resultContentItem);
                RootWorkshop workshopJsonAuthor;
                RootWorkshop workshopJsonGameData;

                // Send the GET request for the author information
                using (var clientAuthor = new HttpClient())
                {
                    clientAuthor.BaseAddress = new Uri("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/");
                    HttpResponseMessage responseAuthor = clientAuthor.GetAsync($"?key={apiKey}&steamids={workshopJsonItem.response.publishedfiledetails[0].creator}").Result;
                    responseAuthor.EnsureSuccessStatusCode();
                    string resultAuthor = responseAuthor.Content.ReadAsStringAsync().Result;

                    // Don't embed anything if getting the author fails for some reason
                    if (resultAuthor == "{}")
                    {
                        return;
                    }

                    // If we get a good response though, we're gonna deserialize it
                    workshopJsonAuthor = JsonConvert.DeserializeObject <RootWorkshop>(resultAuthor);
                }

                // So basically the only way to get game name from appid is to get a list of a user's owned games, then match our appid from the workshop item with their game (and yoink the name)
                using (var clientGame = new HttpClient())
                {
                    clientGame.BaseAddress = new Uri("https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/");
                    HttpResponseMessage responseGame = clientGame.GetAsync($"?key={apiKey}&steamid={workshopJsonItem.response.publishedfiledetails[0].creator}&include_appinfo=1&appids_filter={workshopJsonItem.response.publishedfiledetails[0].consumer_app_id}").Result;
                    responseGame.EnsureSuccessStatusCode();
                    string resultGame = responseGame.Content.ReadAsStringAsync().Result;

                    // Don't embed anything if the third GET request fails (hopefully it doesn't)
                    if (resultGame == "{}")
                    {
                        return;
                    }

                    //Deserialize version 3, electric boogaloo
                    workshopJsonGameData = JsonConvert.DeserializeObject <RootWorkshop>(resultGame);
                }

                // Finally we can build the embed after too many HTTP requests
                var workshopItemEmbed = new EmbedBuilder()
                                        .WithAuthor($"{workshopJsonItem.response.publishedfiledetails[0].title}", workshopJsonAuthor.response.players[0].avatar, workshopLink)
                                        .WithTitle($"Creator: {workshopJsonAuthor.response.players[0].personaname}")
                                        .WithUrl(workshopJsonAuthor.response.players[0].profileurl)
                                        .WithImageUrl(workshopJsonItem.response.publishedfiledetails[0].preview_url)
                                        .WithColor(new Color(71, 126, 159));

                // foreach loop to pull the game name from the list of user games
                foreach (var item in workshopJsonGameData.response.games)
                {
                    if (item.appid == workshopJsonItem.response.publishedfiledetails[0].creator_app_id)
                    {
                        workshopItemEmbed.AddField("Game", item.name, true);
                        break;
                    }
                }

                // Add every other field now
                // Get tags from Json object
                workshopItemEmbed.AddField("Tags", string.Join(", ", workshopJsonItem.response.publishedfiledetails[0].tags.Select(x => x.tag)), true);

                // If test type is null or empty, it will not be included in the embed (bot only)
                if (!string.IsNullOrEmpty(testType))
                {
                    workshopItemEmbed.AddField("Test Type", testType, false);
                }

                //TODO: perhaps strip BBcodes from description?
                workshopItemEmbed.AddField("Description", workshopJsonItem.response.publishedfiledetails[0].description.Length > 497 ? workshopJsonItem.response.publishedfiledetails[0].description.Substring(0, 497) + "..." : workshopJsonItem.response.publishedfiledetails[0].description);

                // If images is null or empty, it will not be included in the embed (bot only)
                if (!string.IsNullOrEmpty(images))
                {
                    workshopItemEmbed.AddField("Links", images, false);
                }

                await message.Channel.SendMessageAsync(embed : workshopItemEmbed.Build());
            }
        }
        public async Task <RootWorkshop> GetWorkshopAuthor(RootWorkshop rootWorkshop)
        {
            RootWorkshop workshopJsonAuthor;

            // If the file is a screenshot, artwork, video, or guide we don't need to embed it because Discord will do it for us
            if (rootWorkshop.response.publishedfiledetails[0].result == 9)
            {
                return(null);
            }
            if (rootWorkshop.response.publishedfiledetails[0].filename
                .Contains("/screenshots/".ToLower()))
            {
                return(null);
            }

            var retryCount = 0;

            while (true)
            {
                // Send the GET request for the author information
                using (var clientAuthor = new HttpClient())
                {
                    string resultAuthor = null;
                    try
                    {
                        clientAuthor.BaseAddress =
                            new Uri("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/");
                        var responseAuthor = clientAuthor
                                             .GetAsync(
                            $"?key={_dataService.RSettings.ProgramSettings.SteamworksAPI}&steamids={rootWorkshop.response.publishedfiledetails[0].creator}")
                                             .Result;
                        responseAuthor.EnsureSuccessStatusCode();
                        resultAuthor = responseAuthor.Content.ReadAsStringAsync().Result;

                        // Don't embed anything if getting the author fails for some reason
                        if (resultAuthor == "{\"response\":{}}")
                        {
                            return(null);
                        }

                        // If we get a good response though, we're gonna deserialize it

                        workshopJsonAuthor = JsonConvert.DeserializeObject <RootWorkshop>(resultAuthor);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error parsing JSON from STEAM. The response was:\n" + resultAuthor);

                        if (retryCount <= 3)
                        {
                            Console.WriteLine("Retrying in 2 seconds...");
                            await Task.Delay(2000);

                            retryCount++;
                            continue;
                        }

                        //Something happened getting the response from Steam. We got a response but it wasn't valid?
                        Console.WriteLine(e);
                        Console.WriteLine("Aborting workshop embed...");
                        return(null);
                    }

                    break;
                }
            }

            return(workshopJsonAuthor);
        }