diff --git a/DeukBot4/MessageHandlers/JokeHandlers.cs b/DeukBot4/MessageHandlers/JokeHandlers.cs deleted file mode 100644 index 8dfdab5..0000000 --- a/DeukBot4/MessageHandlers/JokeHandlers.cs +++ /dev/null @@ -1,93 +0,0 @@ -using System; -using System.Linq; -using System.Text.RegularExpressions; -using System.Threading.Tasks; -using DeukBot4.APIHandlers; -using DeukBot4.Utilities; -using Discord; -using Discord.WebSocket; - -namespace DeukBot4.MessageHandlers -{ - public static class JokeHandlers - { - public static async Task DeltaHandler(SocketMessage message) - { - var lowerCasedContent = message.Content.ToLowerInvariant().RemoveSpecialCharacters(); - if (lowerCasedContent.Contains("origin")) - return; - - var lowerSplit = lowerCasedContent.Split(' '); - if (lowerSplit.Select(s => Lehvenstein.LevenshteinDistance(s, "delta")).Any(diff => diff <= 1)) - { - var warning = "uhh excuse me it's called\n Origin and it's an art"; - await message.Channel.SendFileAsync(await CatPicHandler.GetCatPicture(warning), "cat_pic.png"); - } - } - - public static async Task DadJokeHandler(SocketMessage message) - { - if (!(message.Author is IGuildUser guildUser)) - return; - string newName; - var content = message.Content; - var lower = content.ToLowerInvariant(); - - if (lower.StartsWith("hi ")) - { - lower = lower.Remove(0, 3); - content = content.Remove(0, 3); - } - else if (lower.StartsWith("hello ")) - { - lower = lower.Remove(0, 6); - content = content.Remove(0, 6); - } - if (lower[0] != 'i') - return; - if (lower.StartsWith("i'm ")) - { - newName = content.Remove(0, 4); - } - else if (lower.StartsWith("im ")) - { - newName = content.Remove(0, 3); - } - else if (lower.StartsWith("i am ")) - { - newName = content.Remove(0, 5); - } - else - { - return; - } - if (newName.Length > 15) - return; - - await message.Channel.SendMessageAsync($"Hi {newName}, i'm Deukbot"); - await guildUser.ModifyAsync(user => user.Nickname = newName); - - } - - private static readonly Regex ThanksMatcher = new Regex(@"(?i)((\b)thanks*(\W|$)+)"); - private static readonly string[] ThanksResponses = { - "You're welcome!", "No problem", - "It was the least I could do, I always do the least I can do!", - "Yes, well, we'd be absolutely nowhere without me.", - "Yeah no problem, I'm the only one who does work around here anyway", - "I gotchu, bb." - }; - private static int _pos = 0; - public static async Task ThanksHandler(SocketMessage message) - { - var content = message.Content; - if (ThanksMatcher.IsMatch(content)) - { - var response = ThanksResponses[_pos]; - _pos = (_pos + 1) % ThanksResponses.Length; - message.Channel.SendMessageAsync(response); - } - } - - } -} \ No newline at end of file diff --git a/DeukBot4/MessageHandlers/JokeHandling/DadJoke.cs b/DeukBot4/MessageHandlers/JokeHandling/DadJoke.cs new file mode 100644 index 0000000..2bdbf02 --- /dev/null +++ b/DeukBot4/MessageHandlers/JokeHandling/DadJoke.cs @@ -0,0 +1,30 @@ +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Discord; +using Discord.WebSocket; + +namespace DeukBot4.MessageHandlers.JokeHandling +{ + internal class DadJoke : IJokeController + { + public string Id => "dad"; + public string Name => "Hi I'm dad"; + + private Regex _regex = new Regex(@".*i['| a]*m ([\w\s]{3,15})$", + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); + + public async Task Run(SocketMessage message) + { + if (!(message.Author is IGuildUser guildUser)) + return; + var content = message.Content; + var match = _regex.Match(content); + if (match.Success) + { + var newName = match.Groups[1].Value; + await message.Channel.SendMessageAsync($"Hi {newName}, i'm Deukbot"); + await guildUser.ModifyAsync(user => user.Nickname = newName); + } + } + } +} \ No newline at end of file diff --git a/DeukBot4/MessageHandlers/JokeHandling/DeltaJoke.cs b/DeukBot4/MessageHandlers/JokeHandling/DeltaJoke.cs new file mode 100644 index 0000000..bd4bbc2 --- /dev/null +++ b/DeukBot4/MessageHandlers/JokeHandling/DeltaJoke.cs @@ -0,0 +1,27 @@ +using System.Linq; +using System.Threading.Tasks; +using DeukBot4.APIHandlers; +using DeukBot4.Utilities; +using Discord.WebSocket; + +namespace DeukBot4.MessageHandlers.JokeHandling +{ + internal class DeltaJoke : IJokeController + { + public string Id => "delta"; + public string Name => "No deltas, it's called Origin"; + public async Task Run(SocketMessage message) + { + var lowerCasedContent = message.Content.ToLowerInvariant().RemoveSpecialCharacters(); + if (lowerCasedContent.Contains("origin")) + return; + + var lowerSplit = lowerCasedContent.Split(' '); + if (lowerSplit.Select(s => Lehvenstein.LevenshteinDistance(s, "delta")).Any(diff => diff <= 1)) + { + var warning = "uhh excuse me it's called\n Origin and it's an art"; + await message.Channel.SendFileAsync(await CatPicHandler.GetCatPicture(warning), "cat_pic.png"); + } + } + } +} \ No newline at end of file diff --git a/DeukBot4/MessageHandlers/JokeHandling/IJokeController.cs b/DeukBot4/MessageHandlers/JokeHandling/IJokeController.cs new file mode 100644 index 0000000..3b6ea7e --- /dev/null +++ b/DeukBot4/MessageHandlers/JokeHandling/IJokeController.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; +using Discord.WebSocket; + +namespace DeukBot4.MessageHandlers.JokeHandling +{ + public interface IJokeController + { + string Id { get; } + string Name { get; } + + Task Run(SocketMessage message); + } +} \ No newline at end of file diff --git a/DeukBot4/MessageHandlers/JokeHandling/JokeHandler.cs b/DeukBot4/MessageHandlers/JokeHandling/JokeHandler.cs new file mode 100644 index 0000000..7cbc0a5 --- /dev/null +++ b/DeukBot4/MessageHandlers/JokeHandling/JokeHandler.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Discord.WebSocket; + +namespace DeukBot4.MessageHandlers.JokeHandling +{ + public static class JokeHandler + { + public static readonly Dictionary Jokes = new Dictionary(); + + public static void Initialize() + { + var jokes = typeof(JokeHandler).Assembly.GetTypes() + .Where(x => typeof(IJokeController).IsAssignableFrom(x) && !x.IsInterface); + foreach (var joke in jokes) + { + var c = (IJokeController)Activator.CreateInstance(joke); + Jokes.Add(c.Id, c); + } + } + + public static async Task RunJokes(SocketMessage message) + { + foreach (var jokeController in Jokes) + { + jokeController.Value.Run(message); + } + } + } +} \ No newline at end of file diff --git a/DeukBot4/MessageHandlers/JokeHandling/ThanksJoke.cs b/DeukBot4/MessageHandlers/JokeHandling/ThanksJoke.cs new file mode 100644 index 0000000..ab0490b --- /dev/null +++ b/DeukBot4/MessageHandlers/JokeHandling/ThanksJoke.cs @@ -0,0 +1,33 @@ +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Discord.WebSocket; + +namespace DeukBot4.MessageHandlers.JokeHandling +{ + internal class ThanksJoke : IJokeController + { + public string Id => "thanks"; + public string Name => "Claim credit when people say thanks"; + + private readonly Regex ThanksMatcher = new Regex(@"(?i)((\b)thanks*(\W|$)+)"); + private readonly string[] ThanksResponses = { + "You're welcome!", "No problem", + "It was the least I could do, I always do the least I can do!", + "Yes, well, we'd be absolutely nowhere without me.", + "Yeah no problem, I'm the only one who does work around here anyway", + "I gotchu, bb." + }; + private static int _pos = 0; + + public async Task Run(SocketMessage message) + { + var content = message.Content; + if (ThanksMatcher.IsMatch(content)) + { + var response = ThanksResponses[_pos]; + _pos = (_pos + 1) % ThanksResponses.Length; + message.Channel.SendMessageAsync(response); + } + } + } +} \ No newline at end of file diff --git a/DeukBot4/MessageHandlers/MainHandler.cs b/DeukBot4/MessageHandlers/MainHandler.cs index 5620a2d..dd12016 100644 --- a/DeukBot4/MessageHandlers/MainHandler.cs +++ b/DeukBot4/MessageHandlers/MainHandler.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using DeukBot4.MessageHandlers.JokeHandling; using Discord; using Discord.WebSocket; @@ -22,9 +23,7 @@ namespace DeukBot4.MessageHandlers HandlePrivateMessage(message); ReminderHandler.HandleReminder(message); ImageBackupHandler.Backup(message); - JokeHandlers.DeltaHandler(message); - JokeHandlers.DadJokeHandler(message); - JokeHandlers.ThanksHandler(message); + JokeHandler.RunJokes(message); #pragma warning restore 4014 } catch (Exception e) diff --git a/DeukBot4/Program.cs b/DeukBot4/Program.cs index 244a82d..5095764 100644 --- a/DeukBot4/Program.cs +++ b/DeukBot4/Program.cs @@ -4,6 +4,7 @@ using DeukBot4.Database; using DeukBot4.Database.ServerSettings; using DeukBot4.MessageHandlers; using DeukBot4.MessageHandlers.CommandHandler; +using DeukBot4.MessageHandlers.JokeHandling; using Discord; using Discord.Commands.Builders; using Discord.WebSocket; @@ -37,6 +38,7 @@ namespace DeukBot4 Settings = Settings.FromJsonFile("settings.json"); DatabaseConnection.ConnectionString = Settings.DatabaseConnectionString; await SetupScheduler(); + JokeHandler.Initialize(); DatabaseInitializer.Initialize(); ServerSettingHandler.OnBotStartUp();