[SIS-8] creating modmode customization

enabling entertainment and custom command module
This commit is contained in:
Sheldan
2022-07-21 23:09:07 +02:00
parent d74c10c618
commit 7f8c429a04
28 changed files with 398 additions and 9 deletions

View File

@@ -0,0 +1,95 @@
package dev.sheldan.sissi.module.custom.moderation.commands;
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.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.interaction.slash.SlashCommandConfig;
import dev.sheldan.abstracto.core.interaction.slash.parameter.SlashCommandParameterService;
import dev.sheldan.abstracto.moderation.config.ModerationModuleDefinition;
import dev.sheldan.sissi.module.custom.moderation.config.ModerationCustomFeatureDefinition;
import dev.sheldan.sissi.module.custom.moderation.config.ModerationCustomSlashCommandNames;
import dev.sheldan.sissi.module.custom.moderation.service.ModModeServiceBean;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@Component
public class ModMode extends AbstractConditionableCommand {
public static final String NEW_STATE_PARAMETER = "newState";
public static final String MOD_MODE_COMMAND = "modMode";
public static final String MOD_MODE_RESPONSE = "modMode_response";
@Autowired
private ModModeServiceBean modModeServiceBean;
@Autowired
private SlashCommandParameterService slashCommandParameterService;
@Autowired
private InteractionService interactionService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
Boolean newState = (Boolean) commandContext.getParameters().getParameters().get(0);
return modModeServiceBean.setModModeTo(commandContext.getGuild(), newState)
.thenApply(unused -> CommandResult.fromSuccess());
}
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
Boolean newState = slashCommandParameterService.getCommandOption(NEW_STATE_PARAMETER, event, Boolean.class);
return modModeServiceBean.setModModeTo(event.getGuild(), newState)
.thenApply(unused -> interactionService.replyEmbed(MOD_MODE_RESPONSE, event))
.thenApply(interactionHookCompletableFuture -> CommandResult.fromSuccess());
}
@Override
public CommandConfiguration getConfiguration() {
Parameter memberParameter = Parameter
.builder()
.templated(true)
.name(NEW_STATE_PARAMETER)
.type(Boolean.class)
.build();
List<Parameter> parameters = Collections.singletonList(memberParameter);
HelpInfo helpInfo = HelpInfo
.builder()
.templated(true)
.build();
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.rootCommandName(ModerationCustomSlashCommandNames.MODERATION)
.commandName(MOD_MODE_COMMAND)
.build();
return CommandConfiguration.builder()
.name(MOD_MODE_COMMAND)
.async(true)
.slashCommandConfig(slashCommandConfig)
.module(ModerationModuleDefinition.MODERATION)
.parameters(parameters)
.help(helpInfo)
.templated(true)
.supportsEmbedException(true)
.causesReaction(true)
.build();
}
@Override
public FeatureDefinition getFeature() {
return ModerationCustomFeatureDefinition.MODERATION_CUSTOM;
}
}

View File

