-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
6dd670a
commit 5ac1e07
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,5 +110,6 @@ | |
"special": [ | ||
] | ||
}, | ||
"memberCountCategoryPattern": "Info", | ||
"selectRolesChannelPattern": "select-your-roles" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...cation/src/main/java/org/togetherjava/tjbot/features/basic/MemberCountDisplayRoutine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |