From 0120fa24efc0f934816ff3de471659094b8d5a5d Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 4 Jul 2024 06:24:51 +0100 Subject: [PATCH] find users in a voice channel when starting to make logs more accurate --- .../logging/listeners/ChannelListener.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java b/src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java index fdca649..479f84a 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java +++ b/src/main/java/cc/fascinated/bat/features/logging/listeners/ChannelListener.java @@ -8,8 +8,12 @@ import cc.fascinated.bat.features.logging.LogFeature; import cc.fascinated.bat.features.logging.LogType; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; +import cc.fascinated.bat.service.DiscordService; +import cc.fascinated.bat.service.UserService; +import jakarta.annotation.PostConstruct; import lombok.NonNull; import lombok.extern.log4j.Log4j2; +import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; @@ -36,13 +40,30 @@ public class ChannelListener implements EventListener { * A map of users and the last voice channel they were in */ private final Map lastVoiceChannel = new HashMap<>(); + private final UserService userService; private final LogFeature logFeature; @Autowired - public ChannelListener(@NonNull ApplicationContext context) { + public ChannelListener(@NonNull ApplicationContext context, @NonNull UserService userService) { + this.userService = userService; this.logFeature = context.getBean(LogFeature.class); } + @PostConstruct + public void init() { + log.info("Getting last voice channel for all users"); + for (VoiceChannel voiceChannel : DiscordService.JDA.getVoiceChannels()) { + for (Member member : voiceChannel.getMembers()) { + BatUser user = userService.getUser(member.getId()); + if (user == null) { + continue; + } + lastVoiceChannel.put(user, voiceChannel); + } + } + log.info("Found {} users in a voice channel", lastVoiceChannel.size()); + } + @Override public void onChannelCreate(@NonNull BatGuild guild, @NonNull ChannelCreateEvent event) { ChannelUnion channel = event.getChannel();