Skip to content

Commit 5ac1e07

Browse files
devloveschristolisTaz03
authored
Member Count Display (#1038)
* Added MemberCounter Routine * feat: use pattern compile * removed debug lines * pattern fixes in config * refactor: change cat to category * feat: switch to findAny from forEach * perf: remove pattern compilation for each call Co-authored-by: Devansh Tiwari <[email protected]> * perf: fixed solar linting error * perf: made memberCountCategoryPattern required * Update application/config.json.template Co-authored-by: Chris Sdogkos <[email protected]> * fixed: routine not updating channel name after first write * fixed comment --------- Co-authored-by: christolis <[email protected]> Co-authored-by: Tanish Azad <[email protected]>
1 parent 6dd670a commit 5ac1e07

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

application/config.json.template

+1
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,6 @@
110110
"special": [
111111
]
112112
},
113+
"memberCountCategoryPattern": "Info",
113114
"selectRolesChannelPattern": "select-your-roles"
114115
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public final class Config {
4444
private final HelperPruneConfig helperPruneConfig;
4545
private final FeatureBlacklistConfig featureBlacklistConfig;
4646
private final String selectRolesChannelPattern;
47+
private final String memberCountCategoryPattern;
4748

4849
@SuppressWarnings("ConstructorWithTooManyParameters")
4950
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
@@ -86,6 +87,8 @@ private Config(@JsonProperty(value = "token", required = true) String token,
8687
@JsonProperty(value = "openaiApiKey", required = true) String openaiApiKey,
8788
@JsonProperty(value = "sourceCodeBaseUrl", required = true) String sourceCodeBaseUrl,
8889
@JsonProperty(value = "jshell", required = true) JShellConfig jshell,
90+
@JsonProperty(value = "memberCountCategoryPattern",
91+
required = true) String memberCountCategoryPattern,
8992
@JsonProperty(value = "helperPruneConfig",
9093
required = true) HelperPruneConfig helperPruneConfig,
9194
@JsonProperty(value = "featureBlacklist",
@@ -96,6 +99,7 @@ private Config(@JsonProperty(value = "token", required = true) String token,
9699
this.githubApiKey = Objects.requireNonNull(githubApiKey);
97100
this.databasePath = Objects.requireNonNull(databasePath);
98101
this.projectWebsite = Objects.requireNonNull(projectWebsite);
102+
this.memberCountCategoryPattern = Objects.requireNonNull(memberCountCategoryPattern);
99103
this.discordGuildInvite = Objects.requireNonNull(discordGuildInvite);
100104
this.modAuditLogChannelPattern = Objects.requireNonNull(modAuditLogChannelPattern);
101105
this.modMailChannelPattern = Objects.requireNonNull(modMailChannelPattern);
@@ -405,4 +409,13 @@ public FeatureBlacklistConfig getFeatureBlacklistConfig() {
405409
public String getSelectRolesChannelPattern() {
406410
return selectRolesChannelPattern;
407411
}
412+
413+
/**
414+
* Gets the pattern matching the category that is used to display the total member count.
415+
*
416+
* @return the categories name types
417+
*/
418+
public String getMemberCountCategoryPattern() {
419+
return memberCountCategoryPattern;
420+
}
408421
}

application/src/main/java/org/togetherjava/tjbot/features/Features.java

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.togetherjava.tjbot.config.FeatureBlacklist;
99
import org.togetherjava.tjbot.config.FeatureBlacklistConfig;
1010
import org.togetherjava.tjbot.db.Database;
11+
import org.togetherjava.tjbot.features.basic.MemberCountDisplayRoutine;
1112
import org.togetherjava.tjbot.features.basic.PingCommand;
1213
import org.togetherjava.tjbot.features.basic.RoleSelectCommand;
1314
import org.togetherjava.tjbot.features.basic.SlashCommandEducator;
@@ -109,6 +110,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
109110
.add(new AutoPruneHelperRoutine(config, helpSystemHelper, modAuditLogWriter, database));
110111
features.add(new HelpThreadAutoArchiver(helpSystemHelper));
111112
features.add(new LeftoverBookmarksCleanupRoutine(bookmarksSystem));
113+
features.add(new MemberCountDisplayRoutine(config));
112114

113115
// Message receivers
114116
features.add(new TopHelpersMessageListener(database, config));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.togetherjava.tjbot.features.basic;
2+
3+
import net.dv8tion.jda.api.JDA;
4+
import net.dv8tion.jda.api.entities.channel.concrete.Category;
5+
6+
import org.togetherjava.tjbot.config.Config;
7+
import org.togetherjava.tjbot.features.Routine;
8+
9+
import java.util.concurrent.TimeUnit;
10+
import java.util.function.Predicate;
11+
import java.util.regex.Pattern;
12+
13+
/**
14+
* Shows the guild member count on selected category, which updates everyday.
15+
*/
16+
public class MemberCountDisplayRoutine implements Routine {
17+
private final Predicate<String> memberCountCategoryPredicate;
18+
19+
/**
20+
* Creates an instance on member count display routine.
21+
*
22+
* @param config the config to use
23+
*/
24+
public MemberCountDisplayRoutine(Config config) {
25+
memberCountCategoryPredicate =
26+
Pattern.compile(config.getMemberCountCategoryPattern() + "( - \\d+ Members)?")
27+
.asMatchPredicate();
28+
}
29+
30+
private void updateCategoryName(Category category) {
31+
int totalMemberCount = category.getGuild().getMemberCount();
32+
String baseName = category.getName().split("-")[0].trim();
33+
34+
category.getManager()
35+
.setName("%s - %d Members".formatted(baseName, totalMemberCount))
36+
.queue();
37+
}
38+
39+
@Override
40+
public Schedule createSchedule() {
41+
return new Schedule(ScheduleMode.FIXED_RATE, 0, 24, TimeUnit.HOURS);
42+
}
43+
44+
@Override
45+
public void runRoutine(JDA jda) {
46+
jda.getGuilds()
47+
.forEach(guild -> guild.getCategories()
48+
.stream()
49+
.filter(category -> memberCountCategoryPredicate.test(category.getName()))
50+
.findAny()
51+
.ifPresent(this::updateCategoryName));
52+
}
53+
}

0 commit comments

Comments
 (0)