From d18a0010cc584b1c8286b115267d3051d00d6818 Mon Sep 17 00:00:00 2001 From: sciwhiz12 Date: Mon, 30 Nov 2020 14:01:41 +0800 Subject: [PATCH] Refactor reaction messages to allow full control of when to send the message --- .../janitor/api/messages/ListingMessage.java | 9 +- .../api/messages/emote/ReactionMessage.java | 4 +- .../janitor/commands/CmdListCommand.java | 3 +- .../janitor/commands/misc/HelloCommand.java | 23 ++--- .../messages/ListingMessageBuilder.java | 86 +++++++++++-------- .../messages/emote/ReactionManagerImpl.java | 19 +++- .../messages/emote/ReactionMessageImpl.java | 33 +++---- .../janitor/moderation/notes/NoteCommand.java | 3 +- .../moderation/warns/WarnListCommand.java | 3 +- 9 files changed, 108 insertions(+), 75 deletions(-) diff --git a/core/src/api/java/sciwhiz12/janitor/api/messages/ListingMessage.java b/core/src/api/java/sciwhiz12/janitor/api/messages/ListingMessage.java index a343c95..45cecbf 100644 --- a/core/src/api/java/sciwhiz12/janitor/api/messages/ListingMessage.java +++ b/core/src/api/java/sciwhiz12/janitor/api/messages/ListingMessage.java @@ -3,6 +3,7 @@ package sciwhiz12.janitor.api.messages; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.MessageChannel; import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.requests.RestAction; import sciwhiz12.janitor.api.JanitorBot; import sciwhiz12.janitor.api.messages.substitution.ModifiableSubstitutions; import sciwhiz12.janitor.api.messages.substitution.ModifiableSubstitutor; @@ -191,13 +192,15 @@ public class ListingMessage { Builder with(final String argument, final Supplier value); - void build(MessageChannel channel, + Builder deletionReaction(final boolean addDeletionReaction); + + RestAction build(MessageChannel channel, SubstitutionsMap globalSubstitutions, Message triggerMessage, List entries); - default void build(MessageChannel channel, JanitorBot bot, Message triggerMessage, List entries) { - build(channel, bot.getSubstitutions(), triggerMessage, entries); + default RestAction build(MessageChannel channel, JanitorBot bot, Message triggerMessage, List entries) { + return build(channel, bot.getSubstitutions(), triggerMessage, entries); } } } diff --git a/core/src/api/java/sciwhiz12/janitor/api/messages/emote/ReactionMessage.java b/core/src/api/java/sciwhiz12/janitor/api/messages/emote/ReactionMessage.java index 0e5bf8c..fcf001e 100644 --- a/core/src/api/java/sciwhiz12/janitor/api/messages/emote/ReactionMessage.java +++ b/core/src/api/java/sciwhiz12/janitor/api/messages/emote/ReactionMessage.java @@ -1,8 +1,10 @@ package sciwhiz12.janitor.api.messages.emote; import net.dv8tion.jda.api.entities.Emote; +import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.MessageReaction.ReactionEmote; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; +import net.dv8tion.jda.api.requests.RestAction; import sciwhiz12.janitor.api.JanitorBot; import java.util.Map; @@ -19,7 +21,7 @@ public interface ReactionMessage { ReactionMessage owner(long ownerID); - void create(); + RestAction create(Message message); long getOwnerID(); diff --git a/core/src/main/java/sciwhiz12/janitor/commands/CmdListCommand.java b/core/src/main/java/sciwhiz12/janitor/commands/CmdListCommand.java index 76679a2..f851e73 100644 --- a/core/src/main/java/sciwhiz12/janitor/commands/CmdListCommand.java +++ b/core/src/main/java/sciwhiz12/janitor/commands/CmdListCommand.java @@ -22,7 +22,8 @@ public class CmdListCommand extends BaseCommand { .with("commands_prefix", () -> config(ctx.getSource()).forGuild(COMMAND_PREFIX)) .setEntryApplier((command, subs) -> subs.with("command", () -> command)) .build(ctx.getSource().getChannel(), getBot(), ctx.getSource().getMessage(), - Lists.newArrayList(getRegistry().registry.keySet())); + Lists.newArrayList(getRegistry().registry.keySet())) + .queue(); return 1; }); } diff --git a/core/src/main/java/sciwhiz12/janitor/commands/misc/HelloCommand.java b/core/src/main/java/sciwhiz12/janitor/commands/misc/HelloCommand.java index 33f9fe7..0303146 100644 --- a/core/src/main/java/sciwhiz12/janitor/commands/misc/HelloCommand.java +++ b/core/src/main/java/sciwhiz12/janitor/commands/misc/HelloCommand.java @@ -34,22 +34,23 @@ public class HelloCommand extends BaseCommand { final List memberList = getMembers("member", ctx).fromGuild(ctx.getSource().getGuild()); if (memberList.size() == 1) { final Member member = memberList.get(0); - ctx.getSource().getChannel().sendMessage("Hello " + member.getAsMention() + "!").queue( - success -> { - JANITOR.debug("Sent greeting message to {}, on cmd of {}", Util.toString(member.getUser()), - Util.toString(ctx.getSource().getAuthor())); - getBot().getReactions().newMessage(success) - .add("\u274C", (msg, event) -> success.delete() + ctx.getSource().getChannel().sendMessage("Hello " + member.getAsMention() + "!") + .flatMap(message -> + getBot().getReactions().newMessage(message) + .add("\u274C", (msg, event) -> message.delete() .flatMap(v -> event.getChannel() .deleteMessageById(ctx.getSource().getMessageIdLong())) .queue() ) .owner(ctx.getSource().getAuthor().getIdLong()) - .create(); - }, - err -> JANITOR.error("Error while sending greeting message to {}, on cmd of {}", - Util.toString(member.getUser()), Util.toString(ctx.getSource().getAuthor())) - ); + .create(message) + ) + .queue( + success -> JANITOR.debug("Sent greeting message to {}, on cmd of {}", Util.toString(member.getUser()), + Util.toString(ctx.getSource().getAuthor())), + err -> JANITOR.error("Error while sending greeting message to {}, on cmd of {}", + Util.toString(member.getUser()), Util.toString(ctx.getSource().getAuthor())) + ); } } return 1; diff --git a/core/src/main/java/sciwhiz12/janitor/messages/ListingMessageBuilder.java b/core/src/main/java/sciwhiz12/janitor/messages/ListingMessageBuilder.java index b51daca..e3375b2 100644 --- a/core/src/main/java/sciwhiz12/janitor/messages/ListingMessageBuilder.java +++ b/core/src/main/java/sciwhiz12/janitor/messages/ListingMessageBuilder.java @@ -9,8 +9,9 @@ import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.MessageChannel; import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.Role; -import sciwhiz12.janitor.api.JanitorBot; +import net.dv8tion.jda.api.requests.RestAction; import sciwhiz12.janitor.api.messages.ListingMessage; +import sciwhiz12.janitor.api.messages.emote.ReactionMessage; import sciwhiz12.janitor.api.messages.substitution.ModifiableSubstitutor; import sciwhiz12.janitor.api.messages.substitution.SubstitutionsMap; import sciwhiz12.janitor.api.messages.substitution.Substitutor; @@ -31,6 +32,7 @@ public class ListingMessageBuilder implements ListingMessage.Builder { private final ListingMessage message; private final Map> customSubstitutions; private int amountPerPage = 6; + private boolean addDeletionReaction = true; private BiConsumer> entryApplier = (entry, sub) -> {}; public ListingMessageBuilder(ListingMessage message, Map> customSubstitutions) { @@ -62,7 +64,12 @@ public class ListingMessageBuilder implements ListingMessage.Builder { return this; } - public void build(MessageChannel channel, + public ListingMessageBuilder deletionReaction(final boolean addDeletionReaction) { + this.addDeletionReaction = addDeletionReaction; + return this; + } + + public RestAction build(MessageChannel channel, SubstitutionsMap globalSubstitutions, Message triggerMessage, List entries) { @@ -71,43 +78,46 @@ public class ListingMessageBuilder implements ListingMessage.Builder { final ImmutableList list = ImmutableList.copyOf(entries); final PagedMessage pagedMessage = new PagedMessage(message, list, amountPerPage); - channel.sendMessage(pagedMessage.createMessage(customSubs, entryApplier)) - .queue(listMsg -> globalSubstitutions.getBot().getReactions().newMessage(listMsg) - .owner(triggerMessage.getAuthor().getIdLong()) - .removeEmotes(true) - .add("\u2b05", (msg, event) -> { // PREVIOUS - if (pagedMessage.advancePage(PageDirection.PREVIOUS)) { - event.retrieveMessage() - .flatMap(eventMsg -> eventMsg.editMessage( - pagedMessage.createMessage(customSubs, entryApplier)) - ) - .queue(); - } - }) - .add("\u274c", (msg, event) -> { // CLOSE - event.getChannel().deleteMessageById(event.getMessageIdLong()) - .flatMap(v -> !triggerMessage.isFromGuild() || - event.getGuild().getSelfMember() - .hasPermission(triggerMessage.getTextChannel(), - Permission.MESSAGE_MANAGE), - v -> triggerMessage.delete()) - .queue(); - }) - .add("\u27a1", (msg, event) -> { // NEXT - if (pagedMessage.advancePage(PageDirection.NEXT)) { - event.retrieveMessage() - .flatMap(eventMsg -> eventMsg.editMessage( - pagedMessage.createMessage(customSubs, entryApplier)) - ) - .queue(); - } - }) - .create() - ); - } + return channel.sendMessage(pagedMessage.createMessage(customSubs, entryApplier)) + .flatMap(listMsg -> { + ReactionMessage reactionMsg = globalSubstitutions.getBot().getReactions().newMessage(listMsg) + .owner(triggerMessage.getAuthor().getIdLong()) + .removeEmotes(true) + .add("\u2b05", (msg, event) -> { // PREVIOUS + if (pagedMessage.advancePage(PageDirection.PREVIOUS)) { + event.retrieveMessage() + .flatMap(eventMsg -> eventMsg.editMessage( + pagedMessage.createMessage(customSubs, entryApplier)) + ) + .queue(); + } + }); - public void build(MessageChannel channel, JanitorBot bot, Message triggerMessage, List entries) { - build(channel, bot.getSubstitutions(), triggerMessage, entries); + if (addDeletionReaction) { + reactionMsg.add("\u274c", (msg, event) -> { // CLOSE + event.getChannel().deleteMessageById(event.getMessageIdLong()) + .flatMap(v -> !triggerMessage.isFromGuild() || + event.getGuild().getSelfMember() + .hasPermission(triggerMessage.getTextChannel(), + Permission.MESSAGE_MANAGE), + v -> triggerMessage.delete()) + .queue(); + }); + } + + reactionMsg.add("\u27a1", (msg, event) -> { // NEXT + if (pagedMessage.advancePage(PageDirection.NEXT)) { + event.retrieveMessage() + .flatMap(eventMsg -> eventMsg.editMessage( + pagedMessage.createMessage(customSubs, entryApplier)) + ) + .queue(); + } + }); + + return reactionMsg.create(listMsg); + } + ); } class PagedMessage { diff --git a/core/src/main/java/sciwhiz12/janitor/messages/emote/ReactionManagerImpl.java b/core/src/main/java/sciwhiz12/janitor/messages/emote/ReactionManagerImpl.java index edb37e7..3f2acb6 100644 --- a/core/src/main/java/sciwhiz12/janitor/messages/emote/ReactionManagerImpl.java +++ b/core/src/main/java/sciwhiz12/janitor/messages/emote/ReactionManagerImpl.java @@ -2,18 +2,21 @@ package sciwhiz12.janitor.messages.emote; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.events.message.MessageDeleteEvent; +import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import sciwhiz12.janitor.JanitorBotImpl; import sciwhiz12.janitor.api.messages.emote.ReactionManager; import sciwhiz12.janitor.api.messages.emote.ReactionMessage; +import sciwhiz12.janitor.utils.Pair; import java.util.HashMap; import java.util.Map; +import java.util.stream.Collectors; import javax.annotation.Nonnull; public class ReactionManagerImpl extends ListenerAdapter implements ReactionManager { private final JanitorBotImpl bot; - private final Map messageMap = new HashMap<>(); + private final Map messageMap = new HashMap<>(); public ReactionManagerImpl(JanitorBotImpl bot) { this.bot = bot; @@ -23,7 +26,7 @@ public class ReactionManagerImpl extends ListenerAdapter implements ReactionMana if (messageMap.containsKey(message.getIdLong())) { throw new IllegalArgumentException("Reaction message already exists for message with id " + message.getIdLong()); } - final ReactionMessageImpl msg = new ReactionMessageImpl(bot, message); + final ReactionMessageImpl msg = new ReactionMessageImpl(bot); messageMap.put(message.getIdLong(), msg); return msg; } @@ -33,7 +36,9 @@ public class ReactionManagerImpl extends ListenerAdapter implements ReactionMana } public Map getRegisteredMessages() { - return messageMap; + return messageMap.entrySet().stream() + .map(entry -> Pair.of(entry.getKey(), entry.getValue())) + .collect(Collectors.toMap(Pair::getLeft, Pair::getRight)); } @Override @@ -41,6 +46,14 @@ public class ReactionManagerImpl extends ListenerAdapter implements ReactionMana return bot; } + @Override + public void onMessageReactionAdd(@Nonnull MessageReactionAddEvent event) { + if (event.getUserIdLong() == bot.getDiscord().getSelfUser().getIdLong()) return; + if (messageMap.containsKey(event.getMessageIdLong())) { + messageMap.get(event.getMessageIdLong()).acceptReaction(event); + } + } + @Override public void onMessageDelete(@Nonnull MessageDeleteEvent event) { if (messageMap.containsKey(event.getMessageIdLong())) { diff --git a/core/src/main/java/sciwhiz12/janitor/messages/emote/ReactionMessageImpl.java b/core/src/main/java/sciwhiz12/janitor/messages/emote/ReactionMessageImpl.java index a7c4887..e734096 100644 --- a/core/src/main/java/sciwhiz12/janitor/messages/emote/ReactionMessageImpl.java +++ b/core/src/main/java/sciwhiz12/janitor/messages/emote/ReactionMessageImpl.java @@ -5,35 +5,35 @@ import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.MessageReaction.ReactionEmote; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import net.dv8tion.jda.api.exceptions.ErrorHandler; -import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.requests.ErrorResponse; +import net.dv8tion.jda.api.requests.RestAction; import sciwhiz12.janitor.JanitorBotImpl; import sciwhiz12.janitor.api.messages.emote.ReactionMessage; +import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; -import javax.annotation.Nonnull; import static net.dv8tion.jda.api.Permission.MESSAGE_MANAGE; -public class ReactionMessageImpl extends ListenerAdapter implements ReactionMessage { +public class ReactionMessageImpl implements ReactionMessage { private final JanitorBotImpl bot; - private final Message message; + private long messageID; private final Map emotes = new LinkedHashMap<>(); private boolean removeEmotes = true; private long ownerID; private boolean onlyOwner; - public ReactionMessageImpl(JanitorBotImpl bot, Message message, boolean onlyOwner, long ownerID) { + public ReactionMessageImpl(JanitorBotImpl bot, boolean onlyOwner, long ownerID) { this.bot = bot; - this.message = message; this.ownerID = ownerID; this.onlyOwner = onlyOwner; } - public ReactionMessageImpl(JanitorBotImpl bot, Message message) { - this(bot, message, false, 0); + public ReactionMessageImpl(JanitorBotImpl bot) { + this(bot, false, 0); } public ReactionMessageImpl add(ReactionEmote emote, ReactionListener listener) { @@ -60,21 +60,22 @@ public class ReactionMessageImpl extends ListenerAdapter implements ReactionMess return this; } - public void create() { + public RestAction create(Message message) { + List> reactionList = new ArrayList<>(); for (ReactionEmote reaction : emotes.keySet()) { if (reaction.isEmote()) { - message.addReaction(reaction.getEmote()).queue(); + reactionList.add(message.addReaction(reaction.getEmote())); } else { - message.addReaction(reaction.getEmoji()).queue(); + reactionList.add(message.addReaction(reaction.getEmoji())); } } - bot.getDiscord().addEventListener(this); + messageID = message.getIdLong(); + return RestAction.allOf(reactionList) + .map($ -> message); } - @Override - public void onMessageReactionAdd(@Nonnull MessageReactionAddEvent event) { - if (event.getMessageIdLong() != message.getIdLong()) return; - if (event.getUserIdLong() == bot.getDiscord().getSelfUser().getIdLong()) return; + void acceptReaction(MessageReactionAddEvent event) { + if (event.getMessageIdLong() != messageID) return; if (onlyOwner && event.getUserIdLong() != ownerID) return; emotes.keySet().stream() diff --git a/moderation/src/main/java/sciwhiz12/janitor/moderation/notes/NoteCommand.java b/moderation/src/main/java/sciwhiz12/janitor/moderation/notes/NoteCommand.java index 549d8d7..95d670e 100644 --- a/moderation/src/main/java/sciwhiz12/janitor/moderation/notes/NoteCommand.java +++ b/moderation/src/main/java/sciwhiz12/janitor/moderation/notes/NoteCommand.java @@ -214,7 +214,8 @@ public class NoteCommand extends ModBaseCommand { .filter(predicate) .sorted(Comparator.>comparingInt(Map.Entry::getKey).reversed()) .collect(ImmutableList.toImmutableList()) - ); + ) + .queue(); } return 1; } diff --git a/moderation/src/main/java/sciwhiz12/janitor/moderation/warns/WarnListCommand.java b/moderation/src/main/java/sciwhiz12/janitor/moderation/warns/WarnListCommand.java index 0b7855c..5f9c5ce 100644 --- a/moderation/src/main/java/sciwhiz12/janitor/moderation/warns/WarnListCommand.java +++ b/moderation/src/main/java/sciwhiz12/janitor/moderation/warns/WarnListCommand.java @@ -110,7 +110,8 @@ public class WarnListCommand extends ModBaseCommand { .filter(predicate) .sorted(Comparator.>comparingInt(Map.Entry::getKey).reversed()) .collect(ImmutableList.toImmutableList()) - ); + ) + .queue(); } return 1; }