@@ -1,15 +1,16 @@
package dev.sheldan.sissi.module.costum.config;
package dev.sheldan.sissi.module.custom.moderation.config;
import dev.sheldan.abstracto.core.config.FeatureConfig;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.moderation.config.feature.ReportReactionFeatureConfig;
import dev.sheldan.sissi.module.custom.moderation.service.ModModeServiceBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import static dev.sheldan.sissi.module.costum.listener.ReactionReportReactionListener.REACTION_REPORT_REACTION_AMOUNT_CONFIG_KEY;
import static dev.sheldan.sissi.module.custom.moderation.listener.ReactionReportReactionListener.REACTION_REPORT_REACTION_AMOUNT_CONFIG_KEY;
@Component
public class ModerationCustomFeature implements FeatureConfig {
@@ -29,6 +30,7 @@ public class ModerationCustomFeature implements FeatureConfig {
@Override
public List<String> getRequiredSystemConfigKeys() {
return Arrays.asList(REACTION_REPORT_REACTION_AMOUNT_CONFIG_KEY);
return Arrays.asList(REACTION_REPORT_REACTION_AMOUNT_CONFIG_KEY, ModModeServiceBean.MODMODE_ROLE_CONFIG_KEY,
ModModeServiceBean.MODMODE_CHANGED_ROLE_COLOR_CONFIG_KEY);
}
}

View File

@@ -1,4 +1,4 @@
package dev.sheldan.sissi.module.costum.config;
package dev.sheldan.sissi.module.custom.moderation.config;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import lombok.Getter;

View File

@@ -1,4 +1,4 @@
package dev.sheldan.sissi.module.costum.config;
package dev.sheldan.sissi.module.custom.moderation.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

View File

@@ -0,0 +1,5 @@
package dev.sheldan.sissi.module.custom.moderation.config;
public class ModerationCustomSlashCommandNames {
public static final String MODERATION = "moderation";
}

View File

@@ -0,0 +1,15 @@
package dev.sheldan.sissi.module.custom.moderation.exception;
import dev.sheldan.abstracto.core.exception.AbstractoTemplatableException;
public class ModRoleNotFoundException extends AbstractoTemplatableException {
@Override
public String getTemplateName() {
return "mod_role_not_found_exception";
}
@Override
public Object getTemplateModel() {
return new Object();
}
}

View File

@@ -1,4 +1,4 @@
package dev.sheldan.sissi.module.costum.listener;
package dev.sheldan.sissi.module.custom.moderation.listener;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.listener.DefaultListenerResult;
@@ -6,7 +6,7 @@ import dev.sheldan.abstracto.core.service.ConfigService;
import dev.sheldan.abstracto.core.service.ReactionService;
import dev.sheldan.abstracto.moderation.listener.ReportMessageCreatedListener;
import dev.sheldan.abstracto.moderation.model.listener.ReportMessageCreatedModel;
import dev.sheldan.sissi.module.costum.config.ModerationCustomFeatureDefinition;
import dev.sheldan.sissi.module.custom.moderation.config.ModerationCustomFeatureDefinition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

View File

@@ -0,0 +1,55 @@
package dev.sheldan.sissi.module.custom.moderation.service;
import dev.sheldan.abstracto.core.service.ConfigService;
import dev.sheldan.sissi.module.custom.moderation.exception.ModRoleNotFoundException;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.awt.*;
import java.util.concurrent.CompletableFuture;
@Component
@Slf4j
public class ModModeServiceBean {
public static final String MODMODE_ROLE_CONFIG_KEY = "modModeRoleId";
public static final String MODMODE_CHANGED_ROLE_COLOR_CONFIG_KEY = "modModeNewRoleColor";
@Autowired
private ConfigService configService;
public CompletableFuture<Void> setModModeTo(Guild guild, Boolean newState) {
if(Boolean.TRUE.equals(newState)) {
return enableModMode(guild);
} else {
return disableModMoe(guild);
}
}
private CompletableFuture<Void> enableModMode(Guild guild) {
Color colorToSet = getColorFromConfig(MODMODE_CHANGED_ROLE_COLOR_CONFIG_KEY, guild);
return setModRoleTo(guild, colorToSet);
}
private CompletableFuture<Void> disableModMoe(Guild guild) {
return setModRoleTo(guild, null);
}
private Color getColorFromConfig(String key, Guild guild) {
String colorString = configService.getStringValueOrConfigDefault(key, guild.getIdLong());
String[] parts = colorString.split(",");
return new Color(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
}
private CompletableFuture<Void> setModRoleTo(Guild guild, Color color) {
Long roleId = configService.getLongValue(MODMODE_ROLE_CONFIG_KEY, guild.getIdLong());
Role modRole = guild.getRoleById(roleId);
if(modRole != null) {
return modRole.getManager().setColor(color).submit();
} else {
throw new ModRoleNotFoundException();
}
}
}

View File

@@ -0,0 +1,10 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
<include file="seedData/data.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -0,0 +1,18 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
<property name="moderationCustomFeature" value="(SELECT id FROM feature WHERE key = 'moderationCustom')"/>
<property name="moderationModule" value="(SELECT id FROM module WHERE name = 'moderation')"/>
<changeSet author="Sheldan" id="moderationCustom_modmode-commands">
<insert tableName="command">
<column name="name" value="modMode"/>
<column name="module_id" valueComputed="${moderationModule}"/>
<column name="feature_id" valueComputed="${moderationCustomFeature}"/>
</insert>
</changeSet>
</databaseChangeLog>

View File

@@ -0,0 +1,10 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
<include file="command.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -7,4 +7,5 @@
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
<include file="1.1.0/collection.xml" relativeToChangelogFile="true"/>
<include file="1.2.1/collection.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -4,3 +4,8 @@ abstracto.featureFlags.moderationCustom.enabled=false
abstracto.systemConfigs.reportReactionAmount.name=reportReactionAmount
abstracto.systemConfigs.reportReactionAmount.longValue=5
abstracto.systemConfigs.modModeRoleId.name=modModeRoleId
abstracto.systemConfigs.modModeRoleId.longValue=0
abstracto.systemConfigs.modModeNewRoleColor.name=modModeNewRoleColor
abstracto.systemConfigs.modModeNewRoleColor.stringValue=0,0,0