Skip to content

Commit

Permalink
Merge branch 'master' into slots-leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
butteredwaffles authored Nov 16, 2023
2 parents defb524 + 163a876 commit ed66557
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ public async Task<QueryResult<List<LeaderboardSlot>>> Handle(Query request, Canc
}
}

var posts = await _context.EmoteBoardPosts
var reactions = await _context.EmoteBoardPosts
.Where(p => board != null ? p.EmoteBoardId == board.Id : p.EmoteBoard.GuildId == request.GuildId)
.Include(p => p.Reactions)
.GroupBy(p => p.UserId)
.Select(group => new LeaderboardSlot
{
UserId = group.Key,
ReactionCount = group.Select(p => p.Reactions).Count()
ReactionCount = group.Select(p => p.Reactions.Count).Sum()
})
.OrderByDescending(slot => slot.ReactionCount)
.Take(request.Limit)
.ToListAsync();

return QueryResult<List<LeaderboardSlot>>.Success(posts);
return QueryResult<List<LeaderboardSlot>>.Success(reactions);
}
}
}
4 changes: 4 additions & 0 deletions ClemBot.Bot/bot/clem_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,14 @@ async def on_raw_message_edit(self, payload: discord.RawMessageUpdateEvent) -> N
await self.publish_with_error(Events.on_raw_message_edit, payload)

async def on_message_delete(self, message: discord.Message) -> None:
if not message.guild:
return
if message.author.id != self.user.id:
await self.publish_with_error(Events.on_message_delete, message)

async def on_raw_message_delete(self, payload: discord.RawMessageDeleteEvent) -> None:
if not payload.guild_id:
return
await self.publish_with_error(Events.on_raw_message_delete, payload)

async def on_after_command_invoke(self, ctx: ext.ClemBotContext["ClemBot"]) -> None:
Expand Down
11 changes: 7 additions & 4 deletions ClemBot.Bot/bot/services/emote_board_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async def on_message_edit(self, event: RawMessageUpdateEvent) -> None:

posts = await self.bot.emote_board_route.get_posts(event.guild_id, event.message_id)

post_boards = {}
post_boards: list[tuple[EmoteBoardPost, EmoteBoard]] = []

for post in posts:
board = await self.bot.emote_board_route.get_emote_board(
Expand All @@ -131,9 +131,9 @@ async def on_message_edit(self, event: RawMessageUpdateEvent) -> None:
board=post.name,
)
continue
post_boards[post] = board
post_boards.append((post, board))

for post, board in post_boards.items():
for post, emote_board in post_boards:
for channel_id, message_id in post.channel_message_ids.items():
try:
if not (channel := guild.get_channel_or_thread(channel_id)):
Expand All @@ -142,7 +142,10 @@ async def on_message_edit(self, event: RawMessageUpdateEvent) -> None:
assert isinstance(channel, discord.abc.Messageable)
embed_msg = await channel.fetch_message(message_id)
embed = await self._as_embed(
message, board.reaction_threshold, len(post.reactions), board.emote
message,
emote_board.reaction_threshold,
len(post.reactions),
emote_board.emote,
)
await embed_msg.edit(embed=embed)
except NotFound: # Skips over the item if fetch_message() raises `NotFound`
Expand Down
4 changes: 4 additions & 0 deletions ClemBot.Bot/bot/services/message_handling_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ async def on_message_edit(self, before: discord.Message, after: discord.Message)
# noinspection PyArgumentList
@BaseService.listener(Events.on_raw_message_edit)
async def on_raw_message_edit(self, payload: discord.RawMessageUpdateEvent) -> None:
# ignore any cached messages - this will be handled by on_message_edit instead
if payload.cached_message:
return

message = await self.bot.message_route.get_message(payload.message_id)
channel = self.bot.get_channel(payload.channel_id)

Expand Down

0 comments on commit ed66557

Please sign in to comment.