Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
for(Class<?> cl : getClass().getClassLoader().loadClass("sun.font.FontConfigManager").getDeclaredClasses()) {
hints.jni().registerType(cl, MemberCategory.ACCESS_DECLARED_FIELDS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException _) {
// FontConfigManager is only supported on Linux
}

for (Class<?> cl : WebhookEmbed.class.getClasses()) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/discordjug/javabot/SpringConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import java.util.Collection;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.regex.Pattern;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.discordjug.javabot.data.config.PatternTypeAdapter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -90,4 +94,13 @@ BotConfig botConfig() {
}
return botConfig;
}

@Bean
Gson gson() {
return new GsonBuilder()
.serializeNulls()
.setPrettyPrinting()
.registerTypeAdapter(Pattern.class, new PatternTypeAdapter())
.create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ public void set(String propertyName, String value) throws UnknownPropertyExcepti
Optional<Pair<Field, Object>> result = ReflectionUtils.resolveField(propertyName, this);
result.ifPresent(pair -> {
try {
ReflectionUtils.set(pair.first(), pair.second(), value);
Field updatedField = ReflectionUtils.set(pair.first(), pair.second(), value);
Comment thread
danthe1st marked this conversation as resolved.
Outdated
if (updatedField.get(pair.second()) instanceof GuildConfigItem) {
((GuildConfigItem) updatedField.get(pair.second())).setGuildConfig(this);
Comment thread
danthe1st marked this conversation as resolved.
Outdated
}
this.flush();
} catch (IllegalAccessException e) {
ExceptionLogger.capture(e, getClass().getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.discordjug.javabot.data.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import net.discordjug.javabot.util.ExceptionLogger;
import net.discordjug.javabot.util.Pair;
Expand All @@ -14,13 +16,19 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.regex.Pattern;

/**
* Utility class for resolving JSON files.
*/
@Slf4j
public class ReflectionUtils {
private static final Map<Class<?>, Function<String, Object>> propertyTypeParsers = new HashMap<>();
Comment thread
danthe1st marked this conversation as resolved.
Outdated
private static final Gson gson = new GsonBuilder()
Comment thread
danthe1st marked this conversation as resolved.
Outdated
.serializeNulls()
.setPrettyPrinting()
.registerTypeAdapter(Pattern.class, new PatternTypeAdapter())
.create();

static {
propertyTypeParsers.put(Integer.class, Integer::parseInt);
Expand Down Expand Up @@ -126,13 +134,11 @@ private static Field findDeclaredField(Class<?> cl, String name) throws NoSuchFi
* @param field The field to set.
* @param parent The object whose property value to set.
* @param s The string representation of the value.
* @return Returns the edited {@link Field}.
* @throws IllegalAccessException If the field cannot be set.
*/
public static void set(@NotNull Field field, @NotNull Object parent, @NotNull String s) throws IllegalAccessException {
Function<String, Object> parser = propertyTypeParsers.get(field.getType());
if (parser == null) {
throw new IllegalArgumentException("No supported property type parser for the type " + field.getType().getSimpleName());
}
field.set(parent, parser.apply(s));
public static Field set(@NotNull Field field, @NotNull Object parent, @NotNull String s) throws IllegalAccessException {
field.set(parent, gson.fromJson(s, field.getType()));
Comment thread
danthe1st marked this conversation as resolved.
Outdated
return field;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@
import java.time.Duration;
import java.util.Base64;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.RequiredArgsConstructor;
import net.discordjug.javabot.data.config.BotConfig;
import net.discordjug.javabot.data.config.PatternTypeAdapter;
import net.discordjug.javabot.data.config.guild.MessageRule;
import net.discordjug.javabot.data.config.guild.MessageRule.MessageAction;
import net.discordjug.javabot.data.config.guild.ModerationConfig;
Expand All @@ -41,6 +38,7 @@ public class MessageRuleFilter implements MessageFilter {

private final BotConfig botConfig;
private final MessageCache messageCache;
private final Gson gson;

@Override
public MessageModificationStatus processMessage(MessageContent content) {
Expand Down Expand Up @@ -73,11 +71,6 @@ public MessageModificationStatus processMessage(MessageContent content) {
}

private void log(MessageContent content, MessageRule ruleToExecute, ModerationConfig moderationConfig) {
Gson gson = new GsonBuilder()
.serializeNulls()
.setPrettyPrinting()
.registerTypeAdapter(Pattern.class, new PatternTypeAdapter())
.create();
EmbedBuilder embed = messageCache.buildMessageCacheEmbed(
content.event().getMessage().getChannel(),
content.event().getMessage().getAuthor(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.discordjug.javabot.systems.configuration;

import com.google.gson.Gson;
import net.discordjug.javabot.data.config.BotConfig;
import net.discordjug.javabot.data.config.GuildConfig;
import net.discordjug.javabot.data.config.UnknownPropertyException;
Expand All @@ -19,15 +20,20 @@
* Subcommand that allows staff-members to get a single property variable from the guild config.
*/
public class GetConfigSubcommand extends ConfigSubcommand implements AutoCompletable {

private final Gson gson;

/**
* The constructor of this class, which sets the corresponding {@link SubcommandData}.
* @param botConfig The main configuration of the bot
* @param gson Mapper used for displaying config as json
*/
public GetConfigSubcommand(BotConfig botConfig) {
public GetConfigSubcommand(BotConfig botConfig, Gson gson) {
super(botConfig);
setCommandData(new SubcommandData("get", "Get the current value of a configuration property.")
.addOption(OptionType.STRING, "property", "The name of a property.", true, true)
);
this.gson = gson;
}

@Override
Expand All @@ -38,7 +44,8 @@ public ReplyCallbackAction handleConfigSubcommand(@Nonnull SlashCommandInteracti
}
String property = propertyOption.getAsString().trim();
Object value = config.resolve(property);
return Responses.info(event, "Configuration Property", "The value of the property `%s` is:\n```\n%s\n```", property, value);
String json = gson.toJson(value);
return Responses.info(event, "Configuration Property", "The value of the property `%s` is:\n```\n%s\n```", property, json);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.discordjug.javabot.systems.configuration;

import com.google.gson.Gson;
import net.discordjug.javabot.annotations.AutoDetectableComponentHandler;
import net.discordjug.javabot.data.config.BotConfig;
import net.discordjug.javabot.data.config.GuildConfig;
Expand Down Expand Up @@ -31,16 +32,21 @@
*/
@AutoDetectableComponentHandler("config-set")
public class SetConfigSubcommand extends ConfigSubcommand implements ModalHandler, AutoCompletable {

private final Gson gson;

/**
* The constructor of this class, which sets the corresponding {@link SubcommandData}.
* @param botConfig The main configuration of the bot
* @param gson Mapper used for displaying config as json
Comment thread
danthe1st marked this conversation as resolved.
Outdated
*/
public SetConfigSubcommand(BotConfig botConfig) {
public SetConfigSubcommand(BotConfig botConfig, Gson gson) {
super(botConfig);
setCommandData(new SubcommandData("set", "Sets the value of a configuration property.")
.addOption(OptionType.STRING, "property", "The name of a property.", true, true)
.addOption(OptionType.STRING, "value", "The value to set for the property.", false)
);
this.gson = gson;
}

@Override
Expand All @@ -57,10 +63,11 @@ public InteractionCallbackAction<?> handleConfigSubcommand(@Nonnull SlashCommand
if (resolved == null) {
return Responses.error(event, "Config `%s` not found", property);
}
String json = gson.toJson(resolved);
return event.replyModal(
Modal.create(ComponentIdBuilder.build("config-set", property), "Change configuration value")
.addComponents(Label.of("new value", TextInput.create("value", TextInputStyle.PARAGRAPH)
.setValue(String.valueOf(resolved))
.setValue(json)
.build()))
.build());
}
Expand Down