package cc.fascinated.bat.service; import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.model.DiscordMessage; import lombok.NonNull; import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.events.channel.ChannelCreateEvent; import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent; import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent; import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent; import net.dv8tion.jda.api.events.message.MessageDeleteEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateGlobalNameEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Service; import java.util.HashSet; import java.util.Set; /** * @author Fascinated (fascinated7) */ @Service @Log4j2(topic = "Event Service") @DependsOn("discordService") public class EventService extends ListenerAdapter { /** * The list of listeners registered */ public static final Set LISTENERS = new HashSet<>(); private final GuildService guildService; private final UserService userService; private final DiscordMessageService discordMessageService; @Autowired public EventService(@NonNull ApplicationContext context, @NonNull GuildService guildService, @NonNull UserService userService, @NonNull DiscordMessageService discordMessageService) { this.guildService = guildService; this.userService = userService; this.discordMessageService = discordMessageService; DiscordService.JDA.addEventListener(this); context.getBeansOfType(EventListener.class).values().forEach(this::registerListeners); log.info("Registered {} event listeners.", LISTENERS.size()); } /** * Registers an event listener * * @param listener the listener to register */ public void registerListeners(EventListener... listener) { LISTENERS.addAll(Set.of(listener)); } @Override public void onGuildMemberJoin(@NotNull GuildMemberJoinEvent event) { if (event.getUser().isBot()) { return; } BatGuild guild = guildService.getGuild(event.getGuild().getId()); BatUser user = userService.getUser(event.getUser().getId()); for (EventListener listener : LISTENERS) { listener.onGuildMemberJoin(guild, user, event); } } @Override public void onGuildMemberRemove(@NonNull GuildMemberRemoveEvent event) { if (event.getUser().isBot()) { return; } BatGuild guild = guildService.getGuild(event.getGuild().getId()); BatUser user = userService.getUser(event.getUser().getId()); for (EventListener listener : LISTENERS) { listener.onGuildMemberLeave(guild, user, event); } } @Override public void onMessageReceived(MessageReceivedEvent event) { if (event.getAuthor().isBot()) { return; } BatGuild guild = guildService.getGuild(event.getGuild().getId()); BatUser user = userService.getUser(event.getAuthor().getId()); for (EventListener listener : LISTENERS) { listener.onGuildMessageReceive(guild, user, event); } } @Override public void onMessageDelete(@NotNull MessageDeleteEvent event) { BatGuild guild = guildService.getGuild(event.getGuild().getId()); DiscordMessage message = discordMessageService.getMessage(event.getMessageId()); BatUser user = message == null ? null : userService.getUser(message.getAuthorId()); for (EventListener listener : LISTENERS) { listener.onGuildMessageDelete(guild, user, message, event); } } @Override public void onStringSelectInteraction(StringSelectInteractionEvent event) { if (event.getUser().isBot()) { return; } BatGuild guild = event.getGuild() != null ? guildService.getGuild(event.getGuild().getId()) : null; BatUser user = userService.getUser(event.getUser().getId()); for (EventListener listener : LISTENERS) { listener.onStringSelectInteraction(guild, user, event); } } @Override public void onButtonInteraction(ButtonInteractionEvent event) { if (event.getUser().isBot()) { return; } BatGuild guild = event.getGuild() != null ? guildService.getGuild(event.getGuild().getId()) : null; BatUser user = userService.getUser(event.getUser().getId()); for (EventListener listener : LISTENERS) { listener.onButtonInteraction(guild, user, event); } } @Override public void onModalInteraction(ModalInteractionEvent event) { if (event.getUser().isBot()) { return; } BatGuild guild = event.getGuild() != null ? guildService.getGuild(event.getGuild().getId()) : null; BatUser user = userService.getUser(event.getUser().getId()); for (EventListener listener : LISTENERS) { listener.onModalInteraction(guild, user, event); } } @Override public void onUserUpdateGlobalName(@NotNull UserUpdateGlobalNameEvent event) { if (event.getUser().isBot()) { return; } BatUser user = userService.getUser(event.getUser().getId()); for (EventListener listener : LISTENERS) { listener.onUserUpdateGlobalName(user, event.getOldGlobalName(), event.getNewGlobalName(), event); } } @Override public void onGuildMemberUpdateNickname(@NotNull GuildMemberUpdateNicknameEvent event) { if (event.getUser().isBot()) { return; } BatGuild guild = guildService.getGuild(event.getGuild().getId()); BatUser user = userService.getUser(event.getUser().getId()); for (EventListener listener : LISTENERS) { listener.onGuildMemberUpdateNickname(guild, user, event.getOldNickname(), event.getNewNickname(), event); } } @Override public void onGuildMemberRoleAdd(@NotNull GuildMemberRoleAddEvent event) { if (event.getUser().isBot()) { return; } BatGuild guild = guildService.getGuild(event.getGuild().getId()); BatUser user = userService.getUser(event.getUser().getId()); for (EventListener listener : LISTENERS) { listener.onGuildMemberRoleAdd(guild, user, event.getRoles(), event); } } @Override public void onGuildMemberRoleRemove(@NotNull GuildMemberRoleRemoveEvent event) { if (event.getUser().isBot()) { return; } BatGuild guild = guildService.getGuild(event.getGuild().getId()); BatUser user = userService.getUser(event.getUser().getId()); for (EventListener listener : LISTENERS) { listener.onGuildMemberRoleRemove(guild, user, event.getRoles(), event); } } @Override public void onChannelCreate(@NotNull ChannelCreateEvent event) { BatGuild guild = guildService.getGuild(event.getGuild().getId()); for (EventListener listener : LISTENERS) { listener.onChannelCreate(guild, event); } } @Override public void onChannelDelete(@NotNull ChannelDeleteEvent event) { BatGuild guild = guildService.getGuild(event.getGuild().getId()); for (EventListener listener : LISTENERS) { listener.onChannelDelete(guild, event); } } }