workaround for an exception being thrown when shouldn't

This commit is contained in:
Adam R. Grey 2021-11-09 01:43:11 -05:00
parent 4f70bba30c
commit bc3e583926

View File

@ -46,7 +46,7 @@ namespace silverworker_discord
Console.WriteLine("Bot is connected! going to sign up for message received and user joined in client ready"); Console.WriteLine("Bot is connected! going to sign up for message received and user joined in client ready");
botChatterChannel = _client.GetChannel(ulong.Parse(config["botChatterChannel"])) as ISocketMessageChannel; botChatterChannel = _client.GetChannel(ulong.Parse(config["botChatterChannel"])) as ISocketMessageChannel;
announcementChannel = _client.GetChannel(ulong.Parse(config["announcementChannel"])) as ISocketMessageChannel; announcementChannel = _client.GetChannel(ulong.Parse(config["announcementChannel"])) as ISocketMessageChannel;
mtgChannel= _client.GetChannel(ulong.Parse(config["mtgChannel"])) as ISocketMessageChannel; mtgChannel = _client.GetChannel(ulong.Parse(config["mtgChannel"])) as ISocketMessageChannel;
_client.MessageReceived += MessageReceived; _client.MessageReceived += MessageReceived;
_client.UserJoined += UserJoined; _client.UserJoined += UserJoined;
@ -74,7 +74,7 @@ namespace silverworker_discord
{ {
await twitchery.twitcherize(type, subData); await twitchery.twitcherize(type, subData);
} }
catch(Exception e) catch (Exception e)
{ {
await message.Channel.SendMessageAsync($"aaaadam!\n{JsonConvert.SerializeObject(e)}"); await message.Channel.SendMessageAsync($"aaaadam!\n{JsonConvert.SerializeObject(e)}");
} }
@ -94,11 +94,11 @@ namespace silverworker_discord
} }
} }
} }
else if(message.Channel.Id == mtgChannel.Id) else if (message.Channel.Id == mtgChannel.Id)
{ {
Console.WriteLine("magic channel, checking if card search"); Console.WriteLine("magic channel, checking if card search");
var cardSearch = new Regex("\\[([^\\]]+)\\]").Matches(message.Content); var cardSearch = new Regex("\\[([^\\]]+)\\]").Matches(message.Content);
if(cardSearch.Count > 0) if (cardSearch.Count > 0)
{ {
Console.WriteLine($"looks like I should search scryfall for {cardSearch[0]}"); Console.WriteLine($"looks like I should search scryfall for {cardSearch[0]}");
scryfallSearch(cardSearch[0].Value, message); scryfallSearch(cardSearch[0].Value, message);
@ -125,40 +125,42 @@ namespace silverworker_discord
private async void scryfallSearch(string cardName, SocketUserMessage message) private async void scryfallSearch(string cardName, SocketUserMessage message)
{ {
var request = WebRequest.Create("https://api.scryfall.com/cards/named?fuzzy=" + cardName.Replace(' ', '+')); try
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if(response.StatusCode == HttpStatusCode.OK)
{ {
var request = WebRequest.Create("https://api.scryfall.com/cards/named?fuzzy=" + cardName.Replace(' ', '+'));
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
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>"));
} }
} }
@ -170,7 +172,7 @@ namespace silverworker_discord
ytdl.OutputFolder = ""; ytdl.OutputFolder = "";
ytdl.OutputFileTemplate = "tiktokbad.%(ext)s"; ytdl.OutputFileTemplate = "tiktokbad.%(ext)s";
var res = await ytdl.RunVideoDownload(link.ToString()); var res = await ytdl.RunVideoDownload(link.ToString());
if(!res.Success) if (!res.Success)
{ {
Console.Error.WriteLine("tried to dl, failed. \n" + string.Join('\n', res.ErrorOutput)); Console.Error.WriteLine("tried to dl, failed. \n" + string.Join('\n', res.ErrorOutput));
await message.AddReactionAsync(Emote.Parse("<:problemon:859453047141957643>")); await message.AddReactionAsync(Emote.Parse("<:problemon:859453047141957643>"));
@ -178,13 +180,13 @@ namespace silverworker_discord
else else
{ {
string path = res.Data; string path = res.Data;
if(File.Exists(path)) if (File.Exists(path))
{ {
try try
{ {
await message.Channel.SendFileAsync(path); await message.Channel.SendFileAsync(path);
} }
catch(Exception e) catch (Exception e)
{ {
await message.Channel.SendMessageAsync($"aaaadam!\n{JsonConvert.SerializeObject(e)}"); await message.Channel.SendMessageAsync($"aaaadam!\n{JsonConvert.SerializeObject(e)}");
} }