[AB-271] limiting certain arguments for commands to require the same server

This commit is contained in:
Sheldan
2021-06-27 17:07:01 +02:00
parent e655adf95e
commit 5a35137132
31 changed files with 133 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ import dev.sheldan.abstracto.core.command.config.features.CoreFeatureDefinition;
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.exception.EntityGuildMismatchException;
import dev.sheldan.abstracto.core.service.ChannelGroupService;
import net.dv8tion.jda.api.entities.TextChannel;
import org.springframework.beans.factory.annotation.Autowired;
@@ -27,6 +28,9 @@ public class AddToChannelGroup extends AbstractConditionableCommand {
public CommandResult execute(CommandContext commandContext) {
String name = (String) commandContext.getParameters().getParameters().get(0);
TextChannel channel = (TextChannel) commandContext.getParameters().getParameters().get(1);
if(!channel.getGuild().equals(commandContext.getGuild())) {
throw new EntityGuildMismatchException();
}
channelGroupService.addChannelToChannelGroup(name, channel);
return CommandResult.fromSuccess();
}

View File

@@ -9,6 +9,7 @@ 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.exception.EntityGuildMismatchException;
import dev.sheldan.abstracto.core.exception.PostTargetNotValidException;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.PostTarget;
@@ -80,6 +81,9 @@ public class PostTargetCommand extends AbstractConditionableCommand {
throw new PostTargetNotValidException(targetName, postTargetService.getAvailablePostTargets());
}
GuildChannel channel = (GuildChannel) commandContext.getParameters().getParameters().get(1);
if(!channel.getGuild().equals(commandContext.getGuild())) {
throw new EntityGuildMismatchException();
}
Guild guild = channel.getGuild();
postTargetManagement.createOrUpdate(targetName, guild.getIdLong(), channel.getIdLong());
return CompletableFuture.completedFuture(CommandResult.fromSuccess());

View File

@@ -8,6 +8,7 @@ import dev.sheldan.abstracto.core.command.config.features.CoreFeatureDefinition;
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.exception.EntityGuildMismatchException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.service.ChannelGroupService;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
@@ -31,6 +32,9 @@ public class RemoveFromChannelGroup extends AbstractConditionableCommand {
String name = (String) commandContext.getParameters().getParameters().get(0);
AChannel fakeChannel = (AChannel) commandContext.getParameters().getParameters().get(1);
AChannel actualChannel = channelManagementService.loadChannel(fakeChannel.getId());
if(!actualChannel.getServer().getId().equals(commandContext.getGuild().getIdLong())) {
throw new EntityGuildMismatchException();
}
channelGroupService.removeChannelFromChannelGroup(name, actualChannel);
return CommandResult.fromSuccess();
}

View File

@@ -14,6 +14,7 @@ import dev.sheldan.abstracto.core.command.service.management.CommandManagementSe
import dev.sheldan.abstracto.core.command.service.management.FeatureManagementService;
import dev.sheldan.abstracto.core.commands.config.ConfigModuleDefinition;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.exception.EntityGuildMismatchException;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.service.FeatureConfigService;
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
@@ -50,6 +51,9 @@ public class AllowRole extends AbstractConditionableCommand {
String name = (String) commandContext.getParameters().getParameters().get(0);
ARole fakeRole = (ARole) commandContext.getParameters().getParameters().get(1);
ARole actualRole = roleManagementService.findRole(fakeRole.getId());
if(!actualRole.getServer().getId().equals(commandContext.getGuild().getIdLong())) {
throw new EntityGuildMismatchException();
}
if(featureManagementService.featureExists(name)) {
FeatureDefinition featureDefinition = featureFlagService.getFeatureEnum(name);
commandService.allowFeatureForRole(featureDefinition, actualRole);

View File

@@ -14,6 +14,7 @@ import dev.sheldan.abstracto.core.command.service.management.CommandManagementSe
import dev.sheldan.abstracto.core.command.service.management.FeatureManagementService;
import dev.sheldan.abstracto.core.commands.config.ConfigModuleDefinition;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.exception.EntityGuildMismatchException;
import dev.sheldan.abstracto.core.models.database.AFeature;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
@@ -47,6 +48,9 @@ public class DisAllowRole extends AbstractConditionableCommand {
String name = (String) commandContext.getParameters().getParameters().get(0);
ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
ARole actualRole = roleManagementService.findRole(role.getId());
if(!actualRole.getServer().getId().equals(commandContext.getGuild().getIdLong())) {
throw new EntityGuildMismatchException();
}
if(featureManagementService.featureExists(name)) {
AFeature feature = featureManagementService.getFeature(name);
feature.getCommands().forEach(command ->

View File

@@ -9,6 +9,7 @@ import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.commands.config.ConfigModuleDefinition;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.exception.EntityGuildMismatchException;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.service.RoleImmunityService;
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
@@ -32,6 +33,9 @@ public class MakeAffected extends AbstractConditionableCommand {
String name = (String) commandContext.getParameters().getParameters().get(0);
ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
ARole actualRole = roleManagementService.findRole(role.getId());
if(!actualRole.getServer().getId().equals(commandContext.getGuild().getIdLong())) {
throw new EntityGuildMismatchException();
}
roleImmunityService.makeRoleAffected(actualRole, name);
return CommandResult.fromSuccess();
}

View File

@@ -9,6 +9,7 @@ import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.commands.config.ConfigModuleDefinition;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.exception.EntityGuildMismatchException;
import dev.sheldan.abstracto.core.service.RoleImmunityService;
import net.dv8tion.jda.api.entities.Role;
import org.springframework.beans.factory.annotation.Autowired;
@@ -27,6 +28,9 @@ public class MakeImmune extends AbstractConditionableCommand {
public CommandResult execute(CommandContext commandContext) {
String name = (String) commandContext.getParameters().getParameters().get(0);
Role role = (Role) commandContext.getParameters().getParameters().get(1);
if(!role.getGuild().equals(commandContext.getGuild())) {
throw new EntityGuildMismatchException();
}
roleImmunityService.makeRoleImmune(role, name);
return CommandResult.fromSuccess();
}

View File

@@ -0,0 +1,13 @@
package dev.sheldan.abstracto.core.exception;
public class EntityGuildMismatchException extends AbstractoTemplatableException {
@Override
public String getTemplateName() {
return "entity_guild_mismatch_exception";
}
@Override
public Object getTemplateModel() {
return new Object();
}
}