Skip to content

Commit fd2cdae

Browse files
committed
ReminderRoute factories for readability
CR by Tais
1 parent 932da35 commit fd2cdae

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

application/src/main/java/org/togetherjava/tjbot/commands/reminder/RemindRoutine.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import net.dv8tion.jda.api.EmbedBuilder;
44
import net.dv8tion.jda.api.JDA;
5-
import net.dv8tion.jda.api.entities.MessageChannel;
6-
import net.dv8tion.jda.api.entities.MessageEmbed;
7-
import net.dv8tion.jda.api.entities.TextChannel;
8-
import net.dv8tion.jda.api.entities.User;
5+
import net.dv8tion.jda.api.entities.*;
96
import net.dv8tion.jda.api.requests.RestAction;
107
import net.dv8tion.jda.api.requests.restaction.MessageAction;
118
import org.jetbrains.annotations.NotNull;
@@ -60,6 +57,7 @@ public void runRoutine(@NotNull JDA jda) {
6057
sendReminder(jda, pendingReminder.getId(), pendingReminder.getChannelId(),
6158
pendingReminder.getAuthorId(), pendingReminder.getContent(),
6259
pendingReminder.getCreatedAt());
60+
6361
pendingReminder.delete();
6462
}));
6563
}
@@ -86,16 +84,13 @@ private static RestAction<ReminderRoute> computeReminderRoute(@NotNull JDA jda,
8684
long authorId, @NotNull TextChannel channel) {
8785
return jda.retrieveUserById(authorId)
8886
.onErrorMap(error -> null)
89-
.map(author -> new ReminderRoute(channel, author,
90-
author == null ? null : author.getAsMention()));
87+
.map(author -> ReminderRoute.toPublic(channel, author));
9188
}
9289

9390
private static @NotNull RestAction<ReminderRoute> createDmReminderRoute(@NotNull JDA jda,
9491
long authorId) {
9592
return jda.openPrivateChannelById(authorId)
96-
.map(channel -> new ReminderRoute(channel, channel.getUser(),
97-
"(Sending your reminder directly, because I was unable to"
98-
+ " locate the original channel you wanted it to be send to)"));
93+
.map(channel -> ReminderRoute.toPrivate(channel, channel.getUser()));
9994
}
10095

10196
private static void sendReminderViaRoute(@NotNull RestAction<ReminderRoute> routeAction,
@@ -133,5 +128,15 @@ private static void sendReminderViaRoute(@NotNull RestAction<ReminderRoute> rout
133128

134129
private record ReminderRoute(@NotNull MessageChannel channel, @Nullable User target,
135130
@Nullable String description) {
131+
static ReminderRoute toPublic(@NotNull TextChannel channel, @Nullable User target) {
132+
return new ReminderRoute(channel, target,
133+
target == null ? null : target.getAsMention());
134+
}
135+
136+
static ReminderRoute toPrivate(@NotNull PrivateChannel channel, @NotNull User target) {
137+
return new ReminderRoute(channel, target,
138+
"(Sending your reminder directly, because I was unable to locate"
139+
+ " the original channel you wanted it to be send to)");
140+
}
136141
}
137142
}

0 commit comments

Comments
 (0)