Cancel message handlers if another handler has already handled it
This commit is contained in:
parent
a393f2ca1e
commit
9a563ba0b6
|
@ -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;
|
||||
|
||||
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;
|
||||
if (message.Content[1] == '!')
|
||||
if (receivedMessage.Message.Content[1] == '!')
|
||||
return;
|
||||
|
||||
receivedMessage.IsHandled = true;
|
||||
var message = receivedMessage.Message;
|
||||
|
||||
var req = await CommandRequest.Create(message);
|
||||
var resultCode = req.Response;
|
||||
switch (resultCode)
|
||||
|
|
|
@ -28,8 +28,9 @@ namespace DeukBot4.MessageHandlers
|
|||
private static List<ServerChannelIDs> _backupChannels = new List<ServerChannelIDs>();
|
||||
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))
|
||||
return;
|
||||
|
||||
|
|
|
@ -13,16 +13,19 @@ namespace DeukBot4.MessageHandlers.JokeHandling
|
|||
private readonly Regex _regex = new Regex(@"(\s|^)i(`|’|'| a)?m (?<word>.{3,15})$",
|
||||
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;
|
||||
var content = message.Content;
|
||||
var content = message.Message.Content;
|
||||
var match = _regex.Match(content);
|
||||
if (match.Success)
|
||||
{
|
||||
if (message.IsHandled)
|
||||
return;
|
||||
message.IsHandled = true;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,17 +10,22 @@ namespace DeukBot4.MessageHandlers.JokeHandling
|
|||
{
|
||||
public string Id => "delta";
|
||||
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"))
|
||||
return;
|
||||
|
||||
if (message.IsHandled)
|
||||
return;
|
||||
var lowerSplit = lowerCasedContent.Split(' ');
|
||||
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";
|
||||
await message.Channel.SendFileAsync(await CatPicHandler.GetCatPicture(warning), "cat_pic.png");
|
||||
await message.Message.Channel.SendFileAsync(await CatPicHandler.GetCatPicture(warning), "cat_pic.png");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,6 @@ namespace DeukBot4.MessageHandlers.JokeHandling
|
|||
string Id { get; }
|
||||
string Name { get; }
|
||||
|
||||
Task Run(SocketMessage message);
|
||||
Task Run(ReceivedMessage message);
|
||||
}
|
||||
}
|
|
@ -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 jokes = guildSettings.EnabledJokes;
|
||||
foreach (var enabledJoke in jokes)
|
||||
{
|
||||
if (message.IsHandled)
|
||||
return;
|
||||
var jokeController = Jokes[enabledJoke];
|
||||
jokeController.Run(message);
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ using System.Threading.Tasks;
|
|||
using DeukBot4.Database;
|
||||
using DeukBot4.MessageHandlers.CommandHandler;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace DeukBot4.MessageHandlers.JokeHandling
|
||||
{
|
||||
|
@ -12,15 +11,19 @@ namespace DeukBot4.MessageHandlers.JokeHandling
|
|||
public string Id => "owo-warn";
|
||||
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;
|
||||
|
||||
if (!Matcher.Match(message.Content).Success)
|
||||
if (!Matcher.Match(receivedMessage.Message.Content).Success)
|
||||
return;
|
||||
if (receivedMessage.IsHandled)
|
||||
return;
|
||||
receivedMessage.IsHandled = true;
|
||||
var message = receivedMessage.Message;
|
||||
|
||||
var changePoints = await PointHandler.ChangePoints(serverChannel.GuildId, message.Author.Id, -100);
|
||||
var embed = EmbedFactory.GetStandardEmbedBuilder();
|
||||
|
|
|
@ -19,14 +19,17 @@ namespace DeukBot4.MessageHandlers.JokeHandling
|
|||
};
|
||||
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 (message.IsHandled)
|
||||
return;
|
||||
message.IsHandled = true;
|
||||
var response = _thanksResponses[_pos];
|
||||
_pos = (_pos + 1) % _thanksResponses.Length;
|
||||
message.Channel.SendMessageAsync(response);
|
||||
message.Message.Channel.SendMessageAsync(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,13 +18,14 @@ namespace DeukBot4.MessageHandlers
|
|||
}
|
||||
try
|
||||
{
|
||||
var receivedMessage = new ReceivedMessage(message);
|
||||
#pragma warning disable 4014
|
||||
CommandHandler.CommandHandler.HandleMessage(message);
|
||||
HandlePrivateMessage(message);
|
||||
ReminderHandler.HandleReminder(message);
|
||||
ImageBackupHandler.Backup(message);
|
||||
JokeHandler.RunJokes(message);
|
||||
PointsMessageHandler.HandleMessage(message);
|
||||
CommandHandler.CommandHandler.HandleMessage(receivedMessage);
|
||||
HandlePrivateMessage(receivedMessage);
|
||||
ReminderHandler.HandleReminder(receivedMessage);
|
||||
ImageBackupHandler.Backup(receivedMessage);
|
||||
JokeHandler.RunJokes(receivedMessage);
|
||||
PointsMessageHandler.HandleMessage(receivedMessage);
|
||||
#pragma warning restore 4014
|
||||
}
|
||||
catch (Exception e)
|
||||
|
@ -34,8 +35,9 @@ namespace DeukBot4.MessageHandlers
|
|||
}
|
||||
|
||||
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)
|
||||
{
|
||||
await Logger.Main.Log(($"Private Message: {message.Author.Username}- {message.Content}"));
|
||||
|
|
|
@ -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;
|
||||
|
||||
var permission = await PermissionValidator.GetUserPermissionLevel(message);
|
||||
var permission = await PermissionValidator.GetUserPermissionLevel(receivedMessage.Message);
|
||||
if (permission < PermissionLevel.Helper)
|
||||
return;
|
||||
|
||||
var match = Matcher.Match(message.Content);
|
||||
var match = Matcher.Match(receivedMessage.Message.Content);
|
||||
if (!match.Success)
|
||||
return;
|
||||
if (receivedMessage.IsHandled)
|
||||
return;
|
||||
receivedMessage.IsHandled = true;
|
||||
var message = receivedMessage.Message;
|
||||
|
||||
var points = long.Parse(match.Groups["amount"].Value);
|
||||
|
||||
var embed = EmbedFactory.GetStandardEmbedBuilder();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -14,13 +14,17 @@ namespace DeukBot4.MessageHandlers
|
|||
@".*(remind\s*((?<recipient>me)|<@!*(?<recipient>\d*)>)\s*)(\s+to(?<action>.+)|\s+in\s+(?<time>.+)){2}",
|
||||
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)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (receivedMessage.IsHandled)
|
||||
return;
|
||||
receivedMessage.IsHandled = true;
|
||||
var message = receivedMessage.Message;
|
||||
|
||||
var recipient = match.Groups["recipient"].Captures[0].Value;
|
||||
var action = match.Groups["action"]?.Value.Trim();
|
||||
|
|
Loading…
Reference in New Issue