allowed the Arole parameter type to be either an id or the role mention

fixed adding/removing allowed roles, the compare did not work
changed a few commands to use the Arole parameter
This commit is contained in:
Sheldan
2020-04-28 22:12:29 +02:00
parent 6ed3a133e2
commit 85c47db5ed
18 changed files with 36 additions and 33 deletions

View File

@@ -53,7 +53,7 @@ public class SetExpRole extends AbstractConditionableCommand {
public CommandConfiguration getConfiguration() { public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>(); List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("level").type(Integer.class).build()); parameters.add(Parameter.builder().name("level").type(Integer.class).build());
parameters.add(Parameter.builder().name("roleId").type(Long.class).build()); parameters.add(Parameter.builder().name("role").type(ARole.class).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build(); HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder() return CommandConfiguration.builder()
.name("setExpRole") .name("setExpRole")

View File

@@ -28,8 +28,7 @@ public class UnSetExpRole extends AbstractConditionableCommand {
@Override @Override
public CommandResult execute(CommandContext commandContext) { public CommandResult execute(CommandContext commandContext) {
Long roleId = (Long) commandContext.getParameters().getParameters().get(0); ARole role = (ARole) commandContext.getParameters().getParameters().get(0);
ARole role = roleManagementService.findRole(roleId);
// do not check for the existence of the role, because if the role was deleted, users should be able // do not check for the existence of the role, because if the role was deleted, users should be able
// to get rid of it in the configuration // to get rid of it in the configuration
experienceRoleService.unsetRole(role, commandContext.getUserInitiatedContext().getServer(), commandContext.getUserInitiatedContext().getChannel()); experienceRoleService.unsetRole(role, commandContext.getUserInitiatedContext().getServer(), commandContext.getUserInitiatedContext().getChannel());
@@ -39,7 +38,7 @@ public class UnSetExpRole extends AbstractConditionableCommand {
@Override @Override
public CommandConfiguration getConfiguration() { public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>(); List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("roleId").type(Long.class).build()); parameters.add(Parameter.builder().name("role").type(ARole.class).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build(); HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder() return CommandConfiguration.builder()
.name("unSetExpRole") .name("unSetExpRole")

View File

@@ -23,12 +23,10 @@ import dev.sheldan.abstracto.core.models.context.UserInitiatedServerContext;
import dev.sheldan.abstracto.core.models.database.AUserInAServer; import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.utils.ParseUtils; import dev.sheldan.abstracto.core.utils.ParseUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Emote; import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
@@ -157,6 +155,7 @@ public class CommandReceivedHandler extends ListenerAdapter {
Iterator<TextChannel> channelIterator = message.getMentionedChannels().iterator(); Iterator<TextChannel> channelIterator = message.getMentionedChannels().iterator();
Iterator<Emote> emoteIterator = message.getEmotes().iterator(); Iterator<Emote> emoteIterator = message.getEmotes().iterator();
Iterator<Member> memberIterator = message.getMentionedMembers().iterator(); Iterator<Member> memberIterator = message.getMentionedMembers().iterator();
Iterator<Role> roleIterator = message.getMentionedRoles().iterator();
Parameter param = command.getConfiguration().getParameters().get(0); Parameter param = command.getConfiguration().getParameters().get(0);
boolean reminderActive = false; boolean reminderActive = false;
for (int i = 0; i < unParsedCommandParameter.getParameters().size(); i++) { for (int i = 0; i < unParsedCommandParameter.getParameters().size(); i++) {
@@ -189,7 +188,11 @@ public class CommandReceivedHandler extends ListenerAdapter {
parsedParameters.add(value); parsedParameters.add(value);
} }
} else if(param.getType().equals(ARole.class)) { } else if(param.getType().equals(ARole.class)) {
parsedParameters.add(roleManagementService.findRole(Long.parseLong(value))); if(StringUtils.isNumeric(value)) {
parsedParameters.add(roleManagementService.findRole(Long.parseLong(value)));
} else {
parsedParameters.add(roleManagementService.findRole(roleIterator.next().getIdLong()));
}
} else if(param.getType().equals(Boolean.class)) { } else if(param.getType().equals(Boolean.class)) {
parsedParameters.add(Boolean.valueOf(value)); parsedParameters.add(Boolean.valueOf(value));
} else if (param.getType().equals(Duration.class)) { } else if (param.getType().equals(Duration.class)) {

View File

@@ -44,7 +44,7 @@ public class CommandServiceBean implements CommandService {
@Override @Override
public void allowCommandForRole(ACommand aCommand, ARole role) { public void allowCommandForRole(ACommand aCommand, ARole role) {
ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, role.getServer()); ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, role.getServer());
if(!commandForServer.getAllowedRoles().contains(role)) { if(commandForServer.getAllowedRoles().stream().noneMatch(role1 -> role1.getId().equals(role.getId()))) {
commandForServer.getAllowedRoles().add(role); commandForServer.getAllowedRoles().add(role);
} }
commandForServer.setRestricted(true); commandForServer.setRestricted(true);
@@ -53,7 +53,7 @@ public class CommandServiceBean implements CommandService {
@Override @Override
public void makeRoleImmuneForCommand(ACommand aCommand, ARole role) { public void makeRoleImmuneForCommand(ACommand aCommand, ARole role) {
ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, role.getServer()); ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, role.getServer());
if(!commandForServer.getImmuneRoles().contains(role)) { if(commandForServer.getImmuneRoles().stream().noneMatch(role1 -> role1.getId().equals(role.getId()))) {
commandForServer.getImmuneRoles().add(role); commandForServer.getImmuneRoles().add(role);
} }
} }
@@ -61,7 +61,7 @@ public class CommandServiceBean implements CommandService {
@Override @Override
public void makeRoleAffectedByCommand(ACommand aCommand, ARole role) { public void makeRoleAffectedByCommand(ACommand aCommand, ARole role) {
ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, role.getServer()); ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, role.getServer());
commandForServer.getImmuneRoles().remove(role); commandForServer.getImmuneRoles().removeIf(role1 -> role1.getId().equals(role.getId()));
} }
@Override @Override
@@ -80,7 +80,7 @@ public class CommandServiceBean implements CommandService {
public void disAllowCommandForRole(ACommand aCommand, ARole role) { public void disAllowCommandForRole(ACommand aCommand, ARole role) {
ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, role.getServer()); ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, role.getServer());
commandForServer.setRestricted(true); commandForServer.setRestricted(true);
commandForServer.getAllowedRoles().remove(role); commandForServer.getAllowedRoles().removeIf(role1 -> role1.getId().equals(role.getId()));
} }

View File

@@ -40,8 +40,7 @@ public class AllowRole extends AbstractConditionableCommand {
@Override @Override
public CommandResult execute(CommandContext commandContext) { public CommandResult execute(CommandContext commandContext) {
String name = (String) commandContext.getParameters().getParameters().get(0); String name = (String) commandContext.getParameters().getParameters().get(0);
Long roleId = (Long) commandContext.getParameters().getParameters().get(1); ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
ARole role = roleManagementService.findRole(roleId);
if(featureManagementService.featureExists(name)) { if(featureManagementService.featureExists(name)) {
AFeature feature = featureManagementService.getFeature(name); AFeature feature = featureManagementService.getFeature(name);
feature.getCommands().forEach(command -> feature.getCommands().forEach(command ->
@@ -59,7 +58,7 @@ public class AllowRole extends AbstractConditionableCommand {
@Override @Override
public CommandConfiguration getConfiguration() { public CommandConfiguration getConfiguration() {
Parameter featureName = Parameter.builder().name("feature|commandName").type(String.class).description("The command/feature the role should be able to execute.").build(); Parameter featureName = Parameter.builder().name("feature|commandName").type(String.class).description("The command/feature the role should be able to execute.").build();
Parameter role = Parameter.builder().name("roleId").type(Long.class).description("The roleId to allow it for.").build(); Parameter role = Parameter.builder().name("role").type(ARole.class).description("The role to allow it for.").build();
List<Parameter> parameters = Arrays.asList(featureName, role); List<Parameter> parameters = Arrays.asList(featureName, role);
HelpInfo helpInfo = HelpInfo.builder().templated(true).build(); HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder() return CommandConfiguration.builder()

View File

@@ -40,8 +40,7 @@ public class DisAllowRole extends AbstractConditionableCommand {
@Override @Override
public CommandResult execute(CommandContext commandContext) { public CommandResult execute(CommandContext commandContext) {
String name = (String) commandContext.getParameters().getParameters().get(0); String name = (String) commandContext.getParameters().getParameters().get(0);
Long roleId = (Long) commandContext.getParameters().getParameters().get(1); ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
ARole role = roleManagementService.findRole(roleId);
if(featureManagementService.featureExists(name)) { if(featureManagementService.featureExists(name)) {
AFeature feature = featureManagementService.getFeature(name); AFeature feature = featureManagementService.getFeature(name);
feature.getCommands().forEach(command -> feature.getCommands().forEach(command ->
@@ -59,7 +58,7 @@ public class DisAllowRole extends AbstractConditionableCommand {
@Override @Override
public CommandConfiguration getConfiguration() { public CommandConfiguration getConfiguration() {
Parameter featureName = Parameter.builder().name("feature|commandName").type(String.class).description("The command/feature the role should not be able to execute.").build(); Parameter featureName = Parameter.builder().name("feature|commandName").type(String.class).description("The command/feature the role should not be able to execute.").build();
Parameter role = Parameter.builder().name("roleId").type(Long.class).description("The roleId to disallow it for.").build(); Parameter role = Parameter.builder().name("role").type(ARole.class).description("The roleId to disallow it for.").build();
List<Parameter> parameters = Arrays.asList(featureName, role); List<Parameter> parameters = Arrays.asList(featureName, role);
HelpInfo helpInfo = HelpInfo.builder().templated(true).build(); HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder() return CommandConfiguration.builder()

View File

@@ -59,7 +59,7 @@ public class MakeAffected extends AbstractConditionableCommand {
@Override @Override
public CommandConfiguration getConfiguration() { public CommandConfiguration getConfiguration() {
Parameter featureName = Parameter.builder().name("feature|commandName").type(String.class).description("The command/feature name to make the role affected by.").build(); Parameter featureName = Parameter.builder().name("feature|commandName").type(String.class).description("The command/feature name to make the role affected by.").build();
Parameter role = Parameter.builder().name("roleId").type(Long.class).description("The roleId to make affected.").build(); Parameter role = Parameter.builder().name("role").type(ARole.class).description("The role to make affected.").build();
List<Parameter> parameters = Arrays.asList(featureName, role); List<Parameter> parameters = Arrays.asList(featureName, role);
HelpInfo helpInfo = HelpInfo.builder().templated(true).build(); HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder() return CommandConfiguration.builder()

View File

@@ -40,8 +40,7 @@ public class MakeImmune extends AbstractConditionableCommand {
@Override @Override
public CommandResult execute(CommandContext commandContext) { public CommandResult execute(CommandContext commandContext) {
String name = (String) commandContext.getParameters().getParameters().get(0); String name = (String) commandContext.getParameters().getParameters().get(0);
Long roleId = (Long) commandContext.getParameters().getParameters().get(1); ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
ARole role = roleManagementService.findRole(roleId);
if(featureManagementService.featureExists(name)) { if(featureManagementService.featureExists(name)) {
AFeature feature = featureManagementService.getFeature(name); AFeature feature = featureManagementService.getFeature(name);
feature.getCommands().forEach(command -> feature.getCommands().forEach(command ->
@@ -59,7 +58,7 @@ public class MakeImmune extends AbstractConditionableCommand {
@Override @Override
public CommandConfiguration getConfiguration() { public CommandConfiguration getConfiguration() {
Parameter featureName = Parameter.builder().name("feature|commandName").type(String.class).description("The command/feature name to make the role immune for.").build(); Parameter featureName = Parameter.builder().name("feature|commandName").type(String.class).description("The command/feature name to make the role immune for.").build();
Parameter role = Parameter.builder().name("roleId").type(Long.class).description("The roleId to make immune.").build(); Parameter role = Parameter.builder().name("role").type(ARole.class).description("The roleId to make immune.").build();
List<Parameter> parameters = Arrays.asList(featureName, role); List<Parameter> parameters = Arrays.asList(featureName, role);
HelpInfo helpInfo = HelpInfo.builder().templated(true).build(); HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder() return CommandConfiguration.builder()

View File

@@ -1,3 +1,4 @@
Allows the role to execute all commands in the given feature or the command directly. Allows the role to execute all commands in the given feature or the command directly.
In case the names coincide, the the feature will be taken first. The restriction of commands to certain roles is only in effect, if the command is restricted (see `restrict`). In case the names coincide, the the feature will be taken first. The restriction of commands to certain roles is only in effect, if the command is restricted (see `restrict`).
When executing this command, the affected commands are automatically set to restricted. When executing this command, the affected commands are automatically set to restricted.
The provided role can either be a role ID or a the mention of the role.

View File

@@ -1 +1 @@
allowRole <command/feature> <roleId> allowRole <command/feature> <role>

View File

@@ -1,4 +1,5 @@
Forbids the role to execute all commands in the given feature or the command directly. Forbids the role to execute all commands in the given feature or the command directly.
Does nothing in case the role was not allowed in the first place. Does nothing in case the role was not allowed in the first place.
In case the names coincide, the the feature will be taken first. The restriction of commands to certain roles is only in effect, if the command is restricted (see `restrict`). In case the names coincide, the the feature will be taken first. The restriction of commands to certain roles is only in effect, if the command is restricted (see `restrict`).
When executing this command, the affected commands are automatically set to restricted. When executing this command, the affected commands are automatically set to restricted.
The provided role can either be a role ID or a the mention of the role.

View File

@@ -1 +1 @@
disAllowRole <command/feature> <roleId> disAllowRole <command/feature> <role>

View File

@@ -1,4 +1,5 @@
Makes the given role affected by certain commands like `ban`, `warn` or `mute`. Makes the given role affected by certain commands like `ban`, `warn` or `mute`.
When a feature name is used, all commands of this feature will be changed. When a feature name is used, all commands of this feature will be changed.
Not all commands actually support this functionality. Not all commands actually support this functionality.
By default all roles are affected by the commands. By default all roles are affected by the commands.
The provided role can either be a role ID or a the mention of the role.

View File

@@ -1 +1 @@
makeAffected <feature/command> roleId makeAffected <feature/command> <role>

View File

@@ -1,4 +1,5 @@
Makes the given role immune to certain commands like `ban`, `warn` or `mute`. Makes the given role immune to certain commands like `ban`, `warn` or `mute`.
When a feature name is used, all commands of this feature will be changed. When a feature name is used, all commands of this feature will be changed.
Not all commands actually support this functionality. Not all commands actually support this functionality.
By default all roles are affected by the commands. By default all roles are affected by the commands.
The provided role can either be a role ID or a the mention of the role.

View File

@@ -1 +1 @@
makeImmune <feature/command> roleId makeImmune <feature/command> <role>