如何在 C# Discord Bot 上使用 JSON API

how to use JSON API on C# Discord Bot

我正在尝试制作一个 discord 机器人,它使用 JSON API.

根据给定的命令参数查找用户信息

假设我想查找名为 mellin111

的用户

的 api 将是 https://www.habbo.com/api/public/users?name=mellin111。 我也在使用 bunny83 https://github.com/Bunny83/SimpleJSON

的 SimpleJSON

这是我的部分代码:

[Command("search", RunMode = RunMode.Async)]

        public async Task Search([Remainder] string echo)
        {
            string url = @"https://www.habbo.com/api/public/users?name=" + echo;
            // GET request
            WebClient ipcon = new WebClient();
            string response = ipcon.DownloadString(url);
            // Parse json
            dynamic json = JSON.Parse(response);

            var eb = new EmbedBuilder();
            eb
            .WithTitle("Search Result")
            .AddField("**Habbo IGN: **", "" + json["name"], false)
            .AddField("**Motto: **", "" + json["motto"], false)
            .AddField("**Member since: **", "" + json["memberSince"], false)
            .WithFooter("made with )
            //.WithThumbnailUrl("https://www.habbo.com/habbo-imaging/avatarimage?size=m&figure=" + json["figureString"])
            .WithCurrentTimestamp()
            .WithColor(Color.Gold);
            await ReplyAsync(embed: eb.Build());
         }

尽管此代码没有错误,但似乎return 没有响应。还有其他方法可以从 api 获取数据吗?

API 似乎限制了来自特定客户端的请求。
如果你设置一个正常的用户代理,它似乎工作得很好。

直接来自 WebClient 的示例 Class' documentation:

var url = "https://www.habbo.com/api/public/users?name=mellin111";
var ipcon = new WebClient();
ipcon.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
var response = ipcon.DownloadString(url);
var json = JSON.Parse(response);
var yup_this_works = (DateTime)json["memberSince"];
//works assuming the "SimpleJSON" class is used like this, I havent personally used it