2022-05-12 03:26:28 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using RegexBot.Data;
|
|
|
|
|
|
|
|
|
|
namespace RegexBot.Services.EntityCache;
|
|
|
|
|
class MessageCachingSubservice {
|
2022-08-17 23:59:30 +00:00
|
|
|
|
private readonly RegexbotClient _bot;
|
2022-05-12 03:26:28 +00:00
|
|
|
|
|
2022-08-17 23:59:30 +00:00
|
|
|
|
internal MessageCachingSubservice(RegexbotClient bot) {
|
|
|
|
|
_bot = bot;
|
2022-05-12 03:26:28 +00:00
|
|
|
|
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-07-28 05:02:17 +00:00
|
|
|
|
private Task DiscordClient_MessageUpdated(Cacheable<IMessage, ulong> arg1, SocketMessage arg2, ISocketMessageChannel arg3) {
|
|
|
|
|
// This event is fired also when a link preview embed is added to a message. In those situations, the message's edited timestamp
|
|
|
|
|
// remains null, in addition to having other unusual and unexpected properties. We are not interested in these.
|
|
|
|
|
if (!arg2.EditedTimestamp.HasValue) return Task.CompletedTask;
|
|
|
|
|
return 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-12-04 01:21:42 +00:00
|
|
|
|
//if (!Common.Utilities.IsValidUserMessage(arg, out _)) return;
|
|
|
|
|
if (arg.Channel is not SocketTextChannel) return;
|
|
|
|
|
if (arg.Author.IsWebhook) return; // do get bot messages, don't get webhooks
|
|
|
|
|
if (((IMessage)arg).Type != MessageType.Default) return;
|
|
|
|
|
if (arg is SocketSystemMessage) return;
|
2022-07-08 19:03:15 +00:00
|
|
|
|
|
2022-05-12 03:26:28 +00:00
|
|
|
|
using var db = new BotDatabaseContext();
|
2022-12-04 01:46:26 +00:00
|
|
|
|
CachedGuildMessage? cachedMsg = db.GuildMessageCache.Where(m => m.MessageId == arg.Id).SingleOrDefault();
|
2022-07-08 19:03:15 +00:00
|
|
|
|
|
|
|
|
|
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-08-17 23:59:30 +00:00
|
|
|
|
var updEvent = new MessageCacheUpdateEvent(oldMsg, arg);
|
|
|
|
|
await _bot.PushSharedEventAsync(updEvent);
|
2022-07-08 19:03:15 +00:00
|
|
|
|
}
|
2022-05-12 03:26:28 +00:00
|
|
|
|
|
2022-07-08 19:03:15 +00:00
|
|
|
|
if (cachedMsg == null) {
|
|
|
|
|
cachedMsg = new() {
|
2022-12-04 01:46:26 +00:00
|
|
|
|
MessageId = arg.Id,
|
|
|
|
|
AuthorId = arg.Author.Id,
|
|
|
|
|
GuildId = ((SocketGuildUser)arg.Author).Guild.Id,
|
|
|
|
|
ChannelId = arg.Channel.Id,
|
2022-05-12 03:26:28 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|