playerdb.co API not returning anything, no errors, just leaves variable empty

168 Views Asked by At

I've been trying to access an API for my discord bot to convert the input from an argument in the discord command to a username.

For example, if I were to input the discord command ?bw Tiim into discord, it would return an embed with my bedwars stats, and my username would display on there too.

Bot response to command currently:

Bot response to command currently

Bot response to command with variables:

Bot response to command with variables

The issue is I need to find the correct capitalization for the username, so let's say I run the command ?bw tiim instead, where the "t" is lowercase, the bot would still need to check for my name so I'm trying to grab data from https://playerdb.co/api/player/minecraft/<username>, which displays player UUID, username with correct capitalization, and etc but when I try to call it with the following code it returns nothing, no errors at all. I'm relatively new to coding in C# so I spent around a day trying to figure out what was going on but I haven't even gotten an idea of what may be wrong.

Code for Part of Command Calling API

 //bw stats command
        [Command("bw")]
        [Aliases("bedwars")]
        [Description("Basic Bedwars Stats")]
        public async Task bedwarsStats(CommandContext ctx, string userIdInput)
        

        {

            ApiHelper.InitializeClient();
            var hypixel = new HypixelApi(BotConfig.apikey, 300);
            var playerNameGrabberAsync = await NameProcessor.getUserInfo(userIdInput).ConfigureAwait(false);
            
            {

                var playerUserName = playerNameGrabberAsync.username;
                var playerAvatar = playerNameGrabberAsync.avatar;
                var playerNameRequestASYNC = await hypixel.GetUserByPlayerNameAsync(userIdInput/*This would preferably be the playerUserName Variable but to make it work for now I'm using the input*/).ConfigureAwait(false);


// Stats Fetcher Cut Because Unnecessary right now, would normally fetch data using Hypixel.NET wrapper

                var bwstats = new DiscordEmbedBuilder
                {
                    Title = "`playerUserName`'s " + "Bedwars Stats",
                    Description = "Overall",
                    Thumbnail = new DiscordEmbedBuilder.EmbedThumbnail { Url = playerAvatar },
                    Color = DiscordColor.Purple,
                };
                bwstats.AddField("Level", "`" + Convert.ToString(bedwarsLevel) + "✫`", true);
                bwstats.AddField("Kills", "`" + Convert.ToString(bedwarsKills) + "`", true);
                bwstats.AddField("KDR", "`" + Convert.ToString(bedwarsKDR) + "`", true);
                bwstats.AddField("FKDR", "`" + Convert.ToString(fkdrOVERALL) + "`", true);
                bwstats.AddField("Final Kills", "`" + Convert.ToString(finalKillsBedwarsOverall) + "`", true);
                bwstats.AddField("Final Deaths", "`" + Convert.ToString(finalDeathsBedwarsOverall) + "`", true);
                bwstats.AddField("WLR", "`" + Convert.ToString(wlrOVERALL) + "`", true);
                bwstats.AddField("Wins", "`" + Convert.ToString(winsOverallBedwars) + "`", true);
                bwstats.AddField("Losses", "`" + Convert.ToString(lossesOverallBedwars) + "`", true);
                bwstats.AddField("BBLR", "`" + Convert.ToString(bblrOVERALL) + "`", true);
                bwstats.AddField("Beds Broken", "`" + Convert.ToString(bedsBrokenBedwars) + "`", true);
                bwstats.AddField("Beds Lost", "`" + Convert.ToString(bedsLostBedwars) + "`", true);

ApiHelper.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace EQNX_Stats_Test
{
    public static class ApiHelper
    {
        public static HttpClient ApiClient { get; set; }
        public static void InitializeClient()
        {
            ApiClient = new HttpClient();
            ApiClient.DefaultRequestHeaders.Accept.Clear();
            ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }
    }
}

NameProcessor.cs

using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace EQNX_Stats_Test
{
    public class NameProcessor
    {
        public static async Task<userInfoGrabber> getUserInfo(string userIdInput)
        {
                string apiurl = "https://playerdb.co/api/player/minecraft/" + userIdInput;

         
                using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(apiurl))
                {
                    if (response.IsSuccessStatusCode)
                    {
                    userInfoGrabber user = await response.Content.ReadAsAsync<userInfoGrabber>();
                  
                    Console.WriteLine($"API Status: {response.StatusCode}");
                    return user;
                    }
                    else
                {
                    throw new Exception(response.ReasonPhrase);
                }

                }    
            }
        }
    }

userInfoGrabber.cs

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace EQNX_Stats_Test
{
    public class userInfoGrabber
    {
        public string username { get; set; }

        public string avatar { get; set; }

        public string id { get; set; }
    }
}
0

There are 0 best solutions below