2017-07-30 62 views
0

我目前正在製作一個不協調機器人的過程。我希望它能夠通過特定的查詢訪問API,然後在聊天中顯示響應。對於這一點,我做了這個命令:不和諧不發送特定消息

commands.CreateCommand("card").Parameter("user", ParameterType.Unparsed).Do(async (e) => 
    { 
     string message = e.Args[0]; 

     await e.Channel.SendMessage("https://omgvamp-hearthstone-v1.p.mashape.com/cards/" + message + "?collectible=1"); 

     var r = await Get<RootObject>("https://omgvamp-hearthstone-v1.p.mashape.com/cards/" + message + "?collectible=1"); 
     Console.Write("Hold it!\n"); 
     await e.Channel.SendMessage(r.img); 
    }); 

而且功能也引用:

public async Task<RootObject> Get<RootObject>(string url) 
    { 
     using (HttpClient client = new HttpClient()) 
     { 
      client.DefaultRequestHeaders.TryAddWithoutValidation("X-Mashape-Key", "xxxxx"); 
      client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json"); 
      Console.Write("Hang on!\n"); 
      var json = await client.GetStringAsync(url); 
      Console.Write("Not so fast!\n"); 
      return JsonConvert.DeserializeObject<RootObject>(json); 
     } 
    } 

但是,當我發送命令,只發回第一鏈接以及杭上,而不是如此快速的消息。我想要的信息,r.img,只是不發送。我如何更改代碼,以便用我想要的信息做出響應?

的響應,順便:

[ 
    { 
     "cardId":"EX1_572", 
     "dbfId":"1186", 
     "name":"Ysera", 
     "cardSet":"Classic", 
     "type":"Minion", 
     "faction":"Neutral", 
     "rarity":"Legendary", 
     "cost":9, 
     "attack":4, 
     "health":12, 
     "text":"At the end of your turn, add_a Dream Card to_your hand.", 
     "flavor":"Ysera rules the Emerald Dream. Which is some kind of green-mirror-version of the real world, or something?", 
     "artist":"Gabor Szikszai", 
     "collectible":true, 
     "elite":true, 
     "race":"Dragon", 
     "playerClass":"Neutral", 
     "img":"http://media.services.zam.com/v1/media/byName/hs/cards/enus/EX1_572.png", 
     "imgGold":"http://media.services.zam.com/v1/media/byName/hs/cards/enus/animated/EX1_572_premium.gif", 
     "locale":"enUS" 
    } 
] 

而且RootObject.cs:

namespace DiscordBot 
{ 
    public class RootObject 
    { 
     public string cardId { get; set; } 
     public string dbfId { get; set; } 
     public string name { get; set; } 
     public string cardSet { get; set; } 
     public string type { get; set; } 
     public string faction { get; set; } 
     public string rarity { get; set; } 
     public int cost { get; set; } 
     public int attack { get; set; } 
     public int health { get; set; } 
     public string text { get; set; } 
     public string flavor { get; set; } 
     public string artist { get; set; } 
     public bool collectible { get; set; } 
     public bool elite { get; set; } 
     public string race { get; set; } 
     public string playerClass { get; set; } 
     public string img { get; set; } 
     public string imgGold { get; set; } 
     public string locale { get; set; } 
    } 
} 
+0

這有什麼做用C#,它是在你使用該庫的問題。我建議你調試你的代碼,看看發生了什麼 –

+0

@CamiloTerevinto我把所有的語句都暫停了,雖然它暫停在Get 函數中,並且在執行這個函數時,它不會在後面的語句中暫停。 [這是它暫停的地方,從上面第二和第三個暫停沒有註冊。](http://i.imgur.com/wKzZPth.png) – RobotGandhi

+0

@CamiloTerevinto雖然調試數據沒有什麼奇怪的,但至少沒有公然地明顯。 – RobotGandhi

回答

0

JSON的你用[其指示它是一個數組,而不是當它提供開始以指示對象的{}開始。

所以不是

JsonConvert.DeserializeObject<RootObject>(json); 

您應該使用

JsonConvert.DeserializeObject<RootObject[]>(json);