Moved joke message handler to separate class for organization

This commit is contained in:
Deukhoofd 2018-08-12 13:17:10 +02:00
parent a898e69b58
commit 2787fab689
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
2 changed files with 73 additions and 60 deletions

View File

@ -0,0 +1,71 @@
using System.Linq;
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);
}
}
}

View File

@ -23,8 +23,8 @@ namespace DeukBot4.MessageHandlers
CommandHandler.CommandHandler.HandleMessage(message); CommandHandler.CommandHandler.HandleMessage(message);
HandlePrivateMessage(message); HandlePrivateMessage(message);
ImageBackupHandler.Backup(message); ImageBackupHandler.Backup(message);
DeltaHandler(message); JokeHandlers.DeltaHandler(message);
DadJokeHandler(message); JokeHandlers.DadJokeHandler(message);
#pragma warning restore 4014 #pragma warning restore 4014
} }
catch (Exception e) catch (Exception e)
@ -62,63 +62,5 @@ namespace DeukBot4.MessageHandlers
} }
} }
private 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");
}
}
private 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);
}
} }
} }