Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'

implementation 'net.dv8tion:JDA:4.3.0_309'
// implementation 'com.github.dv8fromtheworld:jda:feature~threads'
implementation 'org.mongodb:mongodb-driver:3.12.10'
implementation 'com.google.code.gson:gson:2.8.7'
implementation 'org.yaml:snakeyaml:1.29'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.javadiscord.javabot.commands.configuation;

import com.javadiscord.javabot.commands.DelegatingCommandHandler;

/**
* The main command for interacting with the bot's configuration at runtime via
* slash commands.
*/
public class Config extends DelegatingCommandHandler {
public Config() {
addSubcommand("list", new ListSubcommand());
addSubcommand("get", new GetSubcommand());
addSubcommand("set", new SetSubcommand());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.javadiscord.javabot.commands.configuation;

import com.javadiscord.javabot.Bot;
import com.javadiscord.javabot.commands.Responses;
import com.javadiscord.javabot.commands.SlashCommandHandler;
import com.javadiscord.javabot.properties.config.UnknownPropertyException;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction;

public class GetSubcommand implements SlashCommandHandler {
@Override
public ReplyAction handle(SlashCommandEvent event) {
var propertyOption = event.getOption("property");
if (propertyOption == null) {
return Responses.warning(event, "Missing required property argument.");
}
String property = propertyOption.getAsString().trim();
try {
Object value = Bot.config.get(event.getGuild()).resolve(property);
return Responses.info(event, "Configuration Property", String.format("The value of the property `%s` is `%s`.", property, value));
} catch (UnknownPropertyException e) {
return Responses.warning(event, "Unknown Property", "The property `" + property + "` could not be found.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.javadiscord.javabot.commands.configuation;

import com.javadiscord.javabot.Bot;
import com.javadiscord.javabot.commands.Responses;
import com.javadiscord.javabot.commands.SlashCommandHandler;
import com.javadiscord.javabot.properties.config.GuildConfig;
import com.javadiscord.javabot.properties.config.ReflectionUtils;
import com.javadiscord.javabot.properties.config.UnknownPropertyException;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction;

import java.util.Map;
import java.util.stream.Collectors;

/**
* Shows a list of all known configuration properties, their type, and their
* current value.
*/
public class ListSubcommand implements SlashCommandHandler {
@Override
public ReplyAction handle(SlashCommandEvent event) {
try {
var results = ReflectionUtils.getFields(null, GuildConfig.class);
String msg = results.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.map(entry -> {
Object propertyValue = null;
try {
propertyValue = Bot.config.get(event.getGuild()).resolve(entry.getKey());
} catch (UnknownPropertyException e) {
e.printStackTrace();
}
return String.format(
"**%s** `%s` = `%s`",
entry.getValue().getSimpleName(),
entry.getKey(),
propertyValue
);
})
.collect(Collectors.joining("\n"));
return Responses.info(event, "Configuration Properties", msg);
} catch (IllegalAccessException e) {
e.printStackTrace();
return Responses.error(event, e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.javadiscord.javabot.commands.configuation;

import com.javadiscord.javabot.Bot;
import com.javadiscord.javabot.commands.Responses;
import com.javadiscord.javabot.commands.SlashCommandHandler;
import com.javadiscord.javabot.properties.config.UnknownPropertyException;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction;

public class SetSubcommand implements SlashCommandHandler {
@Override
public ReplyAction handle(SlashCommandEvent event) {
var propertyOption = event.getOption("property");
var valueOption = event.getOption("value");
if (propertyOption == null || valueOption == null) {
return Responses.warning(event, "Missing required arguments.");
}
String property = propertyOption.getAsString().trim();
String valueString = valueOption.getAsString().trim();
try {
Bot.config.get(event.getGuild()).set(property, valueString);
return Responses.success(event, "Configuration Updated", String.format("The property `%s` has been set to `%s`.", property, valueString));
} catch (UnknownPropertyException e) {
return Responses.warning(event, "Unknown Property", "The property `" + property + "` could not be found.");
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading