|
1 | 1 | package net.javadiscord.javabot.systems.notification; |
2 | 2 |
|
| 3 | +import lombok.RequiredArgsConstructor; |
3 | 4 | import lombok.extern.slf4j.Slf4j; |
| 5 | +import net.dv8tion.jda.api.entities.Guild; |
4 | 6 | import net.dv8tion.jda.api.entities.MessageChannel; |
5 | | -import net.dv8tion.jda.api.entities.MessageEmbed; |
6 | 7 | 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; |
7 | 13 |
|
| 14 | +import java.util.function.Function; |
8 | 15 |
|
9 | 16 | /** |
10 | | - * Abstract class used for sending Notifications to Discord Users and Channels. |
| 17 | + * Handles all types of guild & user notifications. |
11 | 18 | */ |
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); |
19 | 30 | } |
20 | 31 |
|
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 | + } |
26 | 67 | } |
27 | 68 |
|
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 | + } |
30 | 88 | } |
31 | 89 |
|
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 | + } |
34 | 98 | } |
35 | 99 | } |
0 commit comments