Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Member Count Display #1038

Merged
merged 12 commits into from
Mar 13, 2024
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 {
christolis marked this conversation as resolved.
Show resolved Hide resolved
private final Predicate<String> memberCountCategoryPredicate;

/**
* Creates an instance on member count display routine.
*
* @param config the config to use
*/
public MemberCountDisplayRoutine(Config config) {
christolis marked this conversation as resolved.
Show resolved Hide resolved
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) {
Taz03 marked this conversation as resolved.
Show resolved Hide resolved
jda.getGuilds()
.forEach(guild -> guild.getCategories()
.stream()
.filter(category -> memberCountCategoryPredicate.test(category.getName()))
.findAny()
.ifPresent(this::updateCategoryName));
}
}
Loading