forked from Fascinated/Bat
add message snipe feature
This commit is contained in:
@ -0,0 +1,95 @@
|
||||
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 cc.fascinated.bat.repository.DiscordMessageRepository;
|
||||
import lombok.NonNull;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import net.dv8tion.jda.api.events.message.MessageDeleteEvent;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.events.message.MessageUpdateEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Service
|
||||
@Log4j2
|
||||
@DependsOn("discordService")
|
||||
public class DiscordMessageService extends ListenerAdapter {
|
||||
private final DiscordMessageRepository discordMessageRepository;
|
||||
private final GuildService guildService;
|
||||
private final UserService userService;
|
||||
|
||||
@Autowired
|
||||
public DiscordMessageService(DiscordMessageRepository discordMessageRepository, @NonNull GuildService guildService,
|
||||
@NonNull UserService userService) {
|
||||
this.discordMessageRepository = discordMessageRepository;
|
||||
this.guildService = guildService;
|
||||
this.userService = userService;
|
||||
|
||||
DiscordService.JDA.addEventListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the message with the given id
|
||||
*
|
||||
* @param messageId the id of the message
|
||||
* @return the message with the given id
|
||||
*/
|
||||
public DiscordMessage getMessage(String messageId) {
|
||||
Optional<DiscordMessage> optionalMessage = discordMessageRepository.findById(messageId);
|
||||
return optionalMessage.orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
|
||||
discordMessageRepository.save(new DiscordMessage(
|
||||
event.getMessageId(),
|
||||
event.getMessage().getTimeCreated().toInstant().toEpochMilli(),
|
||||
event.getChannel().getId(),
|
||||
event.getGuild().getId(),
|
||||
event.getAuthor().getId(),
|
||||
event.getMessage().getContentRaw(),
|
||||
false
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageUpdate(@NotNull MessageUpdateEvent event) {
|
||||
Optional<DiscordMessage> message = discordMessageRepository.findById(event.getMessageId());
|
||||
if (message.isPresent()) {
|
||||
DiscordMessage discordMessage = message.get();
|
||||
if (discordMessage.getContent().equals(event.getMessage().getContentRaw())) {
|
||||
return;
|
||||
}
|
||||
discordMessage.setContent(event.getMessage().getContentRaw());
|
||||
discordMessageRepository.save(discordMessage);
|
||||
}
|
||||
|
||||
BatGuild guild = guildService.getGuild(event.getGuild().getId());
|
||||
BatUser user = userService.getUser(event.getAuthor().getId());
|
||||
for (EventListener listener : EventService.LISTENERS) {
|
||||
listener.onGuildMessageEdit(guild, user, event);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageDelete(@NotNull MessageDeleteEvent event) {
|
||||
Optional<DiscordMessage> optionalMessage = discordMessageRepository.findById(event.getMessageId());
|
||||
if (optionalMessage.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
DiscordMessage message = optionalMessage.get();
|
||||
message.setDeleted(true);
|
||||
discordMessageRepository.save(message);
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ 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.guild.member.GuildMemberJoinEvent;
|
||||
@ -11,7 +12,9 @@ import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameE
|
||||
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.message.MessageUpdateEvent;
|
||||
import net.dv8tion.jda.api.events.user.update.UserUpdateGlobalNameEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -36,11 +39,14 @@ public class EventService extends ListenerAdapter {
|
||||
public static final Set<EventListener> LISTENERS = new HashSet<>();
|
||||
private final GuildService guildService;
|
||||
private final UserService userService;
|
||||
private final DiscordMessageService discordMessageService;
|
||||
|
||||
@Autowired
|
||||
public EventService(@NonNull GuildService guildService, @NonNull UserService userService, @NonNull ApplicationContext context) {
|
||||
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);
|
||||
@ -95,6 +101,30 @@ public class EventService extends ListenerAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageUpdate(@NotNull MessageUpdateEvent 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.onGuildMessageEdit(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()) {
|
||||
|
Reference in New Issue
Block a user