2024-06-25 10:40:17 +01:00
|
|
|
package cc.fascinated.bat.service;
|
|
|
|
|
|
|
|
import cc.fascinated.bat.event.EventListener;
|
2024-06-25 13:55:54 +01:00
|
|
|
import cc.fascinated.bat.model.BatGuild;
|
|
|
|
import cc.fascinated.bat.model.BatUser;
|
2024-07-01 21:20:39 +01:00
|
|
|
import cc.fascinated.bat.model.DiscordMessage;
|
2024-06-25 10:40:17 +01:00
|
|
|
import lombok.NonNull;
|
|
|
|
import lombok.extern.log4j.Log4j2;
|
2024-07-02 19:24:48 +01:00
|
|
|
import net.dv8tion.jda.api.events.channel.ChannelCreateEvent;
|
|
|
|
import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent;
|
2024-07-02 19:47:51 +01:00
|
|
|
import net.dv8tion.jda.api.events.guild.GuildBanEvent;
|
|
|
|
import net.dv8tion.jda.api.events.guild.GuildUnbanEvent;
|
2024-06-25 13:55:54 +01:00
|
|
|
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
|
2024-06-26 12:48:46 +01:00
|
|
|
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent;
|
2024-07-02 19:05:21 +01:00
|
|
|
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent;
|
|
|
|
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent;
|
2024-06-30 03:36:00 +01:00
|
|
|
import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent;
|
2024-07-02 19:55:50 +01:00
|
|
|
import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateTimeOutEvent;
|
2024-06-28 03:01:21 +01:00
|
|
|
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
|
|
|
|
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
|
2024-06-27 16:01:27 +01:00
|
|
|
import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent;
|
2024-07-01 21:20:39 +01:00
|
|
|
import net.dv8tion.jda.api.events.message.MessageDeleteEvent;
|
2024-06-27 14:29:48 +01:00
|
|
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
2024-06-30 03:36:00 +01:00
|
|
|
import net.dv8tion.jda.api.events.user.update.UserUpdateGlobalNameEvent;
|
2024-07-02 21:03:40 +01:00
|
|
|
import net.dv8tion.jda.api.events.user.update.UserUpdateNameEvent;
|
2024-06-25 13:55:54 +01:00
|
|
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
2024-06-25 10:40:17 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.context.ApplicationContext;
|
2024-06-25 13:55:54 +01:00
|
|
|
import org.springframework.context.annotation.DependsOn;
|
2024-06-25 10:40:17 +01:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fascinated (fascinated7)
|
|
|
|
*/
|
2024-06-28 03:01:21 +01:00
|
|
|
@Service
|
2024-07-01 01:12:32 +01:00
|
|
|
@Log4j2(topic = "Event Service")
|
2024-06-25 13:55:54 +01:00
|
|
|
@DependsOn("discordService")
|
|
|
|
public class EventService extends ListenerAdapter {
|
2024-06-25 10:40:17 +01:00
|
|
|
/**
|
|
|
|
* The list of listeners registered
|
|
|
|
*/
|
|
|
|
public static final Set<EventListener> LISTENERS = new HashSet<>();
|
2024-06-28 03:01:21 +01:00
|
|
|
private final GuildService guildService;
|
|
|
|
private final UserService userService;
|
2024-07-01 21:20:39 +01:00
|
|
|
private final DiscordMessageService discordMessageService;
|
2024-06-25 10:40:17 +01:00
|
|
|
|
|
|
|
@Autowired
|
2024-07-01 21:20:39 +01:00
|
|
|
public EventService(@NonNull ApplicationContext context, @NonNull GuildService guildService, @NonNull UserService userService,
|
|
|
|
@NonNull DiscordMessageService discordMessageService) {
|
2024-06-25 13:55:54 +01:00
|
|
|
this.guildService = guildService;
|
|
|
|
this.userService = userService;
|
2024-07-01 21:20:39 +01:00
|
|
|
this.discordMessageService = discordMessageService;
|
2024-06-25 13:55:54 +01:00
|
|
|
DiscordService.JDA.addEventListener(this);
|
|
|
|
|
2024-06-26 12:48:46 +01:00
|
|
|
context.getBeansOfType(EventListener.class).values().forEach(this::registerListeners);
|
2024-07-02 00:54:55 +01:00
|
|
|
log.info("Registered {} event listeners.", LISTENERS.size());
|
2024-06-25 10:40:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers an event listener
|
|
|
|
*
|
|
|
|
* @param listener the listener to register
|
|
|
|
*/
|
|
|
|
public void registerListeners(EventListener... listener) {
|
|
|
|
LISTENERS.addAll(Set.of(listener));
|
|
|
|
}
|
2024-06-25 13:55:54 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onGuildMemberJoin(@NotNull GuildMemberJoinEvent event) {
|
2024-06-27 14:29:48 +01:00
|
|
|
if (event.getUser().isBot()) {
|
|
|
|
return;
|
|
|
|
}
|
2024-06-25 13:55:54 +01:00
|
|
|
BatGuild guild = guildService.getGuild(event.getGuild().getId());
|
|
|
|
BatUser user = userService.getUser(event.getUser().getId());
|
|
|
|
|
|
|
|
for (EventListener listener : LISTENERS) {
|
|
|
|
listener.onGuildMemberJoin(guild, user, event);
|
|
|
|
}
|
|
|
|
}
|
2024-06-26 12:48:46 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onGuildMemberRemove(@NonNull GuildMemberRemoveEvent event) {
|
2024-06-27 14:29:48 +01:00
|
|
|
if (event.getUser().isBot()) {
|
|
|
|
return;
|
|
|
|
}
|
2024-06-26 12:48:46 +01:00
|
|
|
BatGuild guild = guildService.getGuild(event.getGuild().getId());
|
|
|
|
BatUser user = userService.getUser(event.getUser().getId());
|
|
|
|
|
|
|
|
for (EventListener listener : LISTENERS) {
|
|
|
|
listener.onGuildMemberLeave(guild, user, event);
|
|
|
|
}
|
|
|
|
}
|
2024-06-27 14:29:48 +01:00
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
2024-06-27 16:01:27 +01:00
|
|
|
|
2024-07-01 21:20:39 +01:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-27 16:01:27 +01:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
2024-06-28 03:01:21 +01:00
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
2024-06-30 03:36:00 +01:00
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
2024-07-02 19:05:21 +01:00
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-02 19:24:48 +01:00
|
|
|
@Override
|
2024-07-02 19:05:21 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2024-07-02 19:24:48 +01:00
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
2024-07-02 19:47:51 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onGuildBan(@NotNull GuildBanEvent 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.onGuildMemberBan(guild, user, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onGuildUnban(@NotNull GuildUnbanEvent 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.onGuildMemberUnban(guild, user, event);
|
|
|
|
}
|
|
|
|
}
|
2024-07-02 19:55:50 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onGuildMemberUpdateTimeOut(@NotNull GuildMemberUpdateTimeOutEvent 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.onGuildMemberTimeout(guild, user, event);
|
|
|
|
}
|
|
|
|
}
|
2024-07-02 21:03:40 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUserUpdateName(@NotNull UserUpdateNameEvent event) {
|
|
|
|
if (event.getUser().isBot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BatUser user = userService.getUser(event.getUser().getId());
|
|
|
|
|
|
|
|
for (EventListener listener : LISTENERS) {
|
|
|
|
listener.onUserUpdateName(user, event.getOldName(), event.getNewName(), event);
|
|
|
|
}
|
|
|
|
}
|
2024-06-25 10:40:17 +01:00
|
|
|
}
|