added command to sync roles for the whole server

added command to remove a role from the experience roles
added leaderboard command
added rank command
fixed missfire configuration for cron jobs (they should not be executed)
added ability to configure recovery for jobs
added tracking of message count
added join listener to automatically give the appropriate role for a joined user
added parameter to join listener containing the userInAServer
re-added command exception templates
added method to create a status message (basically only a single message is returned and expected)
added method to edit a single message in a channel to channel service
fixed cases in which there are no embeds, but we still used the embed principle of sending messages (only text was send)
added more functions to channel service to send messages with
moved joined listeners to their separate transaction
This commit is contained in:
Sheldan
2020-04-12 19:35:42 +02:00
parent 6a31dfde8a
commit edb270e887
53 changed files with 828 additions and 25 deletions

View File

@@ -1,12 +1,15 @@
package dev.sheldan.abstracto.core.listener;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.FeatureFlagService;
import dev.sheldan.abstracto.core.service.management.UserManagementService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Nonnull;
@@ -22,6 +25,9 @@ public class JoinListenerBean extends ListenerAdapter {
@Autowired
private FeatureFlagService featureFlagService;
@Autowired
private UserManagementService userManagementService;
@Override
@Transactional
public void onGuildMemberJoin(@Nonnull GuildMemberJoinEvent event) {
@@ -30,10 +36,16 @@ public class JoinListenerBean extends ListenerAdapter {
return;
}
try {
joinListener.execute(event.getMember(), event.getGuild());
AUserInAServer aUserInAServer = userManagementService.loadUser(event.getMember());
executeListener(event, joinListener, aUserInAServer);
} catch (AbstractoRunTimeException e) {
log.error("Listener {} failed with exception:", joinListener.getClass().getName(), e);
}
});
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void executeListener(@Nonnull GuildMemberJoinEvent event, JoinListener joinListener, AUserInAServer aUserInAServer) {
joinListener.execute(event.getMember(), event.getGuild(), aUserInAServer);
}
}

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.exception.ChannelException;
import dev.sheldan.abstracto.core.exception.GuildException;
import dev.sheldan.abstracto.templating.model.MessageToSend;
@@ -7,6 +8,7 @@ import dev.sheldan.abstracto.core.models.database.AChannel;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.requests.restaction.MessageAction;
import org.apache.commons.lang3.StringUtils;
@@ -27,11 +29,16 @@ public class ChannelServiceBean implements ChannelService {
@Override
public void sendTextInAChannel(String text, AChannel channel) {
sendTextInAChannelFuture(text, channel);
}
@Override
public CompletableFuture<Message> sendTextInAChannelFuture(String text, AChannel channel) {
Guild guild = botService.getInstance().getGuildById(channel.getServer().getId());
if (guild != null) {
TextChannel textChannel = guild.getTextChannelById(channel.getId());
if(textChannel != null) {
textChannel.sendMessage(text).queue();
return textChannel.sendMessage(text).submit();
} else {
log.error("Channel {} to post towards was not found in server {}", channel.getId(), channel.getServer().getId());
throw new ChannelException(String.format("Channel %s to post to not found.", channel.getId()));
@@ -42,6 +49,28 @@ public class ChannelServiceBean implements ChannelService {
}
}
@Override
public CompletableFuture<Message> sendEmbedInAChannelFuture(MessageEmbed embed, AChannel channel) {
Guild guild = botService.getInstance().getGuildById(channel.getServer().getId());
if (guild != null) {
TextChannel textChannel = guild.getTextChannelById(channel.getId());
if(textChannel != null) {
return sendEmbedInAChannelFuture(embed, textChannel);
} else {
log.error("Channel {} to post towards was not found in server {}", channel.getId(), channel.getServer().getId());
throw new ChannelException(String.format("Channel %s to post to not found.", channel.getId()));
}
} else {
log.error("Guild {} was not found when trying to post a message", channel.getServer().getId());
throw new GuildException(String.format("Guild %s to post in channel %s was not found.", channel.getServer().getId(), channel.getId()));
}
}
@Override
public CompletableFuture<Message> sendEmbedInAChannelFuture(MessageEmbed embed, TextChannel channel) {
return channel.sendMessage(embed).submit();
}
@Override
public List<CompletableFuture<Message>> sendMessageToEndInAChannel(MessageToSend messageToSend, AChannel channel) {
Optional<TextChannel> textChannelFromServer = botService.getTextChannelFromServer(channel.getServer().getId(), channel.getId());
@@ -57,17 +86,15 @@ public class ChannelServiceBean implements ChannelService {
List<CompletableFuture<Message>> futures = new ArrayList<>();
if(StringUtils.isBlank(messageText)) {
messageToSend.getEmbeds().forEach(embed -> {
CompletableFuture<Message> messageFuture = textChannel.sendMessage(embed).submit();
futures.add(messageFuture);
futures.add(sendEmbedInAChannelFuture(embed, textChannel));
});
} else {
MessageAction messageAction = textChannel.sendMessage(messageText);
if(messageToSend.getEmbeds().size() > 0) {
if(messageToSend.getEmbeds() != null && messageToSend.getEmbeds().size() > 0) {
CompletableFuture<Message> messageFuture = messageAction.embed(messageToSend.getEmbeds().get(0)).submit();
futures.add(messageFuture);
messageToSend.getEmbeds().stream().skip(1).forEach(embed -> {
CompletableFuture<Message> nextEmbedFuture = textChannel.sendMessage(embed).submit();
futures.add(nextEmbedFuture);
futures.add(sendEmbedInAChannelFuture(embed, textChannel));
});
} else {
futures.add(messageAction.submit());
@@ -80,4 +107,33 @@ public class ChannelServiceBean implements ChannelService {
public Optional<TextChannel> getTextChannelInGuild(Long serverId, Long channelId) {
return botService.getTextChannelFromServer(serverId, channelId);
}
@Override
public void editMessageInAChannel(MessageToSend messageToSend, AChannel channel, Long messageId) {
Optional<TextChannel> textChannelFromServer = botService.getTextChannelFromServer(channel.getServer().getId(), channel.getId());
if(textChannelFromServer.isPresent()) {
TextChannel textChannel = textChannelFromServer.get();
editMessageInAChannel(messageToSend, textChannel, messageId);
} else {
throw new ChannelException(String.format("Channel %s was not found.", channel.getId()));
}
}
@Override
public void editMessageInAChannel(MessageToSend messageToSend, TextChannel channel, Long messageId) {
MessageAction messageAction;
if(!StringUtils.isBlank(messageToSend.getMessage())) {
messageAction = channel.editMessageById(messageId, messageToSend.getMessage());
if(messageToSend.getEmbeds() != null && messageToSend.getEmbeds().size() > 0) {
messageAction = messageAction.embed(messageToSend.getEmbeds().get(0));
}
} else {
if(messageToSend.getEmbeds() != null && messageToSend.getEmbeds().size() > 0) {
messageAction = channel.editMessageById(messageId, messageToSend.getEmbeds().get(0));
} else {
throw new AbstractoRunTimeException("Message to send did not contain anything to send.");
}
}
messageAction.queue();
}
}

View File

@@ -2,8 +2,10 @@ package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.exception.EmoteException;
import dev.sheldan.abstracto.core.exception.GuildException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.service.management.EmoteManagementService;
import dev.sheldan.abstracto.core.models.database.AEmote;
import dev.sheldan.abstracto.templating.model.MessageToSend;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Emote;
import net.dv8tion.jda.api.entities.Guild;
@@ -27,6 +29,9 @@ public class MessageServiceBean implements MessageService {
@Autowired
private EmoteService emoteService;
@Autowired
private ChannelService channelService;
@Override
public void addReactionToMessage(String emoteKey, Long serverId, Message message) {
Optional<Guild> guildByIdOptional = botService.getGuildById(serverId);
@@ -59,4 +64,14 @@ public class MessageServiceBean implements MessageService {
public CompletableFuture<Void> deleteMessageInChannelInServer(Long serverId, Long channelId, Long messageId) {
return botService.deleteMessage(serverId, channelId, messageId);
}
@Override
public CompletableFuture<Message> createStatusMessage(MessageToSend messageToSend, AChannel channel) {
return channelService.sendMessageToEndInAChannel(messageToSend, channel).get(0);
}
@Override
public void updateStatusMessage(AChannel channel, Long messageId, MessageToSend messageToSend) {
channelService.editMessageInAChannel(messageToSend, channel, messageId);
}
}

View File

@@ -42,8 +42,7 @@ public class PostTargetServiceBean implements PostTargetService {
@Override
public CompletableFuture<Message> sendTextInPostTarget(String text, PostTarget target) {
TextChannel textChannelForPostTarget = getTextChannelForPostTarget(target);
return textChannelForPostTarget.sendMessage(text).submit();
return channelService.sendTextInAChannelFuture(text, target.getChannelReference());
}
@Override

View File

@@ -45,6 +45,11 @@ public class UserManagementServiceBean implements UserManagementService {
return this.loadUser(member.getGuild().getIdLong(), member.getIdLong());
}
@Override
public AUserInAServer loadUserInServer(Long userInServerId) {
return userInServerRepository.getOne(userInServerId);
}
@Override
public AUserInAServer createUserInServer(Member member) {
return this.createUserInServer(member.getGuild().getIdLong(), member.getIdLong());

View File

@@ -0,0 +1 @@
The necessary parameters were not found. A '${class.simpleName}' was expected as '${parameterName}'. Consult help to see the correct syntax.

View File

@@ -0,0 +1 @@
Insufficient parameters: ${parameterName} was not found.

View File

@@ -0,0 +1 @@
The parameter ${parameterName} had a too large value: ${actualLength}. The maximum is: ${maximumLength}.

View File

@@ -15,7 +15,7 @@ public class FeatureEnabledCondition implements CommandCondition {
@Override
public ConditionResult shouldExecute(CommandContext context, Command command) {
String featureName = command.getFeature();
boolean featureFlagValue = false;
boolean featureFlagValue = true;
String reason = "";
if(featureName != null) {
featureFlagValue = featureFlagManagementService.getFeatureFlagValue(featureName, context.getGuild().getIdLong());

View File

@@ -1,8 +1,9 @@
package dev.sheldan.abstracto.core.listener;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
public interface JoinListener extends FeatureAware {
void execute(Member member, Guild guild);
void execute(Member member, Guild guild, AUserInAServer aUserInAServer);
}

View File

@@ -3,6 +3,7 @@ package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.models.database.AChannel;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.TextChannel;
import java.util.List;
@@ -11,7 +12,12 @@ import java.util.concurrent.CompletableFuture;
public interface ChannelService {
void sendTextInAChannel(String text, AChannel channel);
CompletableFuture<Message> sendTextInAChannelFuture(String text, AChannel channel);
CompletableFuture<Message> sendEmbedInAChannelFuture(MessageEmbed embed, AChannel channel);
CompletableFuture<Message> sendEmbedInAChannelFuture(MessageEmbed embed, TextChannel channel);
List<CompletableFuture<Message>> sendMessageToEndInAChannel(MessageToSend messageToSend, AChannel channel);
List<CompletableFuture<Message>> sendMessageToEndInTextChannel(MessageToSend messageToSend, TextChannel textChannel);
Optional<TextChannel> getTextChannelInGuild(Long serverId, Long channelId);
void editMessageInAChannel(MessageToSend messageToSend, AChannel channel, Long messageId);
void editMessageInAChannel(MessageToSend messageToSend, TextChannel channel, Long messageId);
}

View File

@@ -1,10 +1,15 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.templating.model.MessageToSend;
import net.dv8tion.jda.api.entities.Message;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public interface MessageService {
void addReactionToMessage(String emoteKey, Long serverId, Message message);
CompletableFuture<Void> deleteMessageInChannelInServer(Long serverId, Long channelId, Long messageId);
CompletableFuture<Message> createStatusMessage(MessageToSend messageToSend, AChannel channel);
void updateStatusMessage(AChannel channel, Long messageId, MessageToSend messageToSend);
}

View File

@@ -9,6 +9,7 @@ public interface UserManagementService {
AUserInAServer loadUser(Long serverId, Long userId);
AUserInAServer loadUser(AServer server, AUser user);
AUserInAServer loadUser(Member member);
AUserInAServer loadUserInServer(Long userInServerId);
AUserInAServer createUserInServer(Member member);
AUserInAServer createUserInServer(Long guildId, Long userId);
AUser createUser(Member member);