Skip to content

Commit

Permalink
Fully fix starboard.
Browse files Browse the repository at this point in the history
  • Loading branch information
Naamloos committed Jul 29, 2023
1 parent 2abb27d commit 36b7440
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions ModCore/Listeners/StarboardListeners.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public static async Task ReactionAddedAsync(MessageReactionAddEventArgs eventarg
}
}

[AsyncListener(EventType.MessageReactionRemoved)]
public static async Task ReactionRemovedAsync(MessageReactionRemoveEventArgs eventargs, DatabaseContextBuilder database,
DiscordClient client, IMemoryCache cache)
{
Expand Down Expand Up @@ -285,7 +286,10 @@ public static async Task ReactionsClearedAsync(MessageReactionsClearEventArgs ev
await semaphore.WaitAsync();
try
{
var starDataWithMessage = ctx.StarDatas.FirstOrDefault(x => x.StarboardMessageId != 0);
var starDataWithMessage = ctx.StarDatas.FirstOrDefault(x => x.MessageId == (long)eventargs.Message.Id &&
x.ChannelId == (long)eventargs.Channel.Id &&
x.StarboardMessageId != 0);

if (starDataWithMessage != default(DatabaseStarData))
{
try
Expand All @@ -300,8 +304,7 @@ public static async Task ReactionsClearedAsync(MessageReactionsClearEventArgs ev
// delete all star datas for this message.
ctx.StarDatas.RemoveRange(ctx.StarDatas.Where(x =>
x.MessageId == (long)eventargs.Message.Id &&
x.ChannelId == (long)eventargs.Channel.Id &&
x.GuildId == (long)eventargs.Guild.Id));
x.ChannelId == (long)eventargs.Channel.Id));
}
finally
{
Expand All @@ -312,6 +315,7 @@ public static async Task ReactionsClearedAsync(MessageReactionsClearEventArgs ev
}
}

[AsyncListener(EventType.MessageReactionEmojiRemoved)]
public static async Task ReactionsEmojiRemoveAsync(MessageReactionRemoveEmojiEventArgs eventargs, DatabaseContextBuilder database,
DiscordClient client, IMemoryCache cache)
{
Expand Down Expand Up @@ -379,7 +383,10 @@ public static async Task ReactionsEmojiRemoveAsync(MessageReactionRemoveEmojiEve
await semaphore.WaitAsync();
try
{
var starDataWithMessage = ctx.StarDatas.FirstOrDefault(x => x.StarboardMessageId != 0);
var starDataWithMessage = ctx.StarDatas.FirstOrDefault(x => x.MessageId == (long)eventargs.Message.Id &&
x.ChannelId == (long)eventargs.Channel.Id &&
x.StarboardMessageId != 0);

if (starDataWithMessage != default(DatabaseStarData))
{
try
Expand All @@ -394,8 +401,7 @@ public static async Task ReactionsEmojiRemoveAsync(MessageReactionRemoveEmojiEve
// delete all star datas for this message.
ctx.StarDatas.RemoveRange(ctx.StarDatas.Where(x =>
x.MessageId == (long)eventargs.Message.Id &&
x.ChannelId == (long)eventargs.Channel.Id &&
x.GuildId == (long)eventargs.Guild.Id));
x.ChannelId == (long)eventargs.Channel.Id));
}
finally
{
Expand Down Expand Up @@ -481,7 +487,7 @@ private static async Task updateStarboardMessage(DatabaseContext database, Disco

// Update the starboard message ID for all existing stars
starboardMessageId = (long)starboardMessage.Id;
await existingStars.ForEachAsync(x => x.StarboardMessageId = starboardMessageId);
await existingStars.ForEachAsync(x => x.StarboardMessageId = (long)starboardMessage.Id);
database.UpdateRange(existingStars);
await database.SaveChangesAsync();
try
Expand Down

0 comments on commit 36b7440

Please sign in to comment.