2022-05-12 03:26:28 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using RegexBot.Data;
|
|
|
|
|
using static RegexBot.RegexbotClient;
|
|
|
|
|
|
|
|
|
|
namespace RegexBot.Services.EntityCache;
|
|
|
|
|
class MessageCachingSubservice {
|
|
|
|
|
// Hooked
|
2022-07-08 19:03:15 +00:00
|
|
|
|
public event EcMessageUpdateHandler? OnCachePreUpdate;
|
2022-05-12 03:26:28 +00:00
|
|
|
|
|
|
|
|
|
private readonly Action<string, bool> _log;
|
|
|
|
|
|
|
|
|
|
internal MessageCachingSubservice(RegexbotClient bot, Action<string, bool> logMethod) {
|
|
|
|
|
_log = logMethod;
|
|
|
|
|
bot.DiscordClient.MessageReceived += DiscordClient_MessageReceived;
|
|
|
|
|
bot.DiscordClient.MessageUpdated += DiscordClient_MessageUpdated;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-03 03:43:34 +00:00
|
|
|
|
private Task DiscordClient_MessageReceived(SocketMessage arg)
|
2022-07-08 19:03:15 +00:00
|
|
|
|
=> AddOrUpdateCacheItemAsync(arg, false);
|
2022-06-03 03:43:34 +00:00
|
|
|
|
private Task DiscordClient_MessageUpdated(Cacheable<IMessage, ulong> arg1, SocketMessage arg2, ISocketMessageChannel arg3)
|
2022-07-08 19:03:15 +00:00
|
|
|
|
=> AddOrUpdateCacheItemAsync(arg2, true);
|
2022-05-12 03:26:28 +00:00
|
|
|
|
|
2022-07-08 19:03:15 +00:00
|
|
|
|
private async Task AddOrUpdateCacheItemAsync(SocketMessage arg, bool isUpdate) {
|
2022-07-09 20:22:39 +00:00
|
|
|
|
if (!Common.Utilities.IsValidUserMessage(arg, out _)) return;
|
2022-07-08 19:03:15 +00:00
|
|
|
|
|
2022-05-12 03:26:28 +00:00
|
|
|
|
using var db = new BotDatabaseContext();
|
2022-07-08 19:03:15 +00:00
|
|
|
|
CachedGuildMessage? cachedMsg = db.GuildMessageCache.Where(m => m.MessageId == (long)arg.Id).SingleOrDefault();
|
|
|
|
|
|
|
|
|
|
if (isUpdate) {
|
|
|
|
|
// Alternative for Discord.Net's MessageUpdated handler:
|
|
|
|
|
// Notify subscribers of message update using EC entry for the previous message state
|
2022-07-21 03:37:31 +00:00
|
|
|
|
var oldMsg = CachedGuildMessage.Clone(cachedMsg);
|
2022-07-08 19:03:15 +00:00
|
|
|
|
await Task.Factory.StartNew(async () => await RunPreUpdateHandlersAsync(oldMsg, arg));
|
|
|
|
|
}
|
2022-05-12 03:26:28 +00:00
|
|
|
|
|
2022-07-08 19:03:15 +00:00
|
|
|
|
if (cachedMsg == null) {
|
|
|
|
|
cachedMsg = new() {
|
2022-05-12 03:26:28 +00:00
|
|
|
|
MessageId = (long)arg.Id,
|
|
|
|
|
AuthorId = (long)arg.Author.Id,
|
|
|
|
|
GuildId = (long)((SocketGuildUser)arg.Author).Guild.Id,
|
|
|
|
|
ChannelId = (long)arg.Channel.Id,
|
|
|
|
|
AttachmentNames = arg.Attachments.Select(a => a.Filename).ToList(),
|
|
|
|
|
Content = arg.Content
|
|
|
|
|
};
|
2022-07-08 19:03:15 +00:00
|
|
|
|
db.GuildMessageCache.Add(cachedMsg);
|
2022-05-12 03:26:28 +00:00
|
|
|
|
} else {
|
2022-07-08 19:03:15 +00:00
|
|
|
|
cachedMsg.EditedAt = DateTimeOffset.UtcNow;
|
|
|
|
|
cachedMsg.Content = arg.Content;
|
|
|
|
|
cachedMsg.AttachmentNames = arg.Attachments.Select(a => a.Filename).ToList();
|
|
|
|
|
db.GuildMessageCache.Update(cachedMsg);
|
2022-05-12 03:26:28 +00:00
|
|
|
|
}
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 19:03:15 +00:00
|
|
|
|
private async Task RunPreUpdateHandlersAsync(CachedGuildMessage? oldMsg, SocketMessage newMsg) {
|
|
|
|
|
Delegate[]? subscribers;
|
|
|
|
|
lock (this) {
|
|
|
|
|
subscribers = OnCachePreUpdate?.GetInvocationList();
|
|
|
|
|
if (subscribers == null || subscribers.Length == 0) return;
|
|
|
|
|
}
|
2022-05-12 03:26:28 +00:00
|
|
|
|
|
2022-07-08 19:03:15 +00:00
|
|
|
|
foreach (var handler in subscribers) {
|
2022-05-12 03:26:28 +00:00
|
|
|
|
try {
|
2022-07-08 19:03:15 +00:00
|
|
|
|
await (Task)handler.DynamicInvoke(oldMsg, newMsg)!;
|
2022-05-12 03:26:28 +00:00
|
|
|
|
} catch (Exception ex) {
|
2022-07-08 19:03:15 +00:00
|
|
|
|
_log($"Unhandled exception in {nameof(RegexbotClient.EcOnMessageUpdate)} handler '{handler.Method.Name}':", false);
|
2022-05-12 03:26:28 +00:00
|
|
|
|
_log(ex.ToString(), false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|