1

我一直在尝试访问我的 discord 机器人的 API,以将输入从 discord 命令中的参数转换为用户名。

例如,如果我将 discord 命令输入?bw Tiim到 discord 中,它会返回一个嵌入了我的 bedwars 统计信息,并且我的用户名也会显示在那里。

机器人当前对命令的响应:

机器人当前对命令的响应

机器人对带有变量的命令的响应:

机器人对带有变量的命令的响应

问题是我需要为用户名找到正确的大小写,所以假设我运行命令?bw tiim,其中“t”是小写的,机器人仍然需要检查我的名字,所以我试图从中获取数据https://playerdb.co/api/player/minecraft/<username>,它显示播放器 UUID、具有正确大小写的用户名等,但是当我尝试使用以下代码调用它时,它什么也不返回,根本没有错误。我对 C# 编码比较陌生,所以我花了大约一天的时间试图弄清楚发生了什么,但我什至不知道可能出了什么问题。

部分命令调用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"));
        }
    }
}

名称处理器.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; }
    }
}
4

0 回答 0