Cancel message handlers if another handler has already handled it

This commit is contained in:
Deukhoofd 2019-01-29 23:28:52 +01:00
parent a393f2ca1e
commit 9a563ba0b6
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
12 changed files with 83 additions and 36 deletions

View File

@ -42,16 +42,19 @@ namespace DeukBot4.MessageHandlers.CommandHandler
} }
public static async Task HandleMessage(SocketMessage message) public static async Task HandleMessage(ReceivedMessage receivedMessage)
{ {
if (string.IsNullOrWhiteSpace(message.Content)) if (string.IsNullOrWhiteSpace(receivedMessage.Message.Content))
return; return;
if (message.Content[0] != CommandTrigger && message.MentionedUsers.All(x => x.Id != Program.BotId)) if (receivedMessage.Message.Content[0] != CommandTrigger && receivedMessage.Message.MentionedUsers.All(x => x.Id != Program.BotId))
return; return;
if (message.Content[1] == '!') if (receivedMessage.Message.Content[1] == '!')
return; return;
receivedMessage.IsHandled = true;
var message = receivedMessage.Message;
var req = await CommandRequest.Create(message); var req = await CommandRequest.Create(message);
var resultCode = req.Response; var resultCode = req.Response;
switch (resultCode) switch (resultCode)

View File

@ -28,8 +28,9 @@ namespace DeukBot4.MessageHandlers
private static List<ServerChannelIDs> _backupChannels = new List<ServerChannelIDs>(); private static List<ServerChannelIDs> _backupChannels = new List<ServerChannelIDs>();
public static WebDavSettings Settings { private get; set; } public static WebDavSettings Settings { private get; set; }
public static async Task Backup(SocketMessage message) public static async Task Backup(ReceivedMessage receivedMessage)
{ {
var message = receivedMessage.Message;
if (!(message.Channel is IGuildChannel serverChannel)) if (!(message.Channel is IGuildChannel serverChannel))
return; return;

View File

@ -13,16 +13,19 @@ namespace DeukBot4.MessageHandlers.JokeHandling
private readonly Regex _regex = new Regex(@"(\s|^)i(`||'| a)?m (?<word>.{3,15})$", private readonly Regex _regex = new Regex(@"(\s|^)i(`||'| a)?m (?<word>.{3,15})$",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
public async Task Run(SocketMessage message) public async Task Run(ReceivedMessage message)
{ {
if (!(message.Author is IGuildUser guildUser)) if (!(message.Message.Author is IGuildUser guildUser))
return; return;
var content = message.Content; var content = message.Message.Content;
var match = _regex.Match(content); var match = _regex.Match(content);
if (match.Success) if (match.Success)
{ {
if (message.IsHandled)
return;
message.IsHandled = true;
var newName = match.Groups["word"].Value; var newName = match.Groups["word"].Value;
await message.Channel.SendMessageAsync($"Hi {newName}, i'm Deukbot"); await message.Message.Channel.SendMessageAsync($"Hi {newName}, i'm Deukbot");
await guildUser.ModifyAsync(user => user.Nickname = newName); await guildUser.ModifyAsync(user => user.Nickname = newName);
} }
} }

View File

@ -10,17 +10,22 @@ namespace DeukBot4.MessageHandlers.JokeHandling
{ {
public string Id => "delta"; public string Id => "delta";
public string Name => "No deltas, it's called Origin"; public string Name => "No deltas, it's called Origin";
public async Task Run(SocketMessage message) public async Task Run(ReceivedMessage message)
{ {
var lowerCasedContent = message.Content.ToLowerInvariant().RemoveSpecialCharacters(); var lowerCasedContent = message.Message.Content.ToLowerInvariant().RemoveSpecialCharacters();
if (lowerCasedContent.Contains("origin")) if (lowerCasedContent.Contains("origin"))
return; return;
if (message.IsHandled)
return;
var lowerSplit = lowerCasedContent.Split(' '); var lowerSplit = lowerCasedContent.Split(' ');
if (lowerSplit.Select(s => Lehvenstein.LevenshteinDistance(s, "delta")).Any(diff => diff <= 1)) if (lowerSplit.Select(s => Lehvenstein.LevenshteinDistance(s, "delta")).Any(diff => diff <= 1))
{ {
if (message.IsHandled)
return;
message.IsHandled = true;
var warning = "uhh excuse me it's called\n Origin and it's an art"; 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"); await message.Message.Channel.SendFileAsync(await CatPicHandler.GetCatPicture(warning), "cat_pic.png");
} }
} }
} }

View File

@ -8,6 +8,6 @@ namespace DeukBot4.MessageHandlers.JokeHandling
string Id { get; } string Id { get; }
string Name { get; } string Name { get; }
Task Run(SocketMessage message); Task Run(ReceivedMessage message);
} }
} }

View File

@ -23,14 +23,16 @@ namespace DeukBot4.MessageHandlers.JokeHandling
} }
} }
public static async Task RunJokes(SocketMessage message) public static async Task RunJokes(ReceivedMessage message)
{ {
if (message.Channel is IGuildChannel guildChannel) if (message.Message.Channel is IGuildChannel guildChannel)
{ {
var guildSettings = ServerSettingHandler.GetSettings(guildChannel.GuildId); var guildSettings = ServerSettingHandler.GetSettings(guildChannel.GuildId);
var jokes = guildSettings.EnabledJokes; var jokes = guildSettings.EnabledJokes;
foreach (var enabledJoke in jokes) foreach (var enabledJoke in jokes)
{ {
if (message.IsHandled)
return;
var jokeController = Jokes[enabledJoke]; var jokeController = Jokes[enabledJoke];
jokeController.Run(message); jokeController.Run(message);
} }

View File

@ -3,7 +3,6 @@ using System.Threading.Tasks;
using DeukBot4.Database; using DeukBot4.Database;
using DeukBot4.MessageHandlers.CommandHandler; using DeukBot4.MessageHandlers.CommandHandler;
using Discord; using Discord;
using Discord.WebSocket;
namespace DeukBot4.MessageHandlers.JokeHandling namespace DeukBot4.MessageHandlers.JokeHandling
{ {
@ -12,15 +11,19 @@ namespace DeukBot4.MessageHandlers.JokeHandling
public string Id => "owo-warn"; public string Id => "owo-warn";
public string Name => "Owo Warnings"; public string Name => "Owo Warnings";
private static readonly Regex Matcher = new Regex(@"(?:^|\s).w.(?:\s|$)", RegexOptions.IgnoreCase); private static readonly Regex Matcher = new Regex(@"(?:^|\s)(.)[wv]\1(?:\s|$)", RegexOptions.IgnoreCase);
public async Task Run(SocketMessage message) public async Task Run(ReceivedMessage receivedMessage)
{ {
if (!(message.Channel is IGuildChannel serverChannel)) if (!(receivedMessage.Message.Channel is IGuildChannel serverChannel))
return; return;
if (!Matcher.Match(message.Content).Success) if (!Matcher.Match(receivedMessage.Message.Content).Success)
return; return;
if (receivedMessage.IsHandled)
return;
receivedMessage.IsHandled = true;
var message = receivedMessage.Message;
var changePoints = await PointHandler.ChangePoints(serverChannel.GuildId, message.Author.Id, -100); var changePoints = await PointHandler.ChangePoints(serverChannel.GuildId, message.Author.Id, -100);
var embed = EmbedFactory.GetStandardEmbedBuilder(); var embed = EmbedFactory.GetStandardEmbedBuilder();

View File

@ -19,14 +19,17 @@ namespace DeukBot4.MessageHandlers.JokeHandling
}; };
private static int _pos = 0; private static int _pos = 0;
public async Task Run(SocketMessage message) public async Task Run(ReceivedMessage message)
{ {
var content = message.Content; var content = message.Message.Content;
if (_thanksMatcher.IsMatch(content)) if (_thanksMatcher.IsMatch(content))
{ {
if (message.IsHandled)
return;
message.IsHandled = true;
var response = _thanksResponses[_pos]; var response = _thanksResponses[_pos];
_pos = (_pos + 1) % _thanksResponses.Length; _pos = (_pos + 1) % _thanksResponses.Length;
message.Channel.SendMessageAsync(response); message.Message.Channel.SendMessageAsync(response);
} }
} }
} }

View File

@ -18,13 +18,14 @@ namespace DeukBot4.MessageHandlers
} }
try try
{ {
var receivedMessage = new ReceivedMessage(message);
#pragma warning disable 4014 #pragma warning disable 4014
CommandHandler.CommandHandler.HandleMessage(message); CommandHandler.CommandHandler.HandleMessage(receivedMessage);
HandlePrivateMessage(message); HandlePrivateMessage(receivedMessage);
ReminderHandler.HandleReminder(message); ReminderHandler.HandleReminder(receivedMessage);
ImageBackupHandler.Backup(message); ImageBackupHandler.Backup(receivedMessage);
JokeHandler.RunJokes(message); JokeHandler.RunJokes(receivedMessage);
PointsMessageHandler.HandleMessage(message); PointsMessageHandler.HandleMessage(receivedMessage);
#pragma warning restore 4014 #pragma warning restore 4014
} }
catch (Exception e) catch (Exception e)
@ -34,8 +35,9 @@ namespace DeukBot4.MessageHandlers
} }
private static ITextChannel _dmChannel; private static ITextChannel _dmChannel;
private static async Task HandlePrivateMessage(SocketMessage message) private static async Task HandlePrivateMessage(ReceivedMessage receivedMessage)
{ {
var message = receivedMessage.Message;
if (message.Channel is IPrivateChannel) if (message.Channel is IPrivateChannel)
{ {
await Logger.Main.Log(($"Private Message: {message.Author.Username}- {message.Content}")); await Logger.Main.Log(($"Private Message: {message.Author.Username}- {message.Content}"));

View File

@ -38,18 +38,23 @@ namespace DeukBot4.MessageHandlers
} }
} }
public static async Task HandleMessage(SocketMessage message) public static async Task HandleMessage(ReceivedMessage receivedMessage)
{ {
if (!(message.Channel is IGuildChannel serverChannel)) if (!(receivedMessage.Message.Channel is IGuildChannel serverChannel))
return; return;
var permission = await PermissionValidator.GetUserPermissionLevel(message); var permission = await PermissionValidator.GetUserPermissionLevel(receivedMessage.Message);
if (permission < PermissionLevel.Helper) if (permission < PermissionLevel.Helper)
return; return;
var match = Matcher.Match(message.Content); var match = Matcher.Match(receivedMessage.Message.Content);
if (!match.Success) if (!match.Success)
return; return;
if (receivedMessage.IsHandled)
return;
receivedMessage.IsHandled = true;
var message = receivedMessage.Message;
var points = long.Parse(match.Groups["amount"].Value); var points = long.Parse(match.Groups["amount"].Value);
var embed = EmbedFactory.GetStandardEmbedBuilder(); var embed = EmbedFactory.GetStandardEmbedBuilder();

View File

@ -0,0 +1,16 @@
using Discord.WebSocket;
namespace DeukBot4.MessageHandlers
{
public class ReceivedMessage
{
public readonly SocketMessage Message;
public bool IsHandled;
public ReceivedMessage(SocketMessage message)
{
Message = message;
IsHandled = false;
}
}
}

View File

@ -14,13 +14,17 @@ namespace DeukBot4.MessageHandlers
@".*(remind\s*((?<recipient>me)|<@!*(?<recipient>\d*)>)\s*)(\s+to(?<action>.+)|\s+in\s+(?<time>.+)){2}", @".*(remind\s*((?<recipient>me)|<@!*(?<recipient>\d*)>)\s*)(\s+to(?<action>.+)|\s+in\s+(?<time>.+)){2}",
RegexOptions.IgnoreCase); RegexOptions.IgnoreCase);
public static async Task HandleReminder(SocketMessage message) public static async Task HandleReminder(ReceivedMessage receivedMessage)
{ {
var match = ReminderMatcher.Match(message.Content); var match = ReminderMatcher.Match(receivedMessage.Message.Content);
if (!match.Success) if (!match.Success)
{ {
return; return;
} }
if (receivedMessage.IsHandled)
return;
receivedMessage.IsHandled = true;
var message = receivedMessage.Message;
var recipient = match.Groups["recipient"].Captures[0].Value; var recipient = match.Groups["recipient"].Captures[0].Value;
var action = match.Groups["action"]?.Value.Trim(); var action = match.Groups["action"]?.Value.Trim();