Skip to content

Commit f5c25d0

Browse files
committed
added overloads to FreeChannelMonitor - might revert!
1 parent bd10419 commit f5c25d0

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

application/src/main/java/org/togetherjava/tjbot/commands/free/FreeChannelMonitor.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ public boolean isMonitoringChannel(final long channelId) {
9797
return channelsToMonitorById.containsKey(channelId);
9898
}
9999

100+
/**
101+
* This method tests whether a channel id is configured for monitoring in the free command
102+
* system. To add a channel for monitoring see
103+
* {@link org.togetherjava.tjbot.config.FreeCommandConfig} or
104+
* {@link #addChannelToMonitor(long)}.
105+
*
106+
* @param channel the channel to test.
107+
* @return {@code true} if the channel is configured in the system, {@code false} otherwise.
108+
*/
109+
public boolean isMonitoringChannel(@NotNull TextChannel channel) {
110+
return isMonitoringChannel(channel.getIdLong());
111+
}
112+
100113
private ChannelStatus requiresIsMonitored(final long channelId) {
101114
if (!channelsToMonitorById.containsKey(channelId)) {
102115
throw new IllegalArgumentException(
@@ -117,6 +130,18 @@ public boolean isChannelBusy(final long channelId) {
117130
return requiresIsMonitored(channelId).isBusy();
118131
}
119132

133+
/**
134+
* This method tests if channel status to busy, see {@link ChannelStatus#isBusy()} for details.
135+
*
136+
* @param channel the channel to test.
137+
* @return {@code true} if the channel is 'busy', false if the channel is 'free'.
138+
* @throws IllegalArgumentException if the channel passed is not monitored. See
139+
* {@link #addChannelToMonitor(long)}
140+
*/
141+
public boolean isChannelBusy(@NotNull TextChannel channel) {
142+
return isChannelBusy(channel.getIdLong());
143+
}
144+
120145
/**
121146
* This method tests if a channel is currently active by fetching the latest message and testing
122147
* if it was posted more recently than the configured time limit, see
@@ -170,6 +195,18 @@ public void setChannelFree(final long channelId) {
170195
requiresIsMonitored(channelId).setFree();
171196
}
172197

198+
/**
199+
* This method sets the channel's status to 'free', see {@link ChannelStatus#setFree()} for
200+
* details.
201+
*
202+
* @param channel the channel status to modify.
203+
* @throws IllegalArgumentException if the channel passed is not monitored. See
204+
* {@link #addChannelToMonitor(long)}
205+
*/
206+
public void setChannelFree(@NotNull TextChannel channel) {
207+
setChannelFree(channel.getIdLong());
208+
}
209+
173210
/**
174211
* This method provides a stream of the id's for guilds that are currently being monitored. This
175212
* is streamed purely as a simple method of encapsulation.
@@ -338,6 +375,23 @@ public void displayStatus(@NotNull TextChannel channel) {
338375
}
339376
}
340377

378+
/**
379+
* Displays the message that will be displayed for users.
380+
* <p>
381+
* This method detects if any messages have been posted in the channel below the status message.
382+
* If that is the case this will delete the existing status message and post another one so that
383+
* it's the last message in the channel.
384+
* <p>
385+
* If it cannot find an existing status message it will create a new one.
386+
* <p>
387+
* Otherwise it will edit the existing message.
388+
*
389+
* @param guild the guild the status message will be posted in.
390+
*/
391+
public void displayStatus(@NotNull Guild guild) {
392+
displayStatus(getStatusChannelFor(guild));
393+
}
394+
341395
private @NotNull Optional<Message> deleteIfNotLatest(@NotNull Message message) {
342396

343397
OptionalLong lastId = FreeUtil.getLastMessageId(message.getTextChannel());

application/src/main/java/org/togetherjava/tjbot/commands/free/FreeHelpChannelsRoutine.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ public FreeHelpChannelsRoutine(@NotNull FreeChannelMonitor freeChannelMonitor) {
3434
public void runRoutine(@NotNull JDA jda) {
3535
jda.getTextChannels()
3636
.stream()
37-
.filter(channel -> freeChannelMonitor.isMonitoringChannel(channel.getIdLong()))
38-
.filter(channel -> freeChannelMonitor.isChannelBusy(channel.getIdLong()))
37+
.filter(freeChannelMonitor::isMonitoringChannel)
38+
.filter(freeChannelMonitor::isChannelBusy)
3939
.filter(channel -> TimeUtil.getTimeCreated(channel.getLatestMessageIdLong())
4040
.isBefore(OffsetDateTime.now().minusMinutes(INACTIVE_TIME_MINUTES)))
4141
.forEach(channel -> {
42-
freeChannelMonitor.setChannelFree(channel.getIdLong());
43-
freeChannelMonitor
44-
.displayStatus(freeChannelMonitor.getStatusChannelFor(channel.getGuild()));
42+
freeChannelMonitor.setChannelFree(channel);
43+
freeChannelMonitor.displayStatus(channel.getGuild());
4544

4645
channel.sendMessage(
4746
"This channel was automatically marked as free because it was inactive for more than 2 hours.")

0 commit comments

Comments
 (0)