Skip to content

Commit

Permalink
Member Count Display (#1038)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Mar 13, 2024
1 parent 6dd670a commit 5ac1e07
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions application/config.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@
"special": [
]
},
"memberCountCategoryPattern": "Info",
"selectRolesChannelPattern": "select-your-roles"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -109,6 +110,7 @@ public static Collection<Feature> 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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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));
}
}

0 comments on commit 5ac1e07

Please sign in to comment.