find users in a voice channel when starting to make logs more accurate

This commit is contained in:
Lee 2024-07-04 06:24:51 +01:00
parent 4a522c084d
commit 0120fa24ef

@ -8,8 +8,12 @@ import cc.fascinated.bat.features.logging.LogFeature;
import cc.fascinated.bat.features.logging.LogType; import cc.fascinated.bat.features.logging.LogType;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser; 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.NonNull;
import lombok.extern.log4j.Log4j2; 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.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; 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 * A map of users and the last voice channel they were in
*/ */
private final Map<BatUser, VoiceChannel> lastVoiceChannel = new HashMap<>(); private final Map<BatUser, VoiceChannel> lastVoiceChannel = new HashMap<>();
private final UserService userService;
private final LogFeature logFeature; private final LogFeature logFeature;
@Autowired @Autowired
public ChannelListener(@NonNull ApplicationContext context) { public ChannelListener(@NonNull ApplicationContext context, @NonNull UserService userService) {
this.userService = userService;
this.logFeature = context.getBean(LogFeature.class); 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 @Override
public void onChannelCreate(@NonNull BatGuild guild, @NonNull ChannelCreateEvent event) { public void onChannelCreate(@NonNull BatGuild guild, @NonNull ChannelCreateEvent event) {
ChannelUnion channel = event.getChannel(); ChannelUnion channel = event.getChannel();