Skip to content

Commit 9a5c6c8

Browse files
committed
Make the already existing constants configurable from config.json
For the purposes of speeding up the process of reviews and since it really doesn't matter if they are configurable or not (just extra unnecessary development time), move all the constants into the config.json so that users who run the bot can easily configure these constants without touching the code. Signed-off-by: Chris Sdogkos <work@chris-sdogkos.com>
1 parent 3ba0b7a commit 9a5c6c8

File tree

4 files changed

+59
-26
lines changed

4 files changed

+59
-26
lines changed

application/config.json.template

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,14 @@
205205
"assignmentChannelPattern": "community-commands",
206206
"announcementChannelPattern": "hall-of-fame"
207207
},
208-
"dynamicVoiceChannelPatterns": [
209-
"Gaming",
210-
"Support/Studying Room",
211-
"Chit Chat"
212-
]
208+
"dynamicVoiceChatConfig": {
209+
"dynamicChannelPatterns": [
210+
"Gaming",
211+
"Support/Studying Room",
212+
"Chit Chat"
213+
],
214+
"archiveCategoryPattern": "Voice Channel Archives",
215+
"cleanChannelsAmount": 20,
216+
"minimumChannelsAmount": 40
217+
}
213218
}

application/src/main/java/org/togetherjava/tjbot/config/Config.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public final class Config {
5050
private final String memberCountCategoryPattern;
5151
private final QuoteBoardConfig quoteBoardConfig;
5252
private final TopHelpersConfig topHelpers;
53-
private final List<String> dynamicVoiceChannelPatterns;
53+
private final DynamicVoiceChatConfig dynamicVoiceChatConfig;
5454

5555
@SuppressWarnings("ConstructorWithTooManyParameters")
5656
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
@@ -107,8 +107,8 @@ private Config(@JsonProperty(value = "token", required = true) String token,
107107
@JsonProperty(value = "quoteBoardConfig",
108108
required = true) QuoteBoardConfig quoteBoardConfig,
109109
@JsonProperty(value = "topHelpers", required = true) TopHelpersConfig topHelpers,
110-
@JsonProperty(value = "dynamicVoiceChannelPatterns",
111-
required = true) List<String> dynamicVoiceChannelPatterns) {
110+
@JsonProperty(value = "dynamicVoiceChatConfig",
111+
required = true) DynamicVoiceChatConfig dynamicVoiceChatConfig) {
112112
this.token = Objects.requireNonNull(token);
113113
this.githubApiKey = Objects.requireNonNull(githubApiKey);
114114
this.databasePath = Objects.requireNonNull(databasePath);
@@ -145,7 +145,7 @@ private Config(@JsonProperty(value = "token", required = true) String token,
145145
this.selectRolesChannelPattern = Objects.requireNonNull(selectRolesChannelPattern);
146146
this.quoteBoardConfig = Objects.requireNonNull(quoteBoardConfig);
147147
this.topHelpers = Objects.requireNonNull(topHelpers);
148-
this.dynamicVoiceChannelPatterns = Objects.requireNonNull(dynamicVoiceChannelPatterns);
148+
this.dynamicVoiceChatConfig = Objects.requireNonNull(dynamicVoiceChatConfig);
149149
}
150150

151151
/**
@@ -479,11 +479,11 @@ public TopHelpersConfig getTopHelpers() {
479479
}
480480

481481
/**
482-
* Gets the list of voice channel patterns that are treated dynamically.
482+
* Gets the dynamic voice chat configuration
483483
*
484-
* @return the list of dynamic voice channel patterns
484+
* @return the dynamic voice chat configuration
485485
*/
486-
public List<String> getDynamicVoiceChannelPatterns() {
487-
return dynamicVoiceChannelPatterns;
486+
public DynamicVoiceChatConfig getDynamicVoiceChatConfig() {
487+
return dynamicVoiceChatConfig;
488488
}
489489
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.togetherjava.tjbot.config;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.util.List;
6+
import java.util.Objects;
7+
import java.util.regex.Pattern;
8+
9+
/**
10+
* Configuration for the dynamic voice chat feature.
11+
*
12+
* @param archiveCategoryPattern the name of the Discord Guild category in which the archived
13+
* channels will go
14+
* @param cleanChannelsAmount the amount of channels to clean once a cleanup is triggered
15+
* @param minimumChannelsAmount the amount of voice channels for the archive category to have before
16+
* a cleanup triggers
17+
*/
18+
public record DynamicVoiceChatConfig(
19+
@JsonProperty(value = "dynamicChannelPatterns",
20+
required = true) List<Pattern> dynamicChannelPatterns,
21+
@JsonProperty(value = "archiveCategoryPattern",
22+
required = true) String archiveCategoryPattern,
23+
@JsonProperty(value = "cleanChannelsAmount") int cleanChannelsAmount,
24+
@JsonProperty(value = "minimumChannelsAmount", required = true) int minimumChannelsAmount) {
25+
26+
public DynamicVoiceChatConfig {
27+
Objects.requireNonNull(dynamicChannelPatterns);
28+
Objects.requireNonNull(archiveCategoryPattern);
29+
}
30+
}

application/src/main/java/org/togetherjava/tjbot/features/voicechat/DynamicVoiceChat.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
import org.slf4j.LoggerFactory;
1818

1919
import org.togetherjava.tjbot.config.Config;
20+
import org.togetherjava.tjbot.config.DynamicVoiceChatConfig;
2021
import org.togetherjava.tjbot.features.VoiceReceiverAdapter;
2122

22-
import java.util.List;
2323
import java.util.Optional;
24-
import java.util.regex.Pattern;
2524

2625
/**
2726
* Handles dynamic voice channel creation and deletion based on user activity.
@@ -33,18 +32,15 @@
3332
public final class DynamicVoiceChat extends VoiceReceiverAdapter {
3433
private static final Logger logger = LoggerFactory.getLogger(DynamicVoiceChat.class);
3534

36-
private static final String ARCHIVE_CATEGORY_NAME = "Voice Channel Archives";
37-
private static final int CLEAN_CHANNELS_AMOUNT = 25;
38-
private static final int MINIMUM_CHANNELS_AMOUNT = 50;
39-
4035
private final VoiceChatCleanupStrategy voiceChatCleanupStrategy;
41-
private final List<Pattern> dynamicVoiceChannelPatterns;
36+
private final DynamicVoiceChatConfig dynamicVoiceChannelConfig;
4237

4338
public DynamicVoiceChat(Config config) {
44-
this.dynamicVoiceChannelPatterns =
45-
config.getDynamicVoiceChannelPatterns().stream().map(Pattern::compile).toList();
39+
this.dynamicVoiceChannelConfig = config.getDynamicVoiceChatConfig();
40+
4641
this.voiceChatCleanupStrategy =
47-
new OldestVoiceChatCleanup(CLEAN_CHANNELS_AMOUNT, MINIMUM_CHANNELS_AMOUNT);
42+
new OldestVoiceChatCleanup(dynamicVoiceChannelConfig.cleanChannelsAmount(),
43+
dynamicVoiceChannelConfig.minimumChannelsAmount());
4844
}
4945

5046
@Override
@@ -74,7 +70,8 @@ public void onVoiceUpdate(@NotNull GuildVoiceUpdateEvent event) {
7470
}
7571

7672
private boolean eventHappenOnDynamicRootChannel(AudioChannelUnion channel) {
77-
return dynamicVoiceChannelPatterns.stream()
73+
return dynamicVoiceChannelConfig.dynamicChannelPatterns()
74+
.stream()
7875
.anyMatch(pattern -> pattern.matcher(channel.getName()).matches());
7976
}
8077

@@ -119,7 +116,8 @@ private void archiveDynamicVoiceChannel(AudioChannelUnion channel) {
119116
Optional<Category> archiveCategoryOptional = channel.getGuild()
120117
.getCategoryCache()
121118
.stream()
122-
.filter(c -> c.getName().equalsIgnoreCase(ARCHIVE_CATEGORY_NAME))
119+
.filter(c -> c.getName()
120+
.equalsIgnoreCase(dynamicVoiceChannelConfig.archiveCategoryPattern()))
123121
.findFirst();
124122

125123
AudioChannelManager<?, ?> channelManager = channel.getManager();
@@ -130,7 +128,7 @@ private void archiveDynamicVoiceChannel(AudioChannelUnion channel) {
130128
if (archiveCategoryOptional.isEmpty()) {
131129
logger.warn("Could not find archive category. Attempting to create one...");
132130
channel.getGuild()
133-
.createCategory(ARCHIVE_CATEGORY_NAME)
131+
.createCategory(dynamicVoiceChannelConfig.archiveCategoryPattern())
134132
.queue(newCategory -> restActionChain.and(channelManager.setParent(newCategory))
135133
.queue());
136134
return;

0 commit comments

Comments
 (0)