mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-02 16:05:00 +00:00
added configurable warning and success reactions
changed that channels dont actually get deleted, but rather with a deleted flag, so the foreign keys are valid added more optionals to server/channel/message/member retrieval added throwing exceptions instead of just returning null fixed filtering of deleted channels while the bot was offline fixed channel listener added message deleted listener structure moved quartz properties to general application properties, because they were not found in the separate one added try catch to reminderJob, so that the job is not in an invalid state we now completely remove the starboard post, in case it falls under the threshold added code to 'ignore' a staroard post form further stars, in case the post in the starboard gets deleted now actually setting the reminded flag on a reminder added handnling in case the channel to remind in does not exist anymore
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package dev.sheldan.abstracto.core.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.exception.NotFoundException;
|
||||
import dev.sheldan.abstracto.core.models.ServerChannelUser;
|
||||
import dev.sheldan.abstracto.core.models.database.AEmote;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.entities.Emote;
|
||||
@@ -13,8 +15,10 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.security.auth.login.LoginException;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class BotService implements Bot {
|
||||
|
||||
private JDA instance;
|
||||
@@ -36,13 +40,21 @@ public class BotService implements Bot {
|
||||
|
||||
@Override
|
||||
public ServerChannelUser getServerChannelUser(Long serverId, Long channelId, Long userId) {
|
||||
TextChannel textChannelById = getTextChannelFromServer(serverId, channelId);
|
||||
Guild guildById = getGuildById(serverId);
|
||||
if(textChannelById != null) {
|
||||
Member member = guildById.getMemberById(userId);
|
||||
return ServerChannelUser.builder().guild(guildById).textChannel(textChannelById).member(member).build();
|
||||
Optional<Guild> guildOptional = getGuildById(serverId);
|
||||
if(guildOptional.isPresent()) {
|
||||
Guild guild = guildOptional.get();
|
||||
Optional<TextChannel> textChannelOptional = this.getTextChannelFromServer(guild, channelId);
|
||||
if(textChannelOptional.isPresent()) {
|
||||
TextChannel textChannel = textChannelOptional.get();
|
||||
Member member = guild.getMemberById(userId);
|
||||
return ServerChannelUser.builder().guild(guild).textChannel(textChannel).member(member).build();
|
||||
} else {
|
||||
throw new NotFoundException(String.format("Text channel %s not found in guild %s", channelId, serverId));
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new NotFoundException(String.format("Guild %s not found.", serverId));
|
||||
}
|
||||
throw new RuntimeException(String.format("Member %s or text channel %s not found in guild %s", userId, channelId, serverId));
|
||||
}
|
||||
|
||||
|
||||
@@ -59,37 +71,48 @@ public class BotService implements Bot {
|
||||
|
||||
@Override
|
||||
public void deleteMessage(Long serverId, Long channelId, Long messageId) {
|
||||
getTextChannelFromServer(serverId, channelId).deleteMessageById(messageId).queue();
|
||||
Optional<TextChannel> textChannelOptional = getTextChannelFromServer(serverId, channelId);
|
||||
if(textChannelOptional.isPresent()) {
|
||||
TextChannel textChannel = textChannelOptional.get();
|
||||
textChannel.deleteMessageById(messageId).queue(aVoid -> {}, throwable -> {
|
||||
log.warn("Failed to delete message {} in channel {} in guild {}", messageId, channelId, serverId, throwable);
|
||||
});
|
||||
} else {
|
||||
log.warn("Could not find channel {} in guild {} to delete message {} in.", channelId, serverId, messageId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Emote getEmote(Long serverId, AEmote emote) {
|
||||
public Optional<Emote> getEmote(Long serverId, AEmote emote) {
|
||||
if(!emote.getCustom()) {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
Guild guildById = getGuildById(serverId);
|
||||
return guildById.getEmoteById(emote.getEmoteId());
|
||||
Optional<Guild> guildById = getGuildById(serverId);
|
||||
if(guildById.isPresent()) {
|
||||
Guild guild = guildById.get();
|
||||
return Optional.ofNullable(guild.getEmoteById(emote.getEmoteId()));
|
||||
}
|
||||
throw new NotFoundException(String.format("Not able to find emote %s in server %s", emote.getId(), serverId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextChannel getTextChannelFromServer(Long serverId, Long textChannelId) {
|
||||
Guild guild = getGuildById(serverId);
|
||||
TextChannel textChannelById = guild.getTextChannelById(textChannelId);
|
||||
if(textChannelById != null) {
|
||||
return textChannelById;
|
||||
} else {
|
||||
throw new RuntimeException(String.format("Text channel %s in guild %s not found", textChannelId, serverId));
|
||||
}
|
||||
public Optional<TextChannel> getTextChannelFromServer(Guild guild, Long textChannelId) {
|
||||
return Optional.ofNullable(guild.getTextChannelById(textChannelId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Guild getGuildById(Long serverId) {
|
||||
Guild guildById = instance.getGuildById(serverId);
|
||||
if(guildById != null) {
|
||||
return guildById;
|
||||
} else {
|
||||
throw new RuntimeException(String.format("Guild %s not found", serverId));
|
||||
public Optional<TextChannel> getTextChannelFromServer(Long serverId, Long textChannelId) {
|
||||
Optional<Guild> guildOptional = getGuildById(serverId);
|
||||
if(guildOptional.isPresent()) {
|
||||
Guild guild = guildOptional.get();
|
||||
return Optional.ofNullable(guild.getTextChannelById(textChannelId));
|
||||
}
|
||||
throw new NotFoundException(String.format("Not able to find guild %s", serverId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Guild> getGuildById(Long serverId) {
|
||||
return Optional.ofNullable(instance.getGuildById(serverId));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -8,6 +8,8 @@ import net.dv8tion.jda.api.entities.MessageReaction;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class EmoteServiceBean implements EmoteService {
|
||||
@@ -38,9 +40,9 @@ public class EmoteServiceBean implements EmoteService {
|
||||
@Override
|
||||
public String getEmoteAsMention(AEmote emote, Long serverId, String defaultText) {
|
||||
if(emote != null && emote.getCustom()) {
|
||||
Emote emote1 = botService.getEmote(serverId, emote);
|
||||
if (emote1 != null) {
|
||||
return emote1.getAsMention();
|
||||
Optional<Emote> emoteOptional = botService.getEmote(serverId, emote);
|
||||
if (emoteOptional.isPresent()) {
|
||||
return emoteOptional.get().getAsMention();
|
||||
} else {
|
||||
log.warn("Emote {} with name {} in server {} defined, but not usable.", emote.getEmoteId(), emote.getName(), serverId);
|
||||
return defaultText;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.sheldan.abstracto.core.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.exception.NotFoundException;
|
||||
import dev.sheldan.abstracto.core.management.EmoteManagementService;
|
||||
import dev.sheldan.abstracto.core.management.UserManagementService;
|
||||
import dev.sheldan.abstracto.core.models.CachedMessage;
|
||||
@@ -7,10 +8,7 @@ import dev.sheldan.abstracto.core.models.CachedReaction;
|
||||
import dev.sheldan.abstracto.core.models.database.AUser;
|
||||
import dev.sheldan.abstracto.core.models.embed.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.entities.MessageReaction;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import net.dv8tion.jda.api.requests.restaction.pagination.ReactionPaginationAction;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
@@ -22,6 +20,7 @@ import org.springframework.stereotype.Component;
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@@ -81,10 +80,23 @@ public class MessageCacheBean implements MessageCache {
|
||||
@Async
|
||||
@Override
|
||||
public void loadMessage(CompletableFuture<CachedMessage> future, Long guildId, Long textChannelId, Long messageId) {
|
||||
TextChannel textChannelById = bot.getTextChannelFromServer(guildId, textChannelId);
|
||||
textChannelById.retrieveMessageById(messageId).queue(message -> {
|
||||
buildCachedMessageFromMessage(future, message);
|
||||
});
|
||||
Optional<Guild> guildOptional = bot.getGuildById(guildId);
|
||||
if(guildOptional.isPresent()) {
|
||||
Optional<TextChannel> textChannelByIdOptional = bot.getTextChannelFromServer(guildOptional.get(), textChannelId);
|
||||
if(textChannelByIdOptional.isPresent()) {
|
||||
TextChannel textChannel = textChannelByIdOptional.get();
|
||||
textChannel.retrieveMessageById(messageId).queue(message -> {
|
||||
buildCachedMessageFromMessage(future, message);
|
||||
});
|
||||
} else {
|
||||
log.warn("Not able to load message {} in channel {} in guild {}. Text channel not found.", messageId, textChannelId, guildId);
|
||||
future.completeExceptionally(new NotFoundException(String.format("Not able to load message %s. Text channel %s not found in guild %s", messageId, textChannelId, guildId)));
|
||||
}
|
||||
} else {
|
||||
log.warn("Not able to load message {} in channel {} in guild {}. Guild not found.", messageId, textChannelId, guildId);
|
||||
future.completeExceptionally(new NotFoundException(String.format("Not able to load message %s. Guild %s not found.", messageId, guildId)));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.sheldan.abstracto.core.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.exception.NotFoundException;
|
||||
import dev.sheldan.abstracto.core.management.EmoteManagementService;
|
||||
import dev.sheldan.abstracto.core.models.database.AEmote;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -23,22 +24,30 @@ public class MessageServiceBean implements MessageService {
|
||||
|
||||
@Override
|
||||
public void addReactionToMessage(String emoteKey, Long serverId, Message message) {
|
||||
Guild guildById = bot.getGuildById(serverId);
|
||||
Optional<Guild> guildByIdOptional = bot.getGuildById(serverId);
|
||||
Optional<AEmote> aEmote = emoteManagementService.loadEmoteByName(emoteKey, serverId);
|
||||
if(aEmote.isPresent()) {
|
||||
AEmote emote = aEmote.get();
|
||||
if(emote.getCustom()) {
|
||||
Emote emoteById = guildById.getEmoteById(emote.getEmoteId());
|
||||
if(emoteById != null) {
|
||||
message.addReaction(emoteById).queue();
|
||||
if(guildByIdOptional.isPresent()) {
|
||||
Guild guild = guildByIdOptional.get();
|
||||
if(aEmote.isPresent()) {
|
||||
AEmote emote = aEmote.get();
|
||||
if(emote.getCustom()) {
|
||||
Emote emoteById = guild.getEmoteById(emote.getEmoteId());
|
||||
if(emoteById != null) {
|
||||
message.addReaction(emoteById).queue();
|
||||
} else {
|
||||
log.warn("Emote with key {} and id {} for guild {} was not found.", emoteKey, emote.getEmoteId(), guild.getId());
|
||||
throw new NotFoundException(String.format("Emote with key `%s` and id %s in guild %s was not found. Check whether or not the configured emote is available.", emoteKey, emote.getEmoteId(), guild.getIdLong()));
|
||||
}
|
||||
} else {
|
||||
log.warn("Emote with key {} and id {} for guild {} was not found.", emoteKey, emote.getEmoteId(), guildById.getId());
|
||||
message.addReaction(emote.getEmoteKey()).queue();
|
||||
}
|
||||
} else {
|
||||
message.addReaction(emote.getEmoteKey()).queue();
|
||||
log.warn("Cannot add reaction, emote {} not defined for server {}.", emoteKey, serverId);
|
||||
throw new NotFoundException(String.format("Cannot add reaction. Emote `%s` not defined in server %s. Define the emote via the setEmote command.", emoteKey, serverId));
|
||||
}
|
||||
} else {
|
||||
log.warn("Cannot add reaction, emote {} not defined for server {}.", emoteKey, serverId);
|
||||
log.warn("Cannot add reaction, guild not found {}", serverId);
|
||||
throw new NotFoundException(String.format("Cannot add reaction, guild %s not found.", serverId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import javax.security.auth.login.LoginException;
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -66,7 +67,7 @@ public class StartupManager implements Startup {
|
||||
List<Guild> onlineGuilds = instance.getGuilds();
|
||||
Set<Long> availableServers = SnowflakeUtils.getSnowflakeIds(onlineGuilds);
|
||||
availableServers.forEach(aLong -> {
|
||||
AServer newAServer = serverManagementService.createServer(aLong);
|
||||
AServer newAServer = serverManagementService.loadOrCreate(aLong);
|
||||
Guild newGuild = instance.getGuildById(aLong);
|
||||
log.debug("Synchronizing server: {}", aLong);
|
||||
if(newGuild != null){
|
||||
@@ -95,10 +96,10 @@ public class StartupManager implements Startup {
|
||||
|
||||
private void synchronizeChannelsOf(Guild guild, AServer existingServer){
|
||||
List<GuildChannel> available = guild.getChannels();
|
||||
List<AChannel> knownChannels = existingServer.getChannels();
|
||||
List<AChannel> knownChannels = existingServer.getChannels().stream().filter(aChannel -> !aChannel.getDeleted()).collect(Collectors.toList());
|
||||
Set<Long> knownChannelsIds = SnowflakeUtils.getOwnItemsIds(knownChannels);
|
||||
Set<Long> existingChannelsIds = SnowflakeUtils.getSnowflakeIds(available);
|
||||
Set<Long> newChannels = SetUtils.disjunction(existingChannelsIds, knownChannelsIds);
|
||||
Set<Long> newChannels = SetUtils.difference(existingChannelsIds, knownChannelsIds);
|
||||
newChannels.forEach(aLong -> {
|
||||
GuildChannel channel1 = available.stream().filter(channel -> channel.getIdLong() == aLong).findFirst().get();
|
||||
log.debug("Adding new channel: {}", aLong);
|
||||
@@ -106,5 +107,10 @@ public class StartupManager implements Startup {
|
||||
AChannel newChannel = channelManagementService.createChannel(channel1.getIdLong(), type);
|
||||
serverManagementService.addChannelToServer(existingServer, newChannel);
|
||||
});
|
||||
|
||||
Set<Long> noLongAvailable = SetUtils.difference(knownChannelsIds, existingChannelsIds);
|
||||
noLongAvailable.forEach(aLong -> {
|
||||
channelManagementService.markAsDeleted(aLong);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,12 @@ import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.models.AChannelType;
|
||||
import dev.sheldan.abstracto.core.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.repository.ChannelRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ChannelManagementServiceBean implements ChannelManagementService {
|
||||
|
||||
@Autowired
|
||||
@@ -20,6 +22,20 @@ public class ChannelManagementServiceBean implements ChannelManagementService {
|
||||
|
||||
@Override
|
||||
public AChannel createChannel(Long id, AChannelType type) {
|
||||
return repository.save(AChannel.builder().id(id).type(type).build());
|
||||
log.info("Creating channel {} with type {}", id, type);
|
||||
return repository.save(AChannel.builder().id(id).type(type).deleted(false).build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markAsDeleted(Long id) {
|
||||
AChannel channel = loadChannel(id);
|
||||
channel.setDeleted(true);
|
||||
repository.save(channel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeChannel(Long id) {
|
||||
log.info("Deleting channel {}", id);
|
||||
repository.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class ConfigManagementServiceBean implements ConfigManagementService {
|
||||
|
||||
@Override
|
||||
public AConfig createConfig(Long serverId, String name, String value) {
|
||||
AServer server = serverManagementService.loadServer(serverId);
|
||||
AServer server = serverManagementService.loadOrCreate(serverId);
|
||||
AConfig config = AConfig
|
||||
.builder()
|
||||
.stringValue(value)
|
||||
@@ -54,7 +54,7 @@ public class ConfigManagementServiceBean implements ConfigManagementService {
|
||||
|
||||
@Override
|
||||
public AConfig createConfig(Long serverId, String name, Double value) {
|
||||
AServer server = serverManagementService.loadServer(serverId);
|
||||
AServer server = serverManagementService.loadOrCreate(serverId);
|
||||
AConfig config = AConfig
|
||||
.builder()
|
||||
.doubleValue(value)
|
||||
|
||||
@@ -33,7 +33,7 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
|
||||
|
||||
@Override
|
||||
public AEmote createCustomEmote(String name, String emoteKey, Long emoteId, Boolean animated, Long serverId) {
|
||||
AServer server = serverManagementService.loadServer(serverId);
|
||||
AServer server = serverManagementService.loadOrCreate(serverId);
|
||||
return this.createCustomEmote(name, emoteKey, emoteId, animated, server);
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
|
||||
|
||||
@Override
|
||||
public AEmote createDefaultEmote(String name, String emoteKey, Long serverId) {
|
||||
AServer server = serverManagementService.loadServer(serverId);
|
||||
AServer server = serverManagementService.loadOrCreate(serverId);
|
||||
return createDefaultEmote(name, emoteKey, server);
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
|
||||
|
||||
@Override
|
||||
public Optional<AEmote> loadEmoteByName(String name, Long serverId) {
|
||||
AServer server = serverManagementService.loadServer(serverId);
|
||||
AServer server = serverManagementService.loadOrCreate(serverId);
|
||||
return loadEmoteByName(name, server);
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
|
||||
|
||||
@Override
|
||||
public AEmote setEmoteToCustomEmote(String name, String emoteKey, Long emoteId, Boolean animated, Long serverId) {
|
||||
AServer server = serverManagementService.loadServer(serverId);
|
||||
AServer server = serverManagementService.loadOrCreate(serverId);
|
||||
AEmote emote;
|
||||
Optional<AEmote> emoteOptional = loadEmoteByName(name, server);
|
||||
if(!emoteOptional.isPresent()) {
|
||||
@@ -104,7 +104,7 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
|
||||
|
||||
@Override
|
||||
public AEmote setEmoteToCustomEmote(String name, Emote emote, Long serverId) {
|
||||
AServer server = serverManagementService.loadServer(serverId);
|
||||
AServer server = serverManagementService.loadOrCreate(serverId);
|
||||
AEmote emoteBeingSet;
|
||||
Optional<AEmote> emoteOptional = loadEmoteByName(name, serverId);
|
||||
if(!emoteOptional.isPresent()) {
|
||||
@@ -122,7 +122,7 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
|
||||
|
||||
@Override
|
||||
public AEmote setEmoteToDefaultEmote(String name, String emoteKey, Long serverId) {
|
||||
AServer server = serverManagementService.loadServer(serverId);
|
||||
AServer server = serverManagementService.loadOrCreate(serverId);
|
||||
AEmote emoteBeingSet;
|
||||
Optional<AEmote> emoteOptional = loadEmoteByName(name, serverId);
|
||||
if(!emoteOptional.isPresent()) {
|
||||
@@ -138,7 +138,7 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
|
||||
|
||||
@Override
|
||||
public boolean emoteExists(String name, Long serverId) {
|
||||
AServer server = serverManagementService.loadServer(serverId);
|
||||
AServer server = serverManagementService.loadOrCreate(serverId);
|
||||
return emoteExists(name, server);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class PostTargetManagementBean implements PostTargetManagement {
|
||||
@@ -63,7 +61,7 @@ public class PostTargetManagementBean implements PostTargetManagement {
|
||||
@Override
|
||||
public void createOrUpdate(String name, Long serverId, Long channelId) {
|
||||
AChannel dbChannel = channelManagementService.loadChannel(channelId);
|
||||
AServer dbServer = serverManagementService.loadServer(serverId);
|
||||
AServer dbServer = serverManagementService.loadOrCreate(serverId);
|
||||
createOrUpdate(name, dbServer, dbChannel);
|
||||
}
|
||||
|
||||
@@ -75,7 +73,7 @@ public class PostTargetManagementBean implements PostTargetManagement {
|
||||
|
||||
@Override
|
||||
public PostTarget getPostTarget(String name, Long serverId) {
|
||||
AServer server = serverManagementService.loadServer(serverId);
|
||||
AServer server = serverManagementService.loadOrCreate(serverId);
|
||||
return getPostTarget(name, server);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ public class ServerManagementServiceBean implements ServerManagementService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public AServer loadServer(Long id) {
|
||||
public AServer loadOrCreate(Long id) {
|
||||
if(repository.existsById(id)) {
|
||||
return repository.getOne(id);
|
||||
} else {
|
||||
@@ -66,13 +66,13 @@ public class ServerManagementServiceBean implements ServerManagementService {
|
||||
|
||||
@Override
|
||||
public AChannel getPostTarget(Long serverId, String name) {
|
||||
AServer server = this.loadServer(serverId);
|
||||
AServer server = this.loadOrCreate(serverId);
|
||||
return getPostTarget(server, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AChannel getPostTarget(Long serverId, PostTarget target) {
|
||||
AServer server = this.loadServer(serverId);
|
||||
AServer server = this.loadOrCreate(serverId);
|
||||
return getPostTarget(server, target);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ public class UserManagementServiceBean implements UserManagementService {
|
||||
@Override
|
||||
public AUserInAServer loadUser(Long serverId, Long userId) {
|
||||
AUser user = this.loadUser(userId);
|
||||
AServer server = serverManagementService.loadServer(serverId);
|
||||
AServer server = serverManagementService.loadOrCreate(serverId);
|
||||
return loadUser(server, user);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,44 +1,50 @@
|
||||
package dev.sheldan.abstracto.listener;
|
||||
|
||||
import dev.sheldan.abstracto.core.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.core.management.ServerManagementService;
|
||||
import dev.sheldan.abstracto.core.models.AChannelType;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.repository.ServerRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.events.channel.text.TextChannelCreateEvent;
|
||||
import net.dv8tion.jda.api.events.channel.text.TextChannelDeleteEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ChannelListener extends ListenerAdapter {
|
||||
|
||||
@Autowired
|
||||
private ServerRepository serverRepository;
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(ChannelListener.class);
|
||||
@Autowired
|
||||
private ChannelManagementService channelManagementService;
|
||||
|
||||
@Autowired
|
||||
private ServerManagementService serverManagementService;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void onTextChannelDelete(@Nonnull TextChannelDeleteEvent event) {
|
||||
AServer serverObject = serverRepository.getOne(event.getGuild().getIdLong());
|
||||
serverObject.getChannels().add(AChannel.builder().id(event.getChannel().getIdLong()).build());
|
||||
log.info("Handling channel delete event. Channel {}, Server {}", event.getChannel().getIdLong(), event.getGuild().getIdLong());
|
||||
channelManagementService.markAsDeleted(event.getChannel().getIdLong());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void onTextChannelCreate(@Nonnull TextChannelCreateEvent event) {
|
||||
log.info("Handling channel created event. Channel {}, Server {}", event.getChannel().getIdLong(), event.getGuild().getIdLong());
|
||||
AServer serverObject = serverRepository.getOne(event.getGuild().getIdLong());
|
||||
TextChannel createdChannel = event.getChannel();
|
||||
Optional<AChannel> possibleChannel = serverObject.getChannels().stream().filter(aChannel -> aChannel.id == createdChannel.getIdLong()).findAny();
|
||||
if(possibleChannel.isPresent()){
|
||||
serverObject.getChannels().remove(possibleChannel.get());
|
||||
logger.info("Adding channel {} with id {}", createdChannel.getName(), createdChannel.getIdLong());
|
||||
} else {
|
||||
logger.warn("Channel removed event for channel which was not in present");
|
||||
}
|
||||
AChannelType type = AChannel.getAChannelType(createdChannel.getType());
|
||||
AChannel newChannel = channelManagementService.createChannel(createdChannel.getIdLong(), type);
|
||||
serverManagementService.addChannelToServer(serverObject, newChannel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package dev.sheldan.abstracto.listener;
|
||||
|
||||
import dev.sheldan.abstracto.core.listener.MessageDeletedListener;
|
||||
import dev.sheldan.abstracto.core.models.CachedMessage;
|
||||
import dev.sheldan.abstracto.core.service.MessageCache;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class MessageDeletedListenerBean extends ListenerAdapter {
|
||||
@Autowired
|
||||
private List<MessageDeletedListener> listener;
|
||||
|
||||
@Autowired
|
||||
private MessageCache messageCache;
|
||||
|
||||
@Autowired
|
||||
private MessageDeletedListenerBean self;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void onGuildMessageDelete(@Nonnull GuildMessageDeleteEvent event) {
|
||||
messageCache.getMessageFromCache(event.getGuild().getIdLong(), event.getChannel().getIdLong(), event.getMessageIdLong()).thenAccept(cachedMessage -> {
|
||||
self.executeListener(cachedMessage);
|
||||
});
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void executeListener(CachedMessage cachedMessage) {
|
||||
listener.forEach(messageDeletedListener -> {
|
||||
try {
|
||||
messageDeletedListener.execute(cachedMessage);
|
||||
} catch (Exception e) {
|
||||
log.warn("Listener {} failed with exception:", messageDeletedListener.getClass().getName(), e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,6 @@ import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -99,7 +98,7 @@ public class MessageEmbedListener extends ListenerAdapter {
|
||||
|
||||
private MessageEmbeddedModel buildTemplateParameter(GuildMessageReceivedEvent event, CachedMessage embeddedMessage) {
|
||||
AChannel channel = channelManagementService.loadChannel(event.getChannel().getIdLong());
|
||||
AServer server = serverManagementService.loadServer(event.getGuild().getIdLong());
|
||||
AServer server = serverManagementService.loadOrCreate(event.getGuild().getIdLong());
|
||||
AUserInAServer user = userManagementService.loadUser(event.getMember());
|
||||
Member author = bot.getMemberInServer(embeddedMessage.getServerId(), embeddedMessage.getAuthorId());
|
||||
return MessageEmbeddedModel
|
||||
|
||||
Reference in New Issue
Block a user