Skip to content

Commit

Permalink
error handling stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ExtraConcentratedJuice committed Jul 24, 2018
1 parent 30f80e3 commit d5e53da
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 853 deletions.
29 changes: 25 additions & 4 deletions Assets/poke.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"Stoutland":"0x5A5FFF7E8774395E3E4568005C005B4257624473408847935681586153554C4C54436A3678426D5D",
"Gorebyss":"0x69FFB600AB3C606F697763676A6C68626E67676673676A6B6D69696570686C676C6368676A696E69",
"Drilbur":"0xDABA00CF03F889C9ECC9F2DBFDFFD0D1BBD2DFD4DBD2D0D3EDD6D5F0D7E0E3E0E2D8DFE1D7D8D8D8",
"Incineroar":"0x949C00FFEE71806CDEDA7781CB53AE6F82AB8D8F868995AB7B9393A38F82A78C85A2978F9E8B84A1",
"Incineroar":"0x7C7B00FFC2497053D1C2486EBB35915A668D766F787775906A737D867A7387776D878473857B7188",
"Blissey":"0xA786FFBE00D287CEF5BD70C0B89C5CB59C948CA1A5ABB4ACA7B6B9A2A3ABAB9EA0A0A6999EA5A5A1",
"Pignite":"0x97CEE7B7B50B85CA00855785FADD173AFFF46443A8CC85699ABB988A7AA6B0877391B6A26C97C897",
"Magneton":"0xD2EE1ABAF88C00B85AFE99C7FEDAE6DEE3C7CBD3CCDFD2D8C9EFCBD2CAD7CBBBC9D9CCD9C4CDC0CF",
Expand Down Expand Up @@ -766,8 +766,8 @@
"Mantyke":"0xC0B6008F65A991CFA4F6DEBBFFC7B0E3A0CCA6BBABBFB5B9C2C1C3C3CCC2C0C4C3BFB7C4BCB5C4BA",
"Tyrogue":"0xA4FF0000E9E8C4A1B26DA0BE76A09BC5B7C5A1967BAF9E63CD9EA8C78EB0A09EAC9E95B0A09BBB9B",
"Oricorio":"0x4B00CD22FFCB57411B901E69613F371D6E185C2B5C4D465E335F33424A425138674A5A4855544A5B",
"Nidoran":"0x3C1AFF463614545F4B7D544900653342392D501B192A4D36442743403A20444F4D3D303D3E493144",
"Nidoran":"0x7700FF6C51744B9FCA798165839F6B7A787B6B67767A7A785F828172797B75866C7B707181826983",
"Nidoran":"0x6E00FF57416D369CCD667D59809464786D75615E717A6A70587E776C6C746D7D6073656A76766179",
"Nidoran":"0x3613FF432C0D4F574F734E4300632E3E362B4C141127432F3824403D35173D4746342C3A3944283E",
"Deerling":"0x74FF0056470A6D67539485787371887D7672786477706C7A757074737A7176787A69737971717479",
"Meowstic":"0x4D66FF2B31003293042F604C512C49383A5B4E4F42695839446856594A59593C56504B444E62443E",
"Shaymin":"0xB42549B900BEBDFFEDB3AABEB5C1A8C1B5C69EB59BAFA2AFAEA6BDB4B4AEBAB2B7B1B7B2B8B1B7BA",
Expand All @@ -784,5 +784,26 @@
"Ho-Oh":"0x620018FF510E5A657F70505A625A5F6F5D665F54705C685F5966626467645C63626167665B67615D",
"Giratina":"0x5D71002AFF4186AB573A60694E4F505D1F686E486365465C595C524650655160696854565650555D",
"Keldeo":"0xA500E1FC27FFC8AFC3B097A0B4A9B5A2A3B2A1A7BEA2BDAFB6A1B4A5A1AAA29FA99D9C9AA19DA3A9",
"Meloetta":"0x60660C5DFFE74F000E60647E726D94254D49698B624E682775804A78436E704C6A64557155546D59"
"Meloetta":"0x60660C5DFFE74F000E60647E726D94254D49698B624E682775804A78436E704C6A64557155546D59",
"Poipole":"0x7F95FF00F0839195516896A17F94687F877B7E8097905C7986838B807B817C867A79858181877E7A",
"Mimikyu":"0x940014FF8C73D9BD78998CA7968597AC7C97979C8F879B9F8888A9908E9399959191979B90929898",
"Stakataka":"0x6E6C4F004081A663A1FFB42C09E6D83F2984A550687C726C6C645964686D68616C6E70676E70756A",
"Tapu Bulu":"0x66FF37943F2A6800435338766F9E6D6D935B824B565B41685A507D59766F617E675D6558695E5667",
"Blacephalon":"0x6BFF2A59DA00738C9F22345E667C61995D6C6C6F625A6676717D6F7E6E7366635E67666666696F64",
"Naganadel":"0x950084F4D2DE13FF8C62A37ACE6675B77C9D72AD987AAA7CB07B9D978BA381A6859E928DA386A08B",
"Tapu Koko":"0x9036357393D3FF9800A943BCE379839A8B9E998D837585A487828CAEB07A838E8CB1917D92839BA2",
"Tapu Fini":"0x61F3990B3CFFF44250036CC25D3A0C00575A72586B5C7171723952905643856658656C6D425B6F55",
"Tapu Lele":"0xAB9B9BFFD900D6A577F4AEB0BAD8849EC1C5B891B8A7A2A3B6BDA9A3A7A7A9AEB0AEA4AAAAAFACA7",
"Landorus":"0x4DDBFF2013005A3A6C63575C4A4F4A4A4D414B474B544D4D504B484E4C504C4D4E4E504E4E4E4F4C",
"Zeraora":"0xA3003AB29D778CFFAAC18EC184B89CBD7C8F9CABAAA39EB9A5AEA2A09D98A2A7A4A3A1ACA6A49F9B",
"Meloetta":"0x596F0956FFC75C00105D65626866892B43397481504B563C6A6F4A6D3B6E6747654B5F654C5E5C52",
"Terrakion":"0xA700FF9286A4B6F3B8C499B7A5AEA3AFA6B6AAA49AA5B3A8A7A9A5ACA2A4AAACAAA7A1A7A3AAA7A9",
"Tornadus":"0x72FF76B6004A31206C617C6F7F667A7565697B74757A7E716C70756C7668726F6B6F756F70727773",
"Thundurus":"0xC28200FE45CCC970D7F0E7BAB9DCB4BEC1C0BBBFD4B7BBCFC2C2BEC8C3B5BEC3BCC4C4C8C5C2C4C6",
"Cherrim":"0x4B21FF7F002345736D35416A4E2D385659444454554C424051574A4F4C444C4949494A534D494B4B",
"Gourgeist":"0x9900E8FEC13930E8B3FF5BC5EC52D5AA878B86939398A79A829FA09DA0938CA1978DA08E929C9E97",
"Pumpkaboo":"0xE2AC00EE26F1CEE4D09FB2FBB4FFDFF3EDE1D8ECCEE7E3EAF2E7D4EBEBE5DCD4F0E6E0DEE6DCE3E7",
"Wishiwashi":"0xB19600511C412BFA52EF61E8B9FFDACEBF9CBDA9CAAFAA9EA5B7BFBAB6ABAFAFAEB4B0B3AEAFB2B6",
"Wormadam":"0x83F2C12F006492FF3477E68FD1449369854A9D9B99678C6A7795847A958777758F8B74868B8A7B8C",
"Type: Null":"0x89007176FFE6AA5BE49B939BA063989A847F878E9A838C93769C96927E93898F8D937D9193878B8B"
}
17 changes: 17 additions & 0 deletions PokecordCatcher/MessagePredicates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Discord.WebSocket;
using System.Linq;

