[AB-124] adding admin mode

This commit is contained in:
Sheldan
2021-03-10 01:10:27 +01:00
parent e780b0e75c
commit e2da800d84
12 changed files with 183 additions and 3 deletions

View File

@@ -0,0 +1,51 @@
package dev.sheldan.abstracto.core.commands.config;
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.features.CoreFeatures;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.service.ServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
public class SetAdminMode extends AbstractConditionableCommand {
@Autowired
private ServerService serverService;
@Override
public CommandResult execute(CommandContext commandContext) {
Boolean newState = (Boolean) commandContext.getParameters().getParameters().get(0);
serverService.setAdminModeTo(commandContext.getGuild().getIdLong(), newState);
return CommandResult.fromSuccess();
}
@Override
public CommandConfiguration getConfiguration() {
Parameter valueToSet = Parameter.builder().name("value").type(Boolean.class).templated(true).build();
List<Parameter> parameters = Arrays.asList(valueToSet);
HelpInfo helpInfo = HelpInfo.builder().templated(true).hasExample(true).build();
return CommandConfiguration.builder()
.name("setAdminMode")
.module(ConfigModuleInterface.CONFIG)
.parameters(parameters)
.templated(true)
.supportsEmbedException(true)
.help(helpInfo)
.causesReaction(true)
.build();
}
@Override
public FeatureEnum getFeature() {
return CoreFeatures.CORE_FEATURE;
}
}

View File

@@ -390,7 +390,7 @@ public class ChannelServiceBean implements ChannelService {
public AChannel getFakeChannelFromTextChannel(TextChannel textChannel) {
AServer server = AServer
.builder()
.id(textChannel.getIdLong())
.id(textChannel.getGuild().getIdLong())
.fake(true)
.build();
return AChannel

View File

@@ -0,0 +1,50 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import net.dv8tion.jda.api.entities.Guild;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ServerServiceBean implements ServerService {
@Autowired
private ServerManagementService serverManagementService;
@Override
public boolean adminModeActive(Long id) {
return serverManagementService.loadServer(id).getAdminMode();
}
@Override
public boolean adminModeActive(Guild guild) {
return adminModeActive(guild.getIdLong());
}
@Override
public void setAdminModeTo(Long id, Boolean newState) {
AServer server = serverManagementService.loadServer(id);
server.setAdminMode(newState);
}
@Override
public void setAdminModeTo(Guild guild, Boolean newState) {
setAdminModeTo(guild.getIdLong(), newState);
}
@Override
public void setAdminModeTo(AServer server, Boolean newState) {
server.setAdminMode(newState);
}
@Override
public void activateAdminMode(Long id) {
setAdminModeTo(id, true);
}
@Override
public void deactivateAdminMode(Long id) {
setAdminModeTo(id, false);
}
}

View File

@@ -26,7 +26,7 @@ public class ServerManagementServiceBean implements ServerManagementService {
@Override
public AServer createServer(Long id) {
AServer newServer = AServer.builder().id(id).build();
AServer newServer = AServer.builder().id(id).adminMode(false).build();
log.info("Creating server with id {}.", id);
return repository.save(newServer);
}

View File

@@ -97,6 +97,12 @@
<column name="feature_id" valueComputed="${coreFeature}"/>
<column name="created" valueComputed="${today}"/>
</insert>
<insert tableName="command">
<column name="name" value="setAdminMode"/>
<column name="module_id" valueComputed="${configModule}"/>
<column name="feature_id" valueComputed="${coreFeature}"/>
<column name="created" valueComputed="${today}"/>
</insert>
<insert tableName="command">
<column name="name" value="resetConfig"/>
<column name="module_id" valueComputed="${configModule}"/>

View File

@@ -14,6 +14,9 @@
<column name="created" type="TIMESTAMP WITHOUT TIME ZONE">
<constraints nullable="true"/>
</column>
<column name="admin_only" type="BOOLEAN">
<constraints nullable="false"/>
</column>
<column name="updated" type="TIMESTAMP WITHOUT TIME ZONE"/>
</createTable>
<sql>

View File

@@ -29,10 +29,13 @@ public abstract class AbstractConditionableCommand implements ConditionalCommand
@Autowired
private FeatureModeCondition featureModeCondition;
@Autowired
private AdminModeCondition adminModeCondition;
@Override
public List<CommandCondition> getConditions() {
return new ArrayList<>(Arrays.asList(featureEnabledCondition, commandDisabledCondition, commandDisallowedCondition, featureModeCondition));
return new ArrayList<>(Arrays.asList(adminModeCondition, featureEnabledCondition, commandDisabledCondition, commandDisallowedCondition, featureModeCondition));
}
protected void checkParameters(CommandContext context) {

View File

@@ -0,0 +1,31 @@
package dev.sheldan.abstracto.core.command.condition;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.condition.detail.AdminModeDetail;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.service.ServerService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class AdminModeCondition implements CommandCondition {
@Autowired
private ServerService service;
@Override
public ConditionResult shouldExecute(CommandContext context, Command command) {
boolean adminModeActive = service.adminModeActive(context.getGuild());
if(adminModeActive){
if(context.getAuthor().hasPermission(Permission.ADMINISTRATOR)) {
return ConditionResult.builder().result(true).build();
} else {
return ConditionResult.builder().result(false).conditionDetail(new AdminModeDetail()).build();
}
}
return ConditionResult.builder().result(true).build();
}
}

View File

@@ -0,0 +1,15 @@
package dev.sheldan.abstracto.core.command.condition.detail;
import dev.sheldan.abstracto.core.command.condition.ConditionDetail;
public class AdminModeDetail implements ConditionDetail {
@Override
public String getTemplateName() {
return "admin_mode_enabled_condition";
}
@Override
public Object getTemplateModel() {
return new Object();
}
}

View File

@@ -28,6 +28,10 @@ public class AServer implements SnowFlake, Serializable {
@Column(name = "updated")
private Instant updated;
@Setter
@Column(name = "admin_mode")
private Boolean adminMode;
@Transient
private boolean fake;

View File

@@ -0,0 +1,14 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.models.database.AServer;
import net.dv8tion.jda.api.entities.Guild;
public interface ServerService {
boolean adminModeActive(Long id);
boolean adminModeActive(Guild guild);
void setAdminModeTo(Long id, Boolean newState);
void setAdminModeTo(Guild guild, Boolean newState);
void setAdminModeTo(AServer server, Boolean newState);
void activateAdminMode(Long id);
void deactivateAdminMode(Long id);
}

View File

@@ -42,6 +42,9 @@ Changing a post target::
* Description: Changes the given post target identified by `key` to the given channel. All messages using this post target will be send to this channel from now on.
If neither `key` nor `channel` is given, this will print the currently available post targets and the channels they point to, if set.
* Example: `posttarget banLog #general` to log the bans in the #general channel.
Changing admin mode::
* Usage: `setAdminMode <true/false>`
* Description: Changes the admin modes on this server to the given value. Admin mode means, that **all** commands in the current server, can only be executed by members who have the ADMINISTRATOR permission.
Listing the features::
* Usage: `features`
* Description: Lists the available features and whether or not they are enabled in this server.