mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-04 04:56:49 +00:00
feat: decrement message counts when messages deleted
This commit is contained in:
parent
5d86814415
commit
71dd59b57d
3 changed files with 33 additions and 2 deletions
|
|
@ -32,6 +32,16 @@ public class MessageDeleted: IEventHandler<MessageDeleteEvent>, IEventHandler<Me
|
|||
async Task Inner()
|
||||
{
|
||||
await Task.Delay(MessageDeleteDelay);
|
||||
|
||||
var message = await _repo.GetMessage(evt.Id);
|
||||
if (message == null) return;
|
||||
|
||||
if (message.Member.HasValue)
|
||||
{
|
||||
var memberFull = await _repo.GetMember(message.Member.Value);
|
||||
await _repo.UpdateMemberForDeletedMessage(message.Member.Value, memberFull.MessageCount);
|
||||
}
|
||||
|
||||
await _repo.DeleteMessage(evt.Id);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -141,8 +141,6 @@ public class ReactionAdded: IEventHandler<MessageReactionAddEvent>
|
|||
{
|
||||
// Message was deleted by something/someone else before we got to it
|
||||
}
|
||||
|
||||
await _repo.DeleteMessage(evt.MessageId);
|
||||
}
|
||||
|
||||
private async ValueTask HandleCommandDeleteReaction(MessageReactionAddEvent evt, ulong? authorId, bool isDM)
|
||||
|
|
|
|||
|
|
@ -110,4 +110,27 @@ public partial class ModelRepository
|
|||
|
||||
await _db.ExecuteQuery(query);
|
||||
}
|
||||
|
||||
public async Task UpdateMemberForDeletedMessage(MemberId id, int msgCount)
|
||||
{
|
||||
if (msgCount <= 0) return;
|
||||
|
||||
var query = new Query("members")
|
||||
.When(msgCount > 1,
|
||||
// when there are plenty of messages
|
||||
q => q.AsUpdate(new
|
||||
{
|
||||
message_count = new UnsafeLiteral("message_count - 1")
|
||||
}),
|
||||
// when this is the only message (if the db for some reason thinks there are no messages we already returned)
|
||||
q => q.AsUpdate(new
|
||||
{
|
||||
message_count = new UnsafeLiteral("message_count - 1"),
|
||||
last_message_timestamp = new UnsafeLiteral("null")
|
||||
})
|
||||
)
|
||||
.Where("id", id);
|
||||
|
||||
await _db.ExecuteQuery(query);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue