From 5ac1e07e010fb21a5709891be7b8a4ad8ecf74c4 Mon Sep 17 00:00:00 2001 From: Devansh Tiwari <65783463+devloves@users.noreply.github.com> Date: Wed, 13 Mar 2024 20:51:51 +0530 Subject: [PATCH] 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 <65783463+devloves@users.noreply.github.com> * perf: fixed solar linting error * perf: made memberCountCategoryPattern required * Update application/config.json.template Co-authored-by: Chris Sdogkos * fixed: routine not updating channel name after first write * fixed comment --------- Co-authored-by: christolis Co-authored-by: Tanish Azad --- application/config.json.template | 1 + .../org/togetherjava/tjbot/config/Config.java | 13 +++++ .../togetherjava/tjbot/features/Features.java | 2 + .../basic/MemberCountDisplayRoutine.java | 53 +++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 application/src/main/java/org/togetherjava/tjbot/features/basic/MemberCountDisplayRoutine.java diff --git a/application/config.json.template b/application/config.json.template index 23e8f72648..7958745d39 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -110,5 +110,6 @@ "special": [ ] }, + "memberCountCategoryPattern": "Info", "selectRolesChannelPattern": "select-your-roles" } diff --git a/application/src/main/java/org/togetherjava/tjbot/config/Config.java b/application/src/main/java/org/togetherjava/tjbot/config/Config.java index 57c3f98954..7d809b6c9a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/Config.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/Config.java @@ -44,6 +44,7 @@ public final class Config { private final HelperPruneConfig helperPruneConfig; private final FeatureBlacklistConfig featureBlacklistConfig; private final String selectRolesChannelPattern; + private final String memberCountCategoryPattern; @SuppressWarnings("ConstructorWithTooManyParameters") @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) @@ -86,6 +87,8 @@ private Config(@JsonProperty(value = "token", required = true) String token, @JsonProperty(value = "openaiApiKey", required = true) String openaiApiKey, @JsonProperty(value = "sourceCodeBaseUrl", required = true) String sourceCodeBaseUrl, @JsonProperty(value = "jshell", required = true) JShellConfig jshell, + @JsonProperty(value = "memberCountCategoryPattern", + required = true) String memberCountCategoryPattern, @JsonProperty(value = "helperPruneConfig", required = true) HelperPruneConfig helperPruneConfig, @JsonProperty(value = "featureBlacklist", @@ -96,6 +99,7 @@ private Config(@JsonProperty(value = "token", required = true) String token, this.githubApiKey = Objects.requireNonNull(githubApiKey); this.databasePath = Objects.requireNonNull(databasePath); this.projectWebsite = Objects.requireNonNull(projectWebsite); + this.memberCountCategoryPattern = Objects.requireNonNull(memberCountCategoryPattern); this.discordGuildInvite = Objects.requireNonNull(discordGuildInvite); this.modAuditLogChannelPattern = Objects.requireNonNull(modAuditLogChannelPattern); this.modMailChannelPattern = Objects.requireNonNull(modMailChannelPattern); @@ -405,4 +409,13 @@ public FeatureBlacklistConfig getFeatureBlacklistConfig() { public String getSelectRolesChannelPattern() { return selectRolesChannelPattern; } + + /** + * Gets the pattern matching the category that is used to display the total member count. + * + * @return the categories name types + */ + public String getMemberCountCategoryPattern() { + return memberCountCategoryPattern; + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/features/Features.java b/application/src/main/java/org/togetherjava/tjbot/features/Features.java index 1052381988..38da09aca0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/Features.java @@ -8,6 +8,7 @@ import org.togetherjava.tjbot.config.FeatureBlacklist; import org.togetherjava.tjbot.config.FeatureBlacklistConfig; import org.togetherjava.tjbot.db.Database; +import org.togetherjava.tjbot.features.basic.MemberCountDisplayRoutine; import org.togetherjava.tjbot.features.basic.PingCommand; import org.togetherjava.tjbot.features.basic.RoleSelectCommand; import org.togetherjava.tjbot.features.basic.SlashCommandEducator; @@ -109,6 +110,7 @@ public static Collection createFeatures(JDA jda, Database database, Con .add(new AutoPruneHelperRoutine(config, helpSystemHelper, modAuditLogWriter, database)); features.add(new HelpThreadAutoArchiver(helpSystemHelper)); features.add(new LeftoverBookmarksCleanupRoutine(bookmarksSystem)); + features.add(new MemberCountDisplayRoutine(config)); // Message receivers features.add(new TopHelpersMessageListener(database, config)); diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/MemberCountDisplayRoutine.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/MemberCountDisplayRoutine.java new file mode 100644 index 0000000000..bb67396670 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/MemberCountDisplayRoutine.java @@ -0,0 +1,53 @@ +package org.togetherjava.tjbot.features.basic; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.entities.channel.concrete.Category; + +import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.features.Routine; + +import java.util.concurrent.TimeUnit; +import java.util.function.Predicate; +import java.util.regex.Pattern; + +/** + * Shows the guild member count on selected category, which updates everyday. + */ +public class MemberCountDisplayRoutine implements Routine { + private final Predicate memberCountCategoryPredicate; + + /** + * Creates an instance on member count display routine. + * + * @param config the config to use + */ + public MemberCountDisplayRoutine(Config config) { + memberCountCategoryPredicate = + Pattern.compile(config.getMemberCountCategoryPattern() + "( - \\d+ Members)?") + .asMatchPredicate(); + } + + private void updateCategoryName(Category category) { + int totalMemberCount = category.getGuild().getMemberCount(); + String baseName = category.getName().split("-")[0].trim(); + + category.getManager() + .setName("%s - %d Members".formatted(baseName, totalMemberCount)) + .queue(); + } + + @Override + public Schedule createSchedule() { + return new Schedule(ScheduleMode.FIXED_RATE, 0, 24, TimeUnit.HOURS); + } + + @Override + public void runRoutine(JDA jda) { + jda.getGuilds() + .forEach(guild -> guild.getCategories() + .stream() + .filter(category -> memberCountCategoryPredicate.test(category.getName())) + .findAny() + .ifPresent(this::updateCategoryName)); + } +}