auto register event listeners
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 36s

This commit is contained in:
Lee 2024-06-26 12:48:46 +01:00
parent 6ac8012f47
commit 5d2d4cbf0f
2 changed files with 21 additions and 8 deletions

@ -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) {}
}

@ -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);
}
}
}