Skip to content

Commit e29b124

Browse files
committed
Added channel find methods to Guilds helper (findTextChannel etc)
1 parent 9fadf60 commit e29b124

5 files changed

Lines changed: 47 additions & 25 deletions

File tree

application/src/main/java/org/togetherjava/tjbot/features/help/HelpSystemHelper.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,7 @@ Optional<ForumChannel> handleRequireHelpForum(Guild guild,
361361
Predicate<String> isChannelName = this::isHelpForumName;
362362
String channelPattern = getHelpForumPattern();
363363

364-
Optional<ForumChannel> maybeChannel = guild.getForumChannelCache()
365-
.stream()
366-
.filter(channel -> isChannelName.test(channel.getName()))
367-
.findAny();
368-
364+
Optional<ForumChannel> maybeChannel = Guilds.findForumChannel(guild, isChannelName);
369365
if (maybeChannel.isEmpty()) {
370366
consumeChannelPatternIfNotFound.accept(channelPattern);
371367
}

application/src/main/java/org/togetherjava/tjbot/features/moderation/audit/ModAuditLogWriter.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.slf4j.LoggerFactory;
1111

1212
import org.togetherjava.tjbot.config.Config;
13+
import org.togetherjava.tjbot.features.utils.Guilds;
1314

1415
import javax.annotation.Nullable;
1516

@@ -34,7 +35,7 @@ public final class ModAuditLogWriter {
3435

3536
private final Config config;
3637

37-
private final Predicate<String> auditLogChannelNamePredicate;
38+
private final Predicate<String> isAuditLogChannelName;
3839

3940
/**
4041
* Creates a new instance.
@@ -43,7 +44,7 @@ public final class ModAuditLogWriter {
4344
*/
4445
public ModAuditLogWriter(Config config) {
4546
this.config = config;
46-
auditLogChannelNamePredicate =
47+
isAuditLogChannelName =
4748
Pattern.compile(config.getModAuditLogChannelPattern()).asMatchPredicate();
4849
}
4950

@@ -90,11 +91,8 @@ public void write(String title, String description, @Nullable User author,
9091
* @return the channel used for moderation audit logs, if present
9192
*/
9293
public Optional<TextChannel> getAndHandleModAuditLogChannel(Guild guild) {
93-
Optional<TextChannel> auditLogChannel = guild.getTextChannelCache()
94-
.stream()
95-
.filter(channel -> auditLogChannelNamePredicate.test(channel.getName()))
96-
.findAny();
97-
94+
Optional<TextChannel> auditLogChannel =
95+
Guilds.findTextChannel(guild, isAuditLogChannelName);
9896
if (auditLogChannel.isEmpty()) {
9997
logger.warn(
10098
"Unable to log moderation events, did not find a mod audit log channel matching the configured pattern '{}' for guild '{}'",

application/src/main/java/org/togetherjava/tjbot/features/moderation/scam/ScamBlocker.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public final class ScamBlocker extends MessageReceiverAdapter implements UserInt
6666
private final ScamBlockerConfig.Mode mode;
6767
private final String reportChannelPattern;
6868
private final String botTrapChannelPattern;
69-
private final Predicate<TextChannel> isReportChannel;
69+
private final Predicate<String> isReportChannelName;
7070
private final Predicate<TextChannel> isBotTrapChannel;
7171
private final ScamDetector scamDetector;
7272
private final Config config;
@@ -92,9 +92,7 @@ public ScamBlocker(ModerationActionsStore actionsStore, ScamHistoryStore scamHis
9292
scamDetector = new ScamDetector(config);
9393

9494
reportChannelPattern = config.getScamBlocker().getReportChannelPattern();
95-
Predicate<String> isReportChannelName =
96-
Pattern.compile(reportChannelPattern).asMatchPredicate();
97-
isReportChannel = channel -> isReportChannelName.test(channel.getName());
95+
isReportChannelName = Pattern.compile(reportChannelPattern).asMatchPredicate();
9896

9997
botTrapChannelPattern = config.getScamBlocker().getBotTrapChannelPattern();
10098
Predicate<String> isBotTrapChannelName =
@@ -297,7 +295,7 @@ If you think this was a mistake (for example, your account was hacked, but you g
297295
}
298296

299297
private Optional<TextChannel> getReportChannel(Guild guild) {
300-
return guild.getTextChannelCache().stream().filter(isReportChannel).findAny();
298+
return Guilds.findTextChannel(guild, isReportChannelName);
301299
}
302300

303301
private List<Button> createConfirmDialog(MessageReceivedEvent event) {

application/src/main/java/org/togetherjava/tjbot/features/tophelper/TopHelpersAssignmentRoutine.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,8 @@ private static boolean isFirstDayOfMonth() {
131131
* @param guild to start the dialog and compute Top Helpers for
132132
*/
133133
public void startDialogFor(Guild guild) {
134-
Optional<TextChannel> assignmentChannel = guild.getTextChannelCache()
135-
.stream()
136-
.filter(channel -> assignmentChannelNamePredicate.test(channel.getName()))
137-
.findAny();
134+
Optional<TextChannel> assignmentChannel =
135+
Guilds.findTextChannel(guild, assignmentChannelNamePredicate);
138136

139137
assignmentChannel.ifPresentOrElse(this::startDialogIn, () -> logger.warn(
140138
"Unable to assign Top Helpers, did not find an assignment channel matching the configured pattern '{}' for guild '{}'",
@@ -313,10 +311,8 @@ private void prepareAnnouncement(ButtonInteractionEvent event, List<String> args
313311

314312
private void postAnnouncement(ButtonInteractionEvent event, List<? extends Member> topHelpers) {
315313
Guild guild = Objects.requireNonNull(event.getGuild());
316-
Optional<TextChannel> announcementChannel = guild.getTextChannelCache()
317-
.stream()
318-
.filter(channel -> announcementChannelNamePredicate.test(channel.getName()))
319-
.findAny();
314+
Optional<TextChannel> announcementChannel =
315+
Guilds.findTextChannel(guild, announcementChannelNamePredicate);
320316

321317
if (announcementChannel.isEmpty()) {
322318
logger.warn(

application/src/main/java/org/togetherjava/tjbot/features/utils/Guilds.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import net.dv8tion.jda.api.entities.Guild;
44
import net.dv8tion.jda.api.entities.Member;
55
import net.dv8tion.jda.api.entities.Role;
6+
import net.dv8tion.jda.api.entities.channel.attribute.IGuildChannelContainer;
7+
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
8+
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
9+
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
610

711
import java.util.Optional;
812
import java.util.function.Predicate;
@@ -53,4 +57,34 @@ public static boolean doesMemberNotHaveRole(Member member,
5357
Predicate<? super String> isRoleName) {
5458
return member.getRoles().stream().map(Role::getName).noneMatch(isRoleName);
5559
}
60+
61+
/**
62+
* Finds any text channel in the guild whose name matches the given predicate.
63+
*
64+
* @param guild guild to search the channel in
65+
* @param isChannelName a predicate matching the name of the text channel to search
66+
* @return the matched text channel, if any
67+
*/
68+
public static Optional<TextChannel> findTextChannel(IGuildChannelContainer<GuildChannel> guild,
69+
Predicate<? super String> isChannelName) {
70+
return guild.getTextChannelCache()
71+
.stream()
72+
.filter(channel -> isChannelName.test(channel.getName()))
73+
.findAny();
74+
}
75+
76+
/**
77+
* Finds any forum channel in the guild whose name matches the given predicate.
78+
*
79+
* @param guild guild to search the channel in
80+
* @param isChannelName a predicate matching the name of the forum channel to search
81+
* @return the matched forum channel, if any
82+
*/
83+
public static Optional<ForumChannel> findForumChannel(
84+
IGuildChannelContainer<GuildChannel> guild, Predicate<? super String> isChannelName) {
85+
return guild.getForumChannelCache()
86+
.stream()
87+
.filter(channel -> isChannelName.test(channel.getName()))
88+
.findAny();
89+
}
5690
}

0 commit comments

Comments
 (0)