forked from adam/discord-bot-shtik
workaround for an exception being thrown when shouldn't
This commit is contained in:
parent
4f70bba30c
commit
bc3e583926
26
Program.cs
26
Program.cs
@ -124,41 +124,43 @@ namespace silverworker_discord
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async void scryfallSearch(string cardName, SocketUserMessage message)
|
private async void scryfallSearch(string cardName, SocketUserMessage message)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var request = WebRequest.Create("https://api.scryfall.com/cards/named?fuzzy=" + cardName.Replace(' ', '+'));
|
var request = WebRequest.Create("https://api.scryfall.com/cards/named?fuzzy=" + cardName.Replace(' ', '+'));
|
||||||
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
|
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
|
||||||
if(response.StatusCode == HttpStatusCode.OK)
|
|
||||||
{
|
|
||||||
using (var dataStream = new StreamReader(response.GetResponseStream()))
|
using (var dataStream = new StreamReader(response.GetResponseStream()))
|
||||||
{
|
{
|
||||||
string responseFromServer = dataStream.ReadToEnd();
|
string responseFromServer = dataStream.ReadToEnd();
|
||||||
var cardObj = JsonConvert.DeserializeObject<Scryfalltypes.Card>(responseFromServer);
|
var cardObj = JsonConvert.DeserializeObject<Scryfalltypes.Card>(responseFromServer);
|
||||||
if(cardObj != null){
|
if (cardObj != null)
|
||||||
|
{
|
||||||
if (cardObj.image_uris.png == null)
|
if (cardObj.image_uris.png == null)
|
||||||
{
|
{
|
||||||
await mtgChannel.SendMessageAsync("I know that card, but no image.");
|
await mtgChannel.SendMessageAsync("I know that card, but no image.");
|
||||||
}
|
}
|
||||||
else{
|
else
|
||||||
|
{
|
||||||
using (var cardImgDataStream = WebRequest.Create(cardObj.image_uris.png).GetResponse().GetResponseStream())
|
using (var cardImgDataStream = WebRequest.Create(cardObj.image_uris.png).GetResponse().GetResponseStream())
|
||||||
{
|
{
|
||||||
await mtgChannel.SendFileAsync(cardImgDataStream, $"{cardName}.png");
|
await mtgChannel.SendFileAsync(cardImgDataStream, $"{cardName}.png");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}else{
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Console.WriteLine($"weird 404 searching for card {cardName}");
|
Console.WriteLine($"weird 404 searching for card {cardName}");
|
||||||
await mtgChannel.SendMessageAsync("¯\\_(ツ)_/¯");
|
await mtgChannel.SendMessageAsync("¯\\_(ツ)_/¯");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else if(response.StatusCode == HttpStatusCode.NotFound)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"regular 404 searching for card {cardName}");
|
|
||||||
await mtgChannel.SendMessageAsync("¯\\_(ツ)_/¯");
|
await mtgChannel.SendMessageAsync("¯\\_(ツ)_/¯");
|
||||||
}
|
Console.Error.WriteLine("who's fucking idea was it that on 404 you THROW AN EXCEPTION, even though there's a way to read the status code?");
|
||||||
else
|
Console.Error.Write(e);
|
||||||
{
|
|
||||||
Console.Error.WriteLine("idgi but something happened.");
|
|
||||||
await message.AddReactionAsync(Emote.Parse("<:problemon:859453047141957643>"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user