[AB-365] introducing slash commands for a selection of commands

adding method for pinning a message
moving suggestion to correct deployment
This commit is contained in:
Sheldan
2022-05-17 00:39:06 +02:00
parent 1913bc930d
commit 1d6de3f1e8
286 changed files with 8021 additions and 3065 deletions

View File

@@ -4,19 +4,26 @@ import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.config.SlashCommandConfig;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.slash.parameter.SlashCommandParameterService;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.exception.EntityGuildMismatchException;
import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
import dev.sheldan.abstracto.experience.config.ExperienceFeatureDefinition;
import dev.sheldan.abstracto.experience.config.ExperienceSlashCommandNames;
import dev.sheldan.abstracto.experience.service.management.DisabledExpRoleManagementService;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to add a role to the roles for which experience has been disabled.
@@ -24,21 +31,31 @@ import java.util.List;
@Component
public class DisableExpForRole extends AbstractConditionableCommand {
private static final String DISABLE_EXP_FOR_ROLE_COMMAND = "disableExpForRole";
private static final String DISABLE_EXP_FOR_ROLE_RESPONSE = "disableExpForRole_response";
private static final String ROLE_PARAMETER = "role";
@Autowired
private DisabledExpRoleManagementService disabledExpRoleManagementService;
@Autowired
private RoleManagementService roleManagementService;
@Autowired
private SlashCommandParameterService slashCommandParameterService;
@Autowired
private InteractionService interactionService;
@Override
public CommandResult execute(CommandContext commandContext) {
List<Object> parameters = commandContext.getParameters().getParameters();
ARole role = (ARole) parameters.get(0);
ARole actualRole = roleManagementService.findRole(role.getId());
Role role = (Role) parameters.get(0);
ARole actualRole = roleManagementService.findRole(role.getIdLong());
if(!actualRole.getServer().getId().equals(commandContext.getGuild().getIdLong())) {
throw new EntityGuildMismatchException();
}
// as we mange experience disabled roles via the existence of them in a table, we should not do anything
// as we manage experience disabled roles via the existence of them in a table, we should not do anything
// in case it is used a second time as a disabled experience role
if(!disabledExpRoleManagementService.isExperienceDisabledForRole(actualRole)) {
disabledExpRoleManagementService.setRoleToBeDisabledForExp(actualRole);
@@ -46,15 +63,48 @@ public class DisableExpForRole extends AbstractConditionableCommand {
return CommandResult.fromSuccess();
}
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
Role role = slashCommandParameterService.getCommandOption(ROLE_PARAMETER, event, Role.class);
ARole actualRole = roleManagementService.findRole(role.getIdLong());
if(!actualRole.getServer().getId().equals(event.getGuild().getIdLong())) {
throw new EntityGuildMismatchException();
}
// as we manage experience disabled roles via the existence of them in a table, we should not do anything
// in case it is used a second time as a disabled experience role
if(!disabledExpRoleManagementService.isExperienceDisabledForRole(actualRole)) {
disabledExpRoleManagementService.setRoleToBeDisabledForExp(actualRole);
}
return interactionService.replyEmbed(DISABLE_EXP_FOR_ROLE_RESPONSE, event)
.thenApply(interactionHook -> CommandResult.fromSuccess());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("role").templated(true).type(ARole.class).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
Parameter roleParameter = Parameter
.builder()
.name(ROLE_PARAMETER)
.templated(true)
.type(Role.class)
.build();
List<Parameter> parameters = Arrays.asList(roleParameter);
HelpInfo helpInfo = HelpInfo
.builder()
.templated(true)
.build();
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.rootCommandName(ExperienceSlashCommandNames.EXPERIENCE_CONFIG)
.commandName(DISABLE_EXP_FOR_ROLE_COMMAND)
.build();
return CommandConfiguration.builder()
.name("disableExpForRole")
.name(DISABLE_EXP_FOR_ROLE_COMMAND)
.module(ExperienceModuleDefinition.EXPERIENCE)
.templated(true)
.slashCommandConfig(slashCommandConfig)
.supportsEmbedException(true)
.causesReaction(true)
.parameters(parameters)

View File

@@ -4,20 +4,26 @@ import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.config.SlashCommandConfig;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.slash.parameter.SlashCommandParameterService;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.exception.EntityGuildMismatchException;
import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.experience.config.ExperienceFeatureDefinition;
import dev.sheldan.abstracto.experience.config.ExperienceSlashCommandNames;
import dev.sheldan.abstracto.experience.service.AUserExperienceService;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to disable the experience gain for a specific member
@@ -25,12 +31,21 @@ import java.util.List;
@Component
public class DisableExpGain extends AbstractConditionableCommand {
private static final String MEMBER_PARAMETER = "member";
private static final String DISABLE_EXP_GAIN_COMMAND = "disableExpGain";
private static final String DISABLE_EXP_GAIN_RESPONSE = "disableExpGain_response";
@Autowired
private AUserExperienceService aUserExperienceService;
@Autowired
private UserInServerManagementService userInServerManagementService;
@Autowired
private SlashCommandParameterService slashCommandParameterService;
@Autowired
private InteractionService interactionService;
@Override
public CommandResult execute(CommandContext commandContext) {
Member para = (Member) commandContext.getParameters().getParameters().get(0);
@@ -42,14 +57,43 @@ public class DisableExpGain extends AbstractConditionableCommand {
return CommandResult.fromSuccess();
}
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
Member para = slashCommandParameterService.getCommandOption(MEMBER_PARAMETER, event, Member.class);
if(!para.getGuild().equals(event.getGuild())) {
throw new EntityGuildMismatchException();
}
AUserInAServer userInAServer = userInServerManagementService.loadOrCreateUser(para);
aUserExperienceService.disableExperienceForUser(userInAServer);
return interactionService.replyEmbed(DISABLE_EXP_GAIN_RESPONSE, event)
.thenApply(interactionHook -> CommandResult.fromSuccess());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("member").templated(true).type(Member.class).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
Parameter memberParameter = Parameter
.builder()
.name(MEMBER_PARAMETER)
.templated(true)
.type(Member.class)
.build();
List<Parameter> parameters = Arrays.asList(memberParameter);
HelpInfo helpInfo = HelpInfo
.builder()
.templated(true)
.build();
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.rootCommandName(ExperienceSlashCommandNames.EXPERIENCE_CONFIG)
.commandName(DISABLE_EXP_GAIN_COMMAND)
.build();
return CommandConfiguration.builder()
.name("disableExpGain")
.name(DISABLE_EXP_GAIN_COMMAND)
.module(ExperienceModuleDefinition.EXPERIENCE)
.slashCommandConfig(slashCommandConfig)
.causesReaction(true)
.supportsEmbedException(true)
.templated(true)

View File

@@ -4,19 +4,28 @@ import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.config.SlashCommandConfig;
import dev.sheldan.abstracto.core.command.exception.SlashCommandParameterMissingException;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.slash.parameter.SlashCommandParameterService;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.exception.EntityGuildMismatchException;
import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
import dev.sheldan.abstracto.experience.config.ExperienceFeatureDefinition;
import dev.sheldan.abstracto.experience.config.ExperienceSlashCommandNames;
import dev.sheldan.abstracto.experience.service.management.DisabledExpRoleManagementService;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to remove a role from the list of roles for which experience is disabled
@@ -24,12 +33,22 @@ import java.util.List;
@Component
public class EnableExpForRole extends AbstractConditionableCommand {
private static final String ENABLE_EXP_FOR_ROLE_COMMAND = "enableExpForRole";
private static final String ENABLE_EXP_FOR_ROLE_RESPONSE = "enableExpForRole_response";
private static final String ROLE_PARAMETER = "role";
@Autowired
private DisabledExpRoleManagementService disabledExpRoleManagementService;
@Autowired
private RoleManagementService roleManagementService;
@Autowired
private SlashCommandParameterService slashCommandParameterService;
@Autowired
private InteractionService interactionService;
@Override
public CommandResult execute(CommandContext commandContext) {
ARole role = (ARole) commandContext.getParameters().getParameters().get(0);
@@ -44,15 +63,51 @@ public class EnableExpForRole extends AbstractConditionableCommand {
return CommandResult.fromSuccess();
}
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
ARole actualRole;
if(slashCommandParameterService.hasCommandOptionWithFullType(ROLE_PARAMETER, event, OptionType.ROLE)) {
Role role = slashCommandParameterService.getCommandOption(ROLE_PARAMETER, event, ARole.class, Role.class);
actualRole = roleManagementService.findRole(role.getIdLong());
} else if(slashCommandParameterService.hasCommandOptionWithFullType(ROLE_PARAMETER, event, OptionType.STRING)) {
String roleId = slashCommandParameterService.getCommandOption(ROLE_PARAMETER, event, ARole.class, String.class);
actualRole = roleManagementService.findRole(Long.parseLong(roleId));
} else {
throw new SlashCommandParameterMissingException(ROLE_PARAMETER);
}
if(disabledExpRoleManagementService.isExperienceDisabledForRole(actualRole)) {
disabledExpRoleManagementService.removeRoleToBeDisabledForExp(actualRole);
}
return interactionService.replyEmbed(ENABLE_EXP_FOR_ROLE_RESPONSE, event)
.thenApply(interactionHook -> CommandResult.fromSuccess());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("role").templated(true).type(ARole.class).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
Parameter roleParameter = Parameter
.builder()
.name(ROLE_PARAMETER)
.templated(true)
.type(ARole.class)
.build();
List<Parameter> parameters = Arrays.asList(roleParameter);
HelpInfo helpInfo = HelpInfo
.builder()
.templated(true)
.build();
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.rootCommandName(ExperienceSlashCommandNames.EXPERIENCE_CONFIG)
.commandName(ENABLE_EXP_FOR_ROLE_COMMAND)
.build();
return CommandConfiguration.builder()
.name("enableExpForRole")
.name(ENABLE_EXP_FOR_ROLE_COMMAND)
.module(ExperienceModuleDefinition.EXPERIENCE)
.templated(true)
.slashCommandConfig(slashCommandConfig)
.causesReaction(true)
.supportsEmbedException(true)
.parameters(parameters)

View File

@@ -4,20 +4,26 @@ import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.config.SlashCommandConfig;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.slash.parameter.SlashCommandParameterService;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.exception.EntityGuildMismatchException;
import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.experience.config.ExperienceFeatureDefinition;
import dev.sheldan.abstracto.experience.config.ExperienceSlashCommandNames;
import dev.sheldan.abstracto.experience.service.AUserExperienceService;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command to enable experience gain for a member
@@ -25,30 +31,69 @@ import java.util.List;
@Component
public class EnableExpGain extends AbstractConditionableCommand {
private static final String ENABLE_EXP_GAIN_COMMAND = "enableExpGain";
private static final String ENABLE_EXP_GAIN_RESPONSE = "enableExpGain_response";
private static final String MEMBER_PARAMETER = "member";
@Autowired
private AUserExperienceService aUserExperienceService;
@Autowired
private UserInServerManagementService userInServerManagementService;
@Autowired
private SlashCommandParameterService slashCommandParameterService;
@Autowired
private InteractionService interactionService;
@Override
public CommandResult execute(CommandContext commandContext) {
Member para = (Member) commandContext.getParameters().getParameters().get(0);
if(!para.getGuild().equals(commandContext.getGuild())) {
Member member = (Member) commandContext.getParameters().getParameters().get(0);
if(!member.getGuild().equals(commandContext.getGuild())) {
throw new EntityGuildMismatchException();
}
AUserInAServer userInAServer = userInServerManagementService.loadOrCreateUser(para);
AUserInAServer userInAServer = userInServerManagementService.loadOrCreateUser(member);
aUserExperienceService.enableExperienceForUser(userInAServer);
return CommandResult.fromSuccess();
}
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
Member member = slashCommandParameterService.getCommandOption(MEMBER_PARAMETER, event, Member.class);
if(!member.getGuild().equals(event.getGuild())) {
throw new EntityGuildMismatchException();
}
AUserInAServer userInAServer = userInServerManagementService.loadOrCreateUser(member);
aUserExperienceService.enableExperienceForUser(userInAServer);
return interactionService.replyEmbed(ENABLE_EXP_GAIN_RESPONSE, event)
.thenApply(interactionHook -> CommandResult.fromSuccess());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("member").templated(true).type(Member.class).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
Parameter memberParameter = Parameter
.builder()
.name(MEMBER_PARAMETER)
.templated(true)
.type(Member.class)
.build();
List<Parameter> parameters = Arrays.asList(memberParameter);
HelpInfo helpInfo = HelpInfo
.builder()
.templated(true)
.build();
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.rootCommandName(ExperienceSlashCommandNames.EXPERIENCE_CONFIG)
.commandName(ENABLE_EXP_GAIN_COMMAND)
.build();
return CommandConfiguration.builder()
.name("enableExpGain")
.name(ENABLE_EXP_GAIN_COMMAND)
.slashCommandConfig(slashCommandConfig)
.module(ExperienceModuleDefinition.EXPERIENCE)
.causesReaction(true)
.supportsEmbedException(true)

View File

@@ -4,18 +4,24 @@ import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.config.SlashCommandConfig;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.slash.parameter.SlashCommandParameterService;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.service.ConfigService;
import dev.sheldan.abstracto.experience.config.ExperienceFeatureDefinition;
import dev.sheldan.abstracto.experience.config.ExperienceFeatureConfig;
import dev.sheldan.abstracto.experience.config.ExperienceSlashCommandNames;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
@@ -25,9 +31,19 @@ import java.util.List;
@Slf4j
public class ExpScale extends AbstractConditionableCommand {
private static final String EXP_SCALE_COMMAND = "expScale";
private static final String EXP_SCALE_RESPONSE = "expScale_response";
private static final String SCALE_PARAMETER = "scale";
@Autowired
private ConfigService configService;
@Autowired
private SlashCommandParameterService slashCommandParameterService;
@Autowired
private InteractionService interactionService;
@Override
public CommandResult execute(CommandContext commandContext) {
Double scale = (Double) commandContext.getParameters().getParameters().get(0);
@@ -37,15 +53,42 @@ public class ExpScale extends AbstractConditionableCommand {
return CommandResult.fromSuccess();
}
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
Double newScale = slashCommandParameterService.getCommandOption(SCALE_PARAMETER, event, Double.class);
configService.setDoubleValue(ExperienceFeatureConfig.EXP_MULTIPLIER_KEY, event.getGuild().getIdLong(), newScale);
return interactionService.replyEmbed(EXP_SCALE_RESPONSE, event)
.thenApply(interactionHook -> CommandResult.fromSuccess());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("scale").templated(true).type(Double.class).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
Parameter newScale = Parameter
.builder()
.name(SCALE_PARAMETER)
.templated(true)
.type(Double.class)
.build();
parameters.add(newScale);
HelpInfo helpInfo = HelpInfo
.builder()
.templated(true)
.build();
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.rootCommandName(ExperienceSlashCommandNames.EXPERIENCE_CONFIG)
.commandName(EXP_SCALE_COMMAND)
.build();
return CommandConfiguration.builder()
.name("expScale")
.name(EXP_SCALE_COMMAND)
.module(ExperienceModuleDefinition.EXPERIENCE)
.causesReaction(true)
.slashCommandConfig(slashCommandConfig)
.templated(true)
.supportsEmbedException(true)
.parameters(parameters)

View File

@@ -1,15 +1,13 @@
package dev.sheldan.abstracto.experience.command;
import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand;
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.config.ParameterValidator;
import dev.sheldan.abstracto.core.command.config.*;
import dev.sheldan.abstracto.core.command.config.validator.MinIntegerValueValidator;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
import dev.sheldan.abstracto.core.command.slash.parameter.SlashCommandParameterService;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.ChannelService;
@@ -17,6 +15,7 @@ import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.experience.config.ExperienceFeatureDefinition;
import dev.sheldan.abstracto.experience.config.ExperienceSlashCommandNames;
import dev.sheldan.abstracto.experience.converter.LeaderBoardModelConverter;
import dev.sheldan.abstracto.experience.model.LeaderBoard;
import dev.sheldan.abstracto.experience.model.LeaderBoardEntry;
@@ -26,6 +25,8 @@ import dev.sheldan.abstracto.experience.service.AUserExperienceService;
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -42,6 +43,8 @@ import java.util.concurrent.CompletableFuture;
public class LeaderBoardCommand extends AbstractConditionableCommand {
public static final String LEADER_BOARD_POST_EMBED_TEMPLATE = "leaderboard_post";
private static final String LEADERBOARD_PARAMTER = "leaderboard";
private static final String PAGE_PARAMETER = "page";
@Autowired
private AUserExperienceService userExperienceService;
@@ -60,43 +63,89 @@ public class LeaderBoardCommand extends AbstractConditionableCommand {
@Autowired
private UserInServerManagementService userInServerManagementService;
@Autowired
private SlashCommandParameterService slashCommandParameterService;
@Autowired
private InteractionService interactionService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
List<Object> parameters = commandContext.getParameters().getParameters();
// parameter is optional, in case its not present, we default to the 0th page
Integer page = !parameters.isEmpty() ? (Integer) parameters.get(0) : 1;
AServer server = serverManagementService.loadServer(commandContext.getGuild());
return getMessageToSend(commandContext.getAuthor(), page)
.thenCompose(messageToSend -> FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel())))
.thenApply(aVoid -> CommandResult.fromIgnored());
}
private CompletableFuture<MessageToSend> getMessageToSend(Member actorUser, Integer page) {
AServer server = serverManagementService.loadServer(actorUser.getGuild());
LeaderBoard leaderBoard = userExperienceService.findLeaderBoardData(server, page);
LeaderBoardModel leaderBoardModel = (LeaderBoardModel) ContextConverter.slimFromCommandContext(commandContext, LeaderBoardModel.class);
List<CompletableFuture> futures = new ArrayList<>();
CompletableFuture<List<LeaderBoardEntryModel>> completableFutures = converter.fromLeaderBoard(leaderBoard);
futures.add(completableFutures);
log.info("Rendering leaderboard for page {} in server {} for user {}.", page, commandContext.getAuthor().getId(), commandContext.getGuild().getId());
AUserInAServer aUserInAServer = userInServerManagementService.loadOrCreateUser(commandContext.getAuthor());
log.info("Rendering leaderboard for page {} in server {} for user {}.", page, actorUser.getId(), actorUser.getGuild().getId());
AUserInAServer aUserInAServer = userInServerManagementService.loadOrCreateUser(actorUser);
LeaderBoardEntry userRank = userExperienceService.getRankOfUserInServer(aUserInAServer);
CompletableFuture<List<LeaderBoardEntryModel>> userRankFuture = converter.fromLeaderBoardEntry(Arrays.asList(userRank));
futures.add(userRankFuture);
return FutureUtils.toSingleFuture(futures).thenCompose(aVoid -> {
List<LeaderBoardEntryModel> finalModels = completableFutures.join();
leaderBoardModel.setUserExperiences(finalModels);
leaderBoardModel.setUserExecuting(userRankFuture.join().get(0));
MessageToSend messageToSend = templateService.renderEmbedTemplate(LEADER_BOARD_POST_EMBED_TEMPLATE, leaderBoardModel, commandContext.getGuild().getIdLong());
return FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel()));
}).thenApply(aVoid -> CommandResult.fromIgnored());
LeaderBoardModel leaderBoardModel = LeaderBoardModel
.builder()
.userExperiences(finalModels)
.userExecuting(userRankFuture.join().get(0))
.build();
return CompletableFuture.completedFuture(templateService.renderEmbedTemplate(LEADER_BOARD_POST_EMBED_TEMPLATE, leaderBoardModel, actorUser.getGuild().getIdLong()));
});
}
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
Integer page;
if(slashCommandParameterService.hasCommandOption(PAGE_PARAMETER, event)) {
page = slashCommandParameterService.getCommandOption(PAGE_PARAMETER, event, Integer.class);
} else {
page = 1;
}
return getMessageToSend(event.getMember(), page)
.thenCompose(messageToSend -> interactionService.replyMessageToSend(messageToSend, event))
.thenApply(aVoid -> CommandResult.fromIgnored());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
List<ParameterValidator> leaderBoardPageValidators = Arrays.asList(MinIntegerValueValidator.min(0L));
parameters.add(Parameter.builder().name("page").validators(leaderBoardPageValidators).optional(true).templated(true).type(Integer.class).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
Parameter pageParameter = Parameter
.builder()
.name(PAGE_PARAMETER)
.validators(leaderBoardPageValidators)
.optional(true)
.templated(true)
.type(Integer.class)
.build();
List<Parameter> parameters = Arrays.asList(pageParameter);
HelpInfo helpInfo = HelpInfo
.builder()
.templated(true)
.build();
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.rootCommandName(ExperienceSlashCommandNames.EXPERIENCE)
.commandName(LEADERBOARD_PARAMTER)
.build();
return CommandConfiguration.builder()
.name("leaderboard")
.name(LEADERBOARD_PARAMTER)
.module(ExperienceModuleDefinition.EXPERIENCE)
.templated(true)
.async(true)
.slashCommandConfig(slashCommandConfig)
.supportsEmbedException(true)
.causesReaction(true)
.parameters(parameters)

View File

@@ -4,17 +4,23 @@ import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.config.SlashCommandConfig;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.experience.config.ExperienceFeatureDefinition;
import dev.sheldan.abstracto.experience.config.ExperienceSlashCommandNames;
import dev.sheldan.abstracto.experience.model.template.LevelRole;
import dev.sheldan.abstracto.experience.model.template.LevelRolesModel;
import dev.sheldan.abstracto.experience.service.ExperienceRoleService;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -27,6 +33,9 @@ import java.util.stream.Collectors;
@Component
public class LevelRoles extends AbstractConditionableCommand {
private static final String LEVEL_ROLES_COMMAND = "levelRoles";
private static final String LEVEL_ROLES_TEMPLATE_KEY = "levelRoles_response";
@Autowired
private ExperienceRoleService experienceRoleService;
@@ -36,29 +45,58 @@ public class LevelRoles extends AbstractConditionableCommand {
@Autowired
private ChannelService channelService;
private static final String LEVEL_ROLES_TEMPLATE_KEY = "levelRoles_response";
@Autowired
private TemplateService templateService;
@Autowired
private InteractionService interactionService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
AServer server = serverManagementService.loadServer(commandContext.getGuild());
MessageToSend messageToSend = getResponseModel(commandContext.getGuild().getIdLong());
return FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel()))
.thenApply(unused -> CommandResult.fromSuccess());
}
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
Long serverId = event.getGuild().getIdLong();
MessageToSend messageToSend = getResponseModel(serverId);
return interactionService.replyMessageToSend(messageToSend, event)
.thenApply(interactionHook -> CommandResult.fromSuccess());
}
private MessageToSend getResponseModel(Long serverId) {
AServer server = serverManagementService.loadServer(serverId);
List<LevelRole> levelRoles = experienceRoleService.loadLevelRoleConfigForServer(server);
levelRoles = levelRoles.stream().sorted(Comparator.comparingInt(LevelRole::getLevel).reversed()).collect(Collectors.toList());
LevelRolesModel model = LevelRolesModel
.builder()
.levelRoles(levelRoles)
.build();
return FutureUtils.toSingleFutureGeneric(channelService.sendEmbedTemplateInTextChannelList(LEVEL_ROLES_TEMPLATE_KEY, model, commandContext.getChannel()))
.thenApply(unused -> CommandResult.fromSuccess());
return templateService.renderEmbedTemplate(LEVEL_ROLES_TEMPLATE_KEY, model, serverId);
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
HelpInfo helpInfo = HelpInfo
.builder()
.templated(true)
.build();
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.rootCommandName(ExperienceSlashCommandNames.EXPERIENCE)
.commandName(LEVEL_ROLES_COMMAND)
.build();
return CommandConfiguration.builder()
.name("levelRoles")
.name(LEVEL_ROLES_COMMAND)
.module(ExperienceModuleDefinition.EXPERIENCE)
.async(true)
.slashCommandConfig(slashCommandConfig)
.templated(true)
.supportsEmbedException(true)
.parameters(parameters)

View File

@@ -4,22 +4,28 @@ import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.config.SlashCommandConfig;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.models.FullRole;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.RoleService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.experience.config.ExperienceFeatureDefinition;
import dev.sheldan.abstracto.experience.config.ExperienceSlashCommandNames;
import dev.sheldan.abstracto.experience.model.database.ADisabledExpRole;
import dev.sheldan.abstracto.experience.model.template.DisabledExperienceRolesModel;
import dev.sheldan.abstracto.experience.service.management.DisabledExpRoleManagementService;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -34,6 +40,9 @@ import java.util.concurrent.CompletableFuture;
@Component
public class ListDisabledExperienceRoles extends AbstractConditionableCommand {
private static final String LIST_DISABLED_EXPERIENCE_ROLES_RESPONSE = "list_disabled_experience_roles";
private static final String LIST_DISABLED_EXPERIENCE_ROLES_COMMAND = "listDisabledExperienceRoles";
@Autowired
private DisabledExpRoleManagementService disabledExpRoleManagementService;
@@ -46,11 +55,35 @@ public class ListDisabledExperienceRoles extends AbstractConditionableCommand {
@Autowired
private ServerManagementService serverManagementService;
@Autowired
private TemplateService templateService;
@Autowired
private InteractionService interactionService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
AServer server = serverManagementService.loadServer(commandContext.getGuild());
Long serverId = commandContext.getGuild().getIdLong();
MessageToSend messageToSend = getResponseModel(serverId, commandContext.getAuthor());
List<CompletableFuture<Message>> futures = channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel());
return FutureUtils.toSingleFutureGeneric(futures).thenApply(aVoid -> CommandResult.fromIgnored());
}
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
Long serverId = event.getGuild().getIdLong();
MessageToSend messageToSend = getResponseModel(serverId, event.getMember());
return interactionService.replyMessageToSend(messageToSend, event)
.thenApply(interactionHook -> CommandResult.fromSuccess());
}
private MessageToSend getResponseModel(Long serverId, Member member) {
AServer server = serverManagementService.loadServer(serverId);
List<ADisabledExpRole> disabledRolesForServer = disabledExpRoleManagementService.getDisabledRolesForServer(server);
DisabledExperienceRolesModel disabledExperienceRolesModel = (DisabledExperienceRolesModel) ContextConverter.fromCommandContext(commandContext, DisabledExperienceRolesModel.class);
DisabledExperienceRolesModel disabledExperienceRolesModel = DisabledExperienceRolesModel
.builder()
.member(member)
.build();
disabledRolesForServer.forEach(aDisabledExpRole -> {
Role jdaRole = null;
if(!aDisabledExpRole.getRole().getDeleted()) {
@@ -63,18 +96,29 @@ public class ListDisabledExperienceRoles extends AbstractConditionableCommand {
.build();
disabledExperienceRolesModel.getRoles().add(role);
});
List<CompletableFuture<Message>> futures = channelService.sendEmbedTemplateInTextChannelList("list_disabled_experience_roles", disabledExperienceRolesModel, commandContext.getChannel());
return FutureUtils.toSingleFutureGeneric(futures).thenApply(aVoid -> CommandResult.fromIgnored());
return templateService.renderEmbedTemplate(LIST_DISABLED_EXPERIENCE_ROLES_RESPONSE, disabledExperienceRolesModel, serverId);
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
HelpInfo helpInfo = HelpInfo
.builder()
.templated(true)
.build();
List<String> aliases = Arrays.asList("lsDisEpRoles");
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.rootCommandName(ExperienceSlashCommandNames.EXPERIENCE_CONFIG)
.commandName(LIST_DISABLED_EXPERIENCE_ROLES_COMMAND)
.build();
return CommandConfiguration.builder()
.name("listDisabledExperienceRoles")
.name(LIST_DISABLED_EXPERIENCE_ROLES_COMMAND)
.module(ExperienceModuleDefinition.EXPERIENCE)
.slashCommandConfig(slashCommandConfig)
.templated(true)
.supportsEmbedException(true)
.causesReaction(true)

View File

@@ -4,15 +4,19 @@ import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.config.SlashCommandConfig;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.slash.parameter.SlashCommandParameterService;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.exception.EntityGuildMismatchException;
import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.experience.config.ExperienceFeatureDefinition;
import dev.sheldan.abstracto.experience.config.ExperienceSlashCommandNames;
import dev.sheldan.abstracto.experience.converter.LeaderBoardModelConverter;
import dev.sheldan.abstracto.experience.model.LeaderBoardEntry;
import dev.sheldan.abstracto.experience.model.database.AUserExperience;
@@ -25,11 +29,11 @@ import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@@ -42,6 +46,8 @@ import java.util.concurrent.CompletableFuture;
public class Rank extends AbstractConditionableCommand {
public static final String RANK_POST_EMBED_TEMPLATE = "rank_post";
public static final String RANK_COMMAND = "rank";
public static final String MEMBER_PARAMETER = "member";
@Autowired
private LeaderBoardModelConverter converter;
@@ -66,43 +72,89 @@ public class Rank extends AbstractConditionableCommand {
@Autowired
private UserExperienceManagementService userExperienceManagementService;
@Autowired
private SlashCommandParameterService slashCommandParameterService;
@Autowired
private InteractionService interactionService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
List<Object> parameters = commandContext.getParameters().getParameters();
Member parameter = !parameters.isEmpty() ? (Member) parameters.get(0) : commandContext.getAuthor();
if(!parameter.getGuild().equals(commandContext.getGuild())) {
Member targetMember = !parameters.isEmpty() ? (Member) parameters.get(0) : commandContext.getAuthor();
if(!targetMember.getGuild().equals(commandContext.getGuild())) {
throw new EntityGuildMismatchException();
}
AUserInAServer aUserInAServer = userInServerManagementService.loadOrCreateUser(parameter);
AUserInAServer aUserInAServer = userInServerManagementService.loadOrCreateUser(targetMember);
LeaderBoardEntry userRank = userExperienceService.getRankOfUserInServer(aUserInAServer);
CompletableFuture<List<LeaderBoardEntryModel>> future = converter.fromLeaderBoardEntry(Arrays.asList(userRank));
RankModel rankModel = RankModel
.builder()
.member(parameter)
.member(targetMember)
.build();
return future.thenCompose(leaderBoardEntryModel ->
self.renderAndSendRank(commandContext, parameter, rankModel, leaderBoardEntryModel.get(0))
).thenApply(result -> CommandResult.fromIgnored());
return future.thenCompose(leaderBoardEntryModel -> {
MessageToSend messageToSend = self.renderMessageToSend(targetMember, rankModel, leaderBoardEntryModel.get(0));
return FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel()));
}).thenApply(result -> CommandResult.fromIgnored());
}
@Transactional
public CompletableFuture<Void> renderAndSendRank(CommandContext commandContext, Member toRender, RankModel rankModel, LeaderBoardEntryModel leaderBoardEntryModel) {
public MessageToSend renderMessageToSend(Member toRender, RankModel rankModel, LeaderBoardEntryModel leaderBoardEntryModel) {
rankModel.setRankUser(leaderBoardEntryModel);
AUserInAServer aUserInAServer = userInServerManagementService.loadOrCreateUser(toRender);
AUserExperience experienceObj = userExperienceManagementService.findUserInServer(aUserInAServer);
log.info("Rendering rank for user {} in server {}.", toRender.getId(), commandContext.getGuild().getId());
log.info("Rendering rank for user {} in server {}.", toRender.getId(), toRender.getGuild().getId());
rankModel.setExperienceToNextLevel(experienceLevelService.calculateExperienceToNextLevel(experienceObj.getCurrentLevel().getLevel(), experienceObj.getExperience()));
MessageToSend messageToSend = templateService.renderEmbedTemplate(RANK_POST_EMBED_TEMPLATE, rankModel, commandContext.getGuild().getIdLong());
return FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel()));
return templateService.renderEmbedTemplate(RANK_POST_EMBED_TEMPLATE, rankModel, toRender.getGuild().getIdLong());
}
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
Member targetMember;
if(slashCommandParameterService.hasCommandOption(MEMBER_PARAMETER, event)) {
targetMember = slashCommandParameterService.getCommandOption(MEMBER_PARAMETER, event, Member.class);
} else {
targetMember = event.getMember();
}
AUserInAServer aUserInAServer = userInServerManagementService.loadOrCreateUser(targetMember);
LeaderBoardEntry userRank = userExperienceService.getRankOfUserInServer(aUserInAServer);
CompletableFuture<List<LeaderBoardEntryModel>> future = converter.fromLeaderBoardEntry(Arrays.asList(userRank));
RankModel rankModel = RankModel
.builder()
.member(targetMember)
.build();
return future.thenCompose(leaderBoardEntryModel -> {
MessageToSend messageToSend = self.renderMessageToSend(targetMember, rankModel, leaderBoardEntryModel.get(0));
return interactionService.replyMessageToSend(messageToSend, event);
}).thenApply(result -> CommandResult.fromIgnored());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("member").templated(true).type(Member.class).optional(true).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
Parameter memberParameter = Parameter
.builder()
.name(MEMBER_PARAMETER)
.templated(true)
.type(Member.class)
.optional(true)
.build();
List<Parameter> parameters = Arrays.asList(memberParameter);
HelpInfo helpInfo = HelpInfo
.builder()
.templated(true)
.build();
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.rootCommandName(ExperienceSlashCommandNames.EXPERIENCE)
.commandName(RANK_COMMAND)
.build();
return CommandConfiguration.builder()
.name("rank")
.name(RANK_COMMAND)
.slashCommandConfig(slashCommandConfig)
.module(ExperienceModuleDefinition.EXPERIENCE)
.templated(true)
.async(true)

View File

@@ -1,64 +0,0 @@
package dev.sheldan.abstracto.experience.command;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
import dev.sheldan.abstracto.core.test.command.CommandConfigValidator;
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
import dev.sheldan.abstracto.experience.service.management.DisabledExpRoleManagementService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
@Slf4j
public class DisableExpForRoleTest {
@InjectMocks
private DisableExpForRole testUnit;
@Mock
private RoleManagementService roleManagementService;
@Mock
private DisabledExpRoleManagementService disabledExpRoleManagementService;
@Test
public void testExecuteCommandForNotDisabledRole() {
executeDisableExpForRoleTest(false, 1);
}
@Test
public void testExecuteCommandForDisabledRole() {
executeDisableExpForRoleTest(true, 0);
}
private void executeDisableExpForRoleTest(boolean value, int wantedNumberOfInvocations) {
ARole parameterRole = Mockito.mock(ARole.class);
ARole actualRole = Mockito.mock(ARole.class);
when(parameterRole.getId()).thenReturn(5L);
CommandContext context = CommandTestUtilities.getWithParameters(Arrays.asList(parameterRole));
AServer server = Mockito.mock(AServer.class);
when(actualRole.getServer()).thenReturn(server);
when(roleManagementService.findRole(parameterRole.getId())).thenReturn(actualRole);
when(disabledExpRoleManagementService.isExperienceDisabledForRole(actualRole)).thenReturn(value);
CommandResult result = testUnit.execute(context);
verify(disabledExpRoleManagementService, times(wantedNumberOfInvocations)).setRoleToBeDisabledForExp(actualRole);
CommandTestUtilities.checkSuccessfulCompletion(result);
}
@Test
public void validateCommand() {
CommandConfigValidator.validateCommandConfiguration(testUnit.getConfiguration());
}
}

View File

@@ -1,93 +0,0 @@
package dev.sheldan.abstracto.experience.command;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.core.test.command.CommandConfigValidator;
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
import dev.sheldan.abstracto.experience.converter.LeaderBoardModelConverter;
import dev.sheldan.abstracto.experience.model.LeaderBoard;
import dev.sheldan.abstracto.experience.model.LeaderBoardEntry;
import dev.sheldan.abstracto.experience.model.template.LeaderBoardEntryModel;
import dev.sheldan.abstracto.experience.model.template.LeaderBoardModel;
import dev.sheldan.abstracto.experience.service.AUserExperienceService;
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class LeaderBoardCommandTest {
@InjectMocks
private LeaderBoardCommand testUnit;
@Mock
private AUserExperienceService userExperienceService;
@Mock
private TemplateService templateService;
@Mock
private ChannelService channelService;
@Mock
private LeaderBoardModelConverter converter;
@Mock
private UserInServerManagementService userInServerManagementService;
@Mock
private ServerManagementService serverManagementService;
private static final Long SERVER_ID = 45L;
@Test
public void testLeaderBoardWithNoParameter() {
testLeaderBoardCommand(CommandTestUtilities.getNoParameters(), 1);
}
@Test
public void testLeaderBoardWithPageParameter() {
testLeaderBoardCommand(CommandTestUtilities.getWithParameters(Arrays.asList(5)), 5);
}
private void testLeaderBoardCommand(CommandContext context, int expectedPage) {
when(context.getGuild().getIdLong()).thenReturn(SERVER_ID);
LeaderBoard leaderBoard = Mockito.mock(LeaderBoard.class);
AServer server = Mockito.mock(AServer.class);
when(serverManagementService.loadServer(context.getGuild())).thenReturn(server);
AUserInAServer userInAServer = Mockito.mock(AUserInAServer.class);
when(userInServerManagementService.loadOrCreateUser(context.getAuthor())).thenReturn(userInAServer);
when(userExperienceService.findLeaderBoardData(server, expectedPage)).thenReturn(leaderBoard);
when(converter.fromLeaderBoard(leaderBoard)).thenReturn(CompletableFuture.completedFuture(null));
LeaderBoardEntry executingUserRank = Mockito.mock(LeaderBoardEntry.class);
when(userExperienceService.getRankOfUserInServer(userInAServer)).thenReturn(executingUserRank);
LeaderBoardEntryModel leaderBoardEntryModel = Mockito.mock(LeaderBoardEntryModel.class);
when(converter.fromLeaderBoardEntry(Arrays.asList(executingUserRank))).thenReturn(CompletableFuture.completedFuture(Arrays.asList(leaderBoardEntryModel)));
MessageToSend messageToSend = Mockito.mock(MessageToSend.class);
when(templateService.renderEmbedTemplate(eq(LeaderBoardCommand.LEADER_BOARD_POST_EMBED_TEMPLATE), any(LeaderBoardModel.class), eq(SERVER_ID))).thenReturn(messageToSend);
CompletableFuture<CommandResult> result = testUnit.executeAsync(context);
verify(channelService, times(1)).sendMessageToSendToChannel(messageToSend, context.getChannel());
CommandTestUtilities.checkSuccessfulCompletionAsync(result);
}
@Test
public void validateCommand() {
CommandConfigValidator.validateCommandConfiguration(testUnit.getConfiguration());
}
}

View File

@@ -1,90 +0,0 @@
package dev.sheldan.abstracto.experience.command;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.RoleService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.test.command.CommandConfigValidator;
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
import dev.sheldan.abstracto.experience.model.database.ADisabledExpRole;
import dev.sheldan.abstracto.experience.model.template.DisabledExperienceRolesModel;
import dev.sheldan.abstracto.experience.service.management.DisabledExpRoleManagementService;
import net.dv8tion.jda.api.entities.Role;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class ListDisabledExperienceRolesTest {
@InjectMocks
private ListDisabledExperienceRoles testUnit;
@Mock
private DisabledExpRoleManagementService disabledExpRoleManagementService;
@Mock
private RoleService roleService;
@Mock
private ChannelService channelService;
@Mock
private ServerManagementService serverManagementService;
@Mock
private AServer server;
@Test
public void testCommandExecutionNoRolesFound() {
CommandContext context = CommandTestUtilities.getNoParameters();
when(serverManagementService.loadServer(context.getGuild())).thenReturn(server);
when(disabledExpRoleManagementService.getDisabledRolesForServer(server)).thenReturn(new ArrayList<>());
when(channelService.sendEmbedTemplateInTextChannelList(eq("list_disabled_experience_roles"),
any(DisabledExperienceRolesModel.class), eq(context.getChannel()))).thenReturn(CommandTestUtilities.messageFutureList());
CompletableFuture<CommandResult> result = testUnit.executeAsync(context);
CommandTestUtilities.checkSuccessfulCompletionAsync(result);
verify(roleService, times(0)).getRoleFromGuild(any(ARole.class));
}
@Test
public void testCommandExecutionRolesFound() {
CommandContext context = CommandTestUtilities.getNoParameters();
ADisabledExpRole disabledExpRole1 = Mockito.mock(ADisabledExpRole.class);
ADisabledExpRole disabledExpRole2 = Mockito.mock(ADisabledExpRole.class);
when(disabledExpRoleManagementService.getDisabledRolesForServer(server)).thenReturn(Arrays.asList(disabledExpRole1, disabledExpRole2));
Role role1 = Mockito.mock(Role.class);
Role role2 = Mockito.mock(Role.class);
ARole aRole = Mockito.mock(ARole.class);
ARole aRole2 = Mockito.mock(ARole.class);
when(roleService.getRoleFromGuild(aRole)).thenReturn(role1);
when(roleService.getRoleFromGuild(aRole2)).thenReturn(role2);
when(aRole.getDeleted()).thenReturn(false);
when(aRole2.getDeleted()).thenReturn(false);
when(disabledExpRole1.getRole()).thenReturn(aRole);
when(disabledExpRole2.getRole()).thenReturn(aRole2);
when(serverManagementService.loadServer(context.getGuild())).thenReturn(server);
when(channelService.sendEmbedTemplateInTextChannelList(eq("list_disabled_experience_roles"),
any(DisabledExperienceRolesModel.class), eq(context.getChannel()))).thenReturn(CommandTestUtilities.messageFutureList());
CompletableFuture<CommandResult> result = testUnit.executeAsync(context);
CommandTestUtilities.checkSuccessfulCompletionAsync(result);
}
@Test
public void validateCommand() {
CommandConfigValidator.validateCommandConfiguration(testUnit.getConfiguration());
}
}

View File

@@ -1,110 +0,0 @@
package dev.sheldan.abstracto.experience.command;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.core.test.command.CommandConfigValidator;
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
import dev.sheldan.abstracto.experience.converter.LeaderBoardModelConverter;
import dev.sheldan.abstracto.experience.model.LeaderBoardEntry;
import dev.sheldan.abstracto.experience.model.database.AExperienceLevel;
import dev.sheldan.abstracto.experience.model.database.AUserExperience;
import dev.sheldan.abstracto.experience.model.template.LeaderBoardEntryModel;
import dev.sheldan.abstracto.experience.model.template.RankModel;
import dev.sheldan.abstracto.experience.service.AUserExperienceService;
import dev.sheldan.abstracto.experience.service.ExperienceLevelService;
import dev.sheldan.abstracto.experience.service.management.UserExperienceManagementService;
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import net.dv8tion.jda.api.entities.Member;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import static dev.sheldan.abstracto.experience.command.Rank.RANK_POST_EMBED_TEMPLATE;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class RankTest {
@InjectMocks
private Rank testUnit;
@Mock
private LeaderBoardModelConverter converter;
@Mock
private TemplateService templateService;
@Mock
private AUserExperienceService userExperienceService;
@Mock
private ExperienceLevelService experienceLevelService;
@Mock
private ChannelService channelService;
@Mock
private UserInServerManagementService userInServerManagementService;
@Mock
private Rank self;
private static final Long SERVER_ID = 4L;
@Mock
private AUserInAServer aUserInAServer;
@Mock
private UserExperienceManagementService userExperienceManagementService;
@Test
public void testRankExecution() {
CommandContext context = CommandTestUtilities.getNoParameters();
LeaderBoardEntry leaderBoardEntry = Mockito.mock(LeaderBoardEntry.class);
when(userInServerManagementService.loadOrCreateUser(context.getAuthor())).thenReturn(aUserInAServer);
when(userExperienceService.getRankOfUserInServer(aUserInAServer)).thenReturn(leaderBoardEntry);
LeaderBoardEntryModel leaderBoardEntryModel = Mockito.mock(LeaderBoardEntryModel.class);
when(context.getAuthor().getGuild()).thenReturn(context.getGuild());
when(converter.fromLeaderBoardEntry(Arrays.asList(leaderBoardEntry))).thenReturn(CompletableFuture.completedFuture(Arrays.asList(leaderBoardEntryModel)));
when(self.renderAndSendRank(eq(context), any(Member.class), any(RankModel.class), eq(leaderBoardEntryModel))).thenReturn(CompletableFuture.completedFuture(null));
CompletableFuture<CommandResult> result = testUnit.executeAsync(context);
CommandTestUtilities.checkSuccessfulCompletionAsync(result);
}
@Test
public void testRenderAndSend() {
int currentLevelValue = 50;
long currentExperience = 50L;
AExperienceLevel currentLevel = Mockito.mock(AExperienceLevel.class);
when(currentLevel.getLevel()).thenReturn(currentLevelValue);
AUserExperience aUserExperience = Mockito.mock(AUserExperience.class);
when(aUserExperience.getCurrentLevel()).thenReturn(currentLevel);
when(aUserExperience.getExperience()).thenReturn(currentExperience);
CommandContext context = CommandTestUtilities.getNoParameters();
when(context.getGuild().getIdLong()).thenReturn(SERVER_ID);
RankModel rankModel = Mockito.mock(RankModel.class);
LeaderBoardEntryModel leaderBoardEntryModel = Mockito.mock(LeaderBoardEntryModel.class);
when(userInServerManagementService.loadOrCreateUser(context.getAuthor())).thenReturn(aUserInAServer);
when(userExperienceManagementService.findUserInServer(aUserInAServer)).thenReturn(aUserExperience);
when(experienceLevelService.calculateExperienceToNextLevel(currentLevelValue, currentExperience)).thenReturn(140L);
MessageToSend messageToSend = Mockito.mock(MessageToSend.class);
when(templateService.renderEmbedTemplate(RANK_POST_EMBED_TEMPLATE, rankModel, SERVER_ID)).thenReturn(messageToSend);
when(channelService.sendMessageToSendToChannel(messageToSend, context.getChannel())).thenReturn(Arrays.asList(CompletableFuture.completedFuture(null)));
testUnit.renderAndSendRank(context, context.getAuthor(), rankModel, leaderBoardEntryModel).join();
}
@Test
public void validateCommand() {
CommandConfigValidator.validateCommandConfiguration(testUnit.getConfiguration());
}
}