feat: add last message cache to gateway

This commit is contained in:
alyssa 2025-04-01 10:48:20 +00:00
parent 15c992c572
commit a8664665a6
10 changed files with 172 additions and 18 deletions

View file

@ -55,7 +55,9 @@ public class MessageCreated: IEventHandler<MessageCreateEvent>
public (ulong?, ulong?) ErrorChannelFor(MessageCreateEvent evt, ulong userId) => (evt.GuildId, evt.ChannelId);
private bool IsDuplicateMessage(Message msg) =>
// We consider a message duplicate if it has the same ID as the previous message that hit the gateway
_lastMessageCache.GetLastMessage(msg.ChannelId)?.Current.Id == msg.Id;
// use only the local cache here
// http gateway sets last message before forwarding the message here, so this will always return true
_lastMessageCache._GetLastMessage(msg.ChannelId)?.Current.Id == msg.Id;
public async Task Handle(int shardId, MessageCreateEvent evt)
{

View file

@ -65,7 +65,7 @@ public class MessageEdited: IEventHandler<MessageUpdateEvent>
var guild = await _cache.TryGetGuild(channel.GuildId!.Value);
if (guild == null)
throw new Exception("could not find self guild in MessageEdited event");
var lastMessage = _lastMessageCache.GetLastMessage(evt.ChannelId)?.Current;
var lastMessage = (await _lastMessageCache.GetLastMessage(evt.GuildId.HasValue ? evt.GuildId.Value ?? 0 : 0, evt.ChannelId))?.Current;
// Only react to the last message in the channel
if (lastMessage?.Id != evt.Id)