Skip to content

Commit 23ade16

Browse files
author
Dynxsty
authored
Merge pull request #76 from Java-Discord/andrew/configSetting
Add the Ability to Read and Write Configuration Properties via Slash Commands
2 parents 2b6f6e7 + 9601828 commit 23ade16

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+298
-926
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dependencies {
1919
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
2020

2121
implementation 'net.dv8tion:JDA:4.3.0_309'
22+
// implementation 'com.github.dv8fromtheworld:jda:feature~threads'
2223
implementation 'org.mongodb:mongodb-driver:3.12.10'
2324
implementation 'com.google.code.gson:gson:2.8.7'
2425
implementation 'org.yaml:snakeyaml:1.29'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.javadiscord.javabot.commands.configuation;
2+
3+
import com.javadiscord.javabot.commands.DelegatingCommandHandler;
4+
5+
/**
6+
* The main command for interacting with the bot's configuration at runtime via
7+
* slash commands.
8+
*/
9+
public class Config extends DelegatingCommandHandler {
10+
public Config() {
11+
addSubcommand("list", new ListSubcommand());
12+
addSubcommand("get", new GetSubcommand());
13+
addSubcommand("set", new SetSubcommand());
14+
}
15+
}
16+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.javadiscord.javabot.commands.configuation;
2+
3+
import com.javadiscord.javabot.Bot;
4+
import com.javadiscord.javabot.commands.Responses;
5+
import com.javadiscord.javabot.commands.SlashCommandHandler;
6+
import com.javadiscord.javabot.properties.config.UnknownPropertyException;
7+
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
8+
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction;
9+
10+
public class GetSubcommand implements SlashCommandHandler {
11+
@Override
12+
public ReplyAction handle(SlashCommandEvent event) {
13+
var propertyOption = event.getOption("property");
14+
if (propertyOption == null) {
15+
return Responses.warning(event, "Missing required property argument.");
16+
}
17+
String property = propertyOption.getAsString().trim();
18+
try {
19+
Object value = Bot.config.get(event.getGuild()).resolve(property);
20+
return Responses.info(event, "Configuration Property", String.format("The value of the property `%s` is `%s`.", property, value));
21+
} catch (UnknownPropertyException e) {
22+
return Responses.warning(event, "Unknown Property", "The property `" + property + "` could not be found.");
23+
}
24+
}
25+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.javadiscord.javabot.commands.configuation;
2+
3+
import com.javadiscord.javabot.Bot;
4+
import com.javadiscord.javabot.commands.Responses;
5+
import com.javadiscord.javabot.commands.SlashCommandHandler;
6+
import com.javadiscord.javabot.properties.config.GuildConfig;
7+
import com.javadiscord.javabot.properties.config.ReflectionUtils;
8+
import com.javadiscord.javabot.properties.config.UnknownPropertyException;
9+
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
10+
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction;
11+
12+
import java.util.Map;
13+
import java.util.stream.Collectors;
14+
15+
/**
16+
* Shows a list of all known configuration properties, their type, and their
17+
* current value.
18+
*/
19+
public class ListSubcommand implements SlashCommandHandler {
20+
@Override
21+
public ReplyAction handle(SlashCommandEvent event) {
22+
try {
23+
var results = ReflectionUtils.getFields(null, GuildConfig.class);
24+
String msg = results.entrySet().stream()
25+
.sorted(Map.Entry.comparingByKey())
26+
.map(entry -> {
27+
Object propertyValue = null;
28+
try {
29+
propertyValue = Bot.config.get(event.getGuild()).resolve(entry.getKey());
30+
} catch (UnknownPropertyException e) {
31+
e.printStackTrace();
32+
}
33+
return String.format(
34+
"**%s** `%s` = `%s`",
35+
entry.getValue().getSimpleName(),
36+
entry.getKey(),
37+
propertyValue
38+
);
39+
})
40+
.collect(Collectors.joining("\n"));
41+
return Responses.info(event, "Configuration Properties", msg);
42+
} catch (IllegalAccessException e) {
43+
e.printStackTrace();
44+
return Responses.error(event, e.getMessage());
45+
}
46+
}
47+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.javadiscord.javabot.commands.configuation;
2+
3+
import com.javadiscord.javabot.Bot;
4+
import com.javadiscord.javabot.commands.Responses;
5+
import com.javadiscord.javabot.commands.SlashCommandHandler;
6+
import com.javadiscord.javabot.properties.config.UnknownPropertyException;
7+
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
8+
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction;
9+
10+
public class SetSubcommand implements SlashCommandHandler {
11+
@Override
12+
public ReplyAction handle(SlashCommandEvent event) {
13+
var propertyOption = event.getOption("property");
14+
var valueOption = event.getOption("value");
15+
if (propertyOption == null || valueOption == null) {
16+
return Responses.warning(event, "Missing required arguments.");
17+
}
18+
String property = propertyOption.getAsString().trim();
19+
String valueString = valueOption.getAsString().trim();
20+
try {
21+
Bot.config.get(event.getGuild()).set(property, valueString);
22+
return Responses.success(event, "Configuration Updated", String.format("The property `%s` has been set to `%s`.", property, valueString));
23+
} catch (UnknownPropertyException e) {
24+
return Responses.warning(event, "Unknown Property", "The property `" + property + "` could not be found.");
25+
}
26+
}
27+
}

src/main/java/com/javadiscord/javabot/commands/configuation/config/Config.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/GetList.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetDMQOTWStatus.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamAdminRole.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/com/javadiscord/javabot/commands/configuation/config/subcommands/SetJamAnnouncementChannel.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)