namespace PokecordCatcherBot
{
public static class MessagePredicates
{
public static bool TradeMessage(SocketMessage x, SocketMessage context) =>
x.Channel.Id == context.Channel.Id && x.Author.Id == PokecordCatcher.POKECORD_ID && x.Embeds.Count > 0 && x.Embeds.First().Title?.StartsWith("Trade between ") == true;

public static bool PokemonListingMessage(SocketMessage x, SocketMessage context) =>
x.Channel.Id == context.Channel.Id && x.Author.Id == PokecordCatcher.POKECORD_ID && x.Embeds.Count > 0 && x.Embeds.First().Title?.StartsWith("Your") == true;

public static bool SuccessfulCatchMessage(SocketMessage x, SocketMessage context, ulong userId) =>
x.Channel.Id == context.Channel.Id && x.Author.Id == PokecordCatcher.POKECORD_ID && x.MentionedUsers.Any(y => y.Id == userId) && x.Content.StartsWith("Congratulations");
}
}
14 changes: 2 additions & 12 deletions PokecordCatcher/PokecordCatcher.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
using Discord;
using Discord.WebSocket;
using Newtonsoft.Json;
using PokecordCatcherBot.Models;
using PokecordCatcherBot.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using Shipwreck.Phash;
using Shipwreck.Phash.Bitmaps;
using Discord.Rest;
using PokecordCatcherBot.Models;
using PokecordCatcherBot.Modules;

namespace PokecordCatcherBot
{
Expand Down
14 changes: 11 additions & 3 deletions PokecordCatcher/PokemonComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public string GetPokemon(Stream image)
return Compare(hash);
}

private string Compare(Digest hash)
private string Compare(Digest hash, double minSimilarity = 0)
{
Dictionary<string, double> similarities = new Dictionary<string, double>();

Expand All @@ -57,9 +57,17 @@ private string Compare(Digest hash)
}

var sim = similarities.OrderByDescending(x => x.Value).First();
Console.WriteLine($"Detected '{sim.Key}' with a similarity of {sim.Value}");

return sim.Key;
if (sim.Value >= minSimilarity)
{
Console.WriteLine($"Detected '{sim.Key}' with a similarity of {sim.Value}");
return sim.Key;
}
else
{
Console.WriteLine($"Failed to find a Pokemon that satisfies the minimum similarity of {minSimilarity}.");
return null;
}
}
}
}
5 changes: 1 addition & 4 deletions PokecordCatcher/ResponseGrabber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ public class ResponseGrabber
{
private readonly DiscordSocketClient client;

public ResponseGrabber(DiscordSocketClient client)
{
this.client = client;
}
public ResponseGrabber(DiscordSocketClient client) => this.client = client;

public Task<SocketMessage> SendMessageAndGrabResponse(ITextChannel channel, string msg, Func<SocketMessage, bool> predicate, double timeout) =>
GrabResponse(async () => await channel.SendMessageAsync(msg), predicate, timeout);
Expand Down
4 changes: 2 additions & 2 deletions PokecordCatcher/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace PokecordCatcherBot
{
Expand All @@ -15,8 +16,7 @@ public class Service
protected State State { get => bot.State; }
protected ResponseGrabber ResponseGrabber { get => bot.ResponseGrabber; }

public Service(PokecordCatcher bot) => this.bot = bot;

private Service() { }
public Service(PokecordCatcher bot) => this.bot = bot;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Text;
using System.Threading.Tasks;

namespace PokecordCatcherBot.Modules
namespace PokecordCatcherBot.Services
{
public class CatcherService : Service
{
Expand All @@ -21,7 +21,9 @@ public CatcherService(PokecordCatcher bot, string hashPath) : base(bot)
comparer = new PokemonComparer(LoadPokemon(hashPath));
http = new HttpClient();

Client.MessageReceived += async x => Task.Run(async () => await OnMessage(x));

Client.MessageReceived += async x => Task.Run(async () => await OnMessage(x))
.ContinueWith(t => Console.WriteLine(t.Exception.Flatten().InnerException), TaskContinuationOptions.OnlyOnFaulted);
}

private async Task OnMessage(SocketMessage msg)
Expand Down Expand Up @@ -66,7 +68,7 @@ private async Task OnMessage(SocketMessage msg)
var resp = await ResponseGrabber.SendMessageAndGrabResponse(
(ITextChannel)msg.Channel,
$"{Configuration.PokecordPrefix}catch {name}",
x => x.Channel.Id == msg.Channel.Id && x.Author.Id == PokecordCatcher.POKECORD_ID && x.MentionedUsers.Any(y => y.Id == Client.CurrentUser.Id) && x.Content.StartsWith("Congratulations"),
x => MessagePredicates.SuccessfulCatchMessage(x, msg, Client.CurrentUser.Id),
5
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using PokecordCatcherBot.Attributes;
using PokecordCatcherBot.Models;

namespace PokecordCatcherBot.Modules
namespace PokecordCatcherBot.Services
{
public class CommandService : Service
{
Expand All @@ -21,7 +21,7 @@ public CommandService(PokecordCatcher bot) : base(bot)
{
commandMethods = FindCommandMethods();

Client.MessageReceived += async x => Task.Run(async () => await OnMessage(x));
Client.MessageReceived += OnMessage;
}

private async Task OnMessage(SocketMessage msg)
Expand All @@ -39,22 +39,19 @@ private async Task OnMessage(SocketMessage msg)
{
try
{
var t = (Task)command.Invoke(this, new object[] { msg, args.ToArray() });
await t;
var cmdTask = (Task)command.Invoke(this, new object[] { msg, args.ToArray() });

var task = Task.Run(async () => await cmdTask)
.ContinueWith(t => Console.WriteLine(t.Exception.Flatten().InnerException), TaskContinuationOptions.OnlyOnFaulted);
}
catch (Exception e) { Console.WriteLine(e); }
}
}

private List<MethodInfo> FindCommandMethods()
{
List<MethodInfo> methods = new List<MethodInfo>();

methods.AddRange(typeof(PokecordCatcher).Assembly.GetTypes()
.SelectMany(x => x.GetMethods()).Where(x => x.GetCustomAttribute(typeof(CommandAttribute)) != null));

return methods;
}
private List<MethodInfo> FindCommandMethods() =>
typeof(PokecordCatcher).Assembly.GetTypes()
.SelectMany(x => x.GetMethods())
.Where(x => x.GetCustomAttribute(typeof(CommandAttribute)) != null).ToList();

[Command(nameof(Status), "Displays information about the bot's state.")]
public async Task Status(SocketMessage msg, string[] args)
Expand Down Expand Up @@ -115,7 +112,7 @@ public async Task Trade(SocketMessage msg, string[] args)
var list = await ResponseGrabber.SendMessageAndGrabResponse(
(ITextChannel)msg.Channel,
$"{Configuration.PokecordPrefix}pokemon --name {String.Join(' ', args)}",
x => x.Channel.Id == msg.Channel.Id && x.Author.Id == PokecordCatcher.POKECORD_ID && x.Embeds.Count > 0 && x.Embeds.First().Title?.StartsWith("Your") == true,
x => MessagePredicates.PokemonListingMessage(x, msg),
5
);

Expand All @@ -132,7 +129,7 @@ public async Task Trade(SocketMessage msg, string[] args)
var trade = await ResponseGrabber.SendMessageAndGrabResponse(
(ITextChannel)msg.Channel,
$"{Configuration.PokecordPrefix}trade <@{Configuration.OwnerID}>",
x => x.Channel.Id == msg.Channel.Id && x.Author.Id == PokecordCatcher.POKECORD_ID && x.Embeds.Count > 0 && x.Embeds.First().Title?.StartsWith("Trade between ") == true,
x => MessagePredicates.TradeMessage(x, msg),
5
);

Expand All @@ -149,5 +146,35 @@ public async Task Trade(SocketMessage msg, string[] args)
await msg.Channel.SendMessageAsync($"{Configuration.PokecordPrefix}confirm");
await Task.Delay(1500);
}

[Command(nameof(TradeById), "Trades pokemon with a certain ID.")]
public async Task TradeById(SocketMessage msg, string[] args)
{
if (args.Length != 1)
{
await msg.Channel.SendMessageAsync("Invalid arguments. Please supply an ID.");
return;
}

var trade = await ResponseGrabber.SendMessageAndGrabResponse(
(ITextChannel)msg.Channel,
$"{Configuration.PokecordPrefix}trade <@{Configuration.OwnerID}>",
x => MessagePredicates.TradeMessage(x, msg),
5
);

await Task.Delay(1500);

if (trade == null)
{
await msg.Channel.SendMessageAsync("Pokecord didn't create trade, aborting.");
return;
}

await msg.Channel.SendMessageAsync($"{Configuration.PokecordPrefix}p add {args[0]}");
await Task.Delay(1500);
await msg.Channel.SendMessageAsync($"{Configuration.PokecordPrefix}confirm");
await Task.Delay(1500);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Discord;
using Discord.WebSocket;

namespace PokecordCatcherBot.Modules
namespace PokecordCatcherBot.Services
{
public class SpammerService : Service
{
Expand Down
Loading

0 comments on commit d5e53da

Please sign in to comment.