Skip to content

Commit 96a8af7

Browse files
Started work on refactoring the NotificationService
1 parent 6b15baf commit 96a8af7

File tree

5 files changed

+120
-21
lines changed

5 files changed

+120
-21
lines changed

src/main/java/net/javadiscord/javabot/systems/notification/GuildNotificationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Sends notifications within a single {@link Guild}.
1111
*/
1212
@Slf4j
13-
public final class GuildNotificationService extends NotificationService {
13+
public final class GuildNotificationService extends NotificationServiceDEPRECATED {
1414

1515
private final Guild guild;
1616
private final GuildConfig config;
Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,99 @@
11
package net.javadiscord.javabot.systems.notification;
22

3+
import lombok.RequiredArgsConstructor;
34
import lombok.extern.slf4j.Slf4j;
5+
import net.dv8tion.jda.api.entities.Guild;
46
import net.dv8tion.jda.api.entities.MessageChannel;
5-
import net.dv8tion.jda.api.entities.MessageEmbed;
67
import net.dv8tion.jda.api.entities.User;
8+
import net.dv8tion.jda.api.requests.restaction.MessageAction;
9+
import net.javadiscord.javabot.Bot;
10+
import net.javadiscord.javabot.util.ExceptionLogger;
11+
import org.jetbrains.annotations.Contract;
12+
import org.jetbrains.annotations.NotNull;
713

14+
import java.util.function.Function;
815

916
/**
10-
* Abstract class used for sending Notifications to Discord Users and Channels.
17+
* Handles all types of guild & user notifications.
1118
*/
12-
@Slf4j
13-
public abstract class NotificationService {
14-
void sendDirectMessageNotification(User user, MessageEmbed message) {
15-
user.openPrivateChannel().queue(
16-
channel -> sendMessageChannelNotification(channel, message),
17-
error -> log.warn("Could not send private Notification to User " + user.getAsTag())
18-
);
19+
public final class NotificationService {
20+
private NotificationService() {}
21+
22+
@Contract("_ -> new")
23+
public static @NotNull GuildNotificationService of(Guild guild) {
24+
return new GuildNotificationService(guild);
25+
}
26+
27+
@Contract("_ -> new")
28+
public static @NotNull UserNotificationService of(User user) {
29+
return new UserNotificationService(user);
1930
}
2031

21-
void sendDirectMessageNotification(User user, String s, Object... args) {
22-
user.openPrivateChannel().queue(
23-
channel -> sendMessageChannelNotification(channel, s, args),
24-
error -> log.warn("Could not send private Notification to User " + user.getAsTag())
25-
);
32+
/**
33+
* Handles all sorts of guild notifications.
34+
*/
35+
@Slf4j
36+
@RequiredArgsConstructor
37+
public static final class GuildNotificationService extends MessageChannelNotification {
38+
private final Guild guild;
39+
40+
/**
41+
* Sends a notification to the log channel.
42+
*
43+
* @param function The {@link Function} to use which MUST return a {@link MessageAction}.
44+
*/
45+
public void sendToModerationLog(@NotNull Function<MessageChannel, MessageAction> function) {
46+
MessageChannel channel = Bot.getConfig().get(guild).getModerationConfig().getLogChannel();
47+
if (channel == null) {
48+
log.error("Could not send message to LogChannel in guild " + guild.getId());
49+
return;
50+
}
51+
send(channel, function);
52+
}
53+
54+
/**
55+
* Sends a notification to the message cache log channel.
56+
*
57+
* @param function The {@link Function} to use which MUST return a {@link MessageAction}.
58+
*/
59+
public void sendToMessageLog(@NotNull Function<MessageChannel, MessageAction> function) {
60+
MessageChannel channel = Bot.getConfig().get(guild).getMessageCacheConfig().getMessageCacheLogChannel();
61+
if (channel == null) {
62+
log.error("Could not find MessageCacheLogChannel in guild " + guild.getId());
63+
return;
64+
}
65+
send(channel, function);
66+
}
2667
}
2768

28-
void sendMessageChannelNotification(MessageChannel channel, MessageEmbed message) {
29-
channel.sendMessageEmbeds(message).queue(s -> {}, e -> log.warn("Could not send embed to channel " + channel.getName()));
69+
/**
70+
* Handles all sorts of user notifications.
71+
*/
72+
@Slf4j
73+
@RequiredArgsConstructor
74+
public static final class UserNotificationService extends MessageChannelNotification {
75+
private final User user;
76+
77+
/**
78+
* Sends a notification to a {@link User}s' {@link net.dv8tion.jda.api.entities.PrivateChannel}.
79+
*
80+
* @param function The {@link Function} to use which MUST return a {@link MessageAction}.
81+
*/
82+
public void sendDirectMessage(@NotNull Function<MessageChannel, MessageAction> function) {
83+
user.openPrivateChannel().queue(
84+
channel -> send(channel, function),
85+
error -> log.error("Could not open PrivateChannel with user " + user.getAsTag(), error)
86+
);
87+
}
3088
}
3189

32-
void sendMessageChannelNotification(MessageChannel channel, String s, Object... args) {
33-
channel.sendMessageFormat(s, args).queue(success -> {}, e -> log.warn("Could not send message to channel " + channel.getName()));
90+
@Slf4j
91+
private abstract static class MessageChannelNotification {
92+
protected void send(MessageChannel channel, @NotNull Function<MessageChannel, MessageAction> function) {
93+
function.apply(channel).queue(s -> {}, err -> {
94+
ExceptionLogger.capture(err, getClass().getSimpleName());
95+
log.error("Could not send message to channel \" " + channel.getName() + "\": ", err);
96+
});
97+
}
3498
}
3599
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.javadiscord.javabot.systems.notification;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import net.dv8tion.jda.api.entities.MessageChannel;
5+
import net.dv8tion.jda.api.entities.MessageEmbed;
6+
import net.dv8tion.jda.api.entities.User;
7+
8+
9+
/**
10+
* Abstract class used for sending Notifications to Discord Users and Channels.
11+
*/
12+
@Slf4j
13+
public abstract class NotificationServiceDEPRECATED {
14+
void sendDirectMessageNotification(User user, MessageEmbed message) {
15+
user.openPrivateChannel().queue(
16+
channel -> sendMessageChannelNotification(channel, message),
17+
error -> log.warn("Could not send private Notification to User " + user.getAsTag())
18+
);
19+
}
20+
21+
void sendDirectMessageNotification(User user, String s, Object... args) {
22+
user.openPrivateChannel().queue(
23+
channel -> sendMessageChannelNotification(channel, s, args),
24+
error -> log.warn("Could not send private Notification to User " + user.getAsTag())
25+
);
26+
}
27+
28+
void sendMessageChannelNotification(MessageChannel channel, MessageEmbed message) {
29+
channel.sendMessageEmbeds(message).queue(s -> {}, e -> log.warn("Could not send embed to channel " + channel.getName()));
30+
}
31+
32+
void sendMessageChannelNotification(MessageChannel channel, String s, Object... args) {
33+
channel.sendMessageFormat(s, args).queue(success -> {}, e -> log.warn("Could not send message to channel " + channel.getName()));
34+
}
35+
}

src/main/java/net/javadiscord/javabot/systems/notification/QOTWNotificationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* Sends Notifications regarding QOTW.
2727
*/
2828
@Slf4j
29-
public final class QOTWNotificationService extends NotificationService {
29+
public final class QOTWNotificationService extends NotificationServiceDEPRECATED {
3030

3131
@Nullable
3232
private final User user;

src/main/java/net/javadiscord/javabot/systems/notification/UserNotificationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
@Slf4j
1212
@RequiredArgsConstructor
13-
public final class UserNotificationService extends NotificationService {
13+
public final class UserNotificationService extends NotificationServiceDEPRECATED {
1414
private final User user;
1515

1616
/**

0 commit comments

Comments
 (0)