From 5d2d4cbf0f68308a5eb82faa38cfa37b0dc98c3d Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 26 Jun 2024 12:48:46 +0100 Subject: [PATCH] auto register event listeners --- .../fascinated/bat/event/EventListener.java | 9 +++++++++ .../fascinated/bat/service/EventService.java | 20 +++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/event/EventListener.java b/src/main/java/cc/fascinated/bat/event/EventListener.java index d00773c..ee0a2a6 100644 --- a/src/main/java/cc/fascinated/bat/event/EventListener.java +++ b/src/main/java/cc/fascinated/bat/event/EventListener.java @@ -7,6 +7,7 @@ import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberPlayerScoreT import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken; import lombok.NonNull; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; +import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; /** * @author Fascinated (fascinated7) @@ -29,4 +30,12 @@ public interface EventListener { * @param user the user that joined the guild */ default void onGuildMemberJoin(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberJoinEvent event) {} + + /** + * Called when a user leaves a guild + * + * @param guild the guild the user left + * @param user the user that left the guild + */ + default void onGuildMemberLeave(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberRemoveEvent event) {} } diff --git a/src/main/java/cc/fascinated/bat/service/EventService.java b/src/main/java/cc/fascinated/bat/service/EventService.java index 5ef629b..c6b9fcd 100644 --- a/src/main/java/cc/fascinated/bat/service/EventService.java +++ b/src/main/java/cc/fascinated/bat/service/EventService.java @@ -1,14 +1,12 @@ package cc.fascinated.bat.service; import cc.fascinated.bat.event.EventListener; -import cc.fascinated.bat.features.autorole.AutoRoleListener; -import cc.fascinated.bat.features.scoresaber.NumberOneScoreFeedListener; -import cc.fascinated.bat.features.scoresaber.UserScoreFeedListener; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; import lombok.NonNull; import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; +import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; @@ -39,11 +37,7 @@ public class EventService extends ListenerAdapter { this.userService = userService; DiscordService.JDA.addEventListener(this); - registerListeners( - context.getBean(UserScoreFeedListener.class), - context.getBean(NumberOneScoreFeedListener.class), - context.getBean(AutoRoleListener.class) - ); + context.getBeansOfType(EventListener.class).values().forEach(this::registerListeners); log.info("Registered {} listeners.", LISTENERS.size()); } @@ -65,4 +59,14 @@ public class EventService extends ListenerAdapter { listener.onGuildMemberJoin(guild, user, event); } } + + @Override + public void onGuildMemberRemove(@NonNull GuildMemberRemoveEvent event) { + BatGuild guild = guildService.getGuild(event.getGuild().getId()); + BatUser user = userService.getUser(event.getUser().getId()); + + for (EventListener listener : LISTENERS) { + listener.onGuildMemberLeave(guild, user, event); + } + } }