1
0
mirror of https://github.com/sciwhiz12/Janitor.git synced 2024-11-10 02:21:25 +00:00

Refactor reaction messages to allow full control of when to send the message

This commit is contained in:
sciwhiz12 2020-11-30 14:01:41 +08:00
parent 66ab6b7005
commit d18a0010cc
Signed by: sciwhiz12
GPG Key ID: 622CF446534317E1
9 changed files with 108 additions and 75 deletions

View File

@ -3,6 +3,7 @@ package sciwhiz12.janitor.api.messages;
import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel; import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.requests.RestAction;
import sciwhiz12.janitor.api.JanitorBot; import sciwhiz12.janitor.api.JanitorBot;
import sciwhiz12.janitor.api.messages.substitution.ModifiableSubstitutions; import sciwhiz12.janitor.api.messages.substitution.ModifiableSubstitutions;
import sciwhiz12.janitor.api.messages.substitution.ModifiableSubstitutor; import sciwhiz12.janitor.api.messages.substitution.ModifiableSubstitutor;
@ -191,13 +192,15 @@ public class ListingMessage {
Builder<T> with(final String argument, final Supplier<String> value); Builder<T> with(final String argument, final Supplier<String> value);
void build(MessageChannel channel, Builder<T> deletionReaction(final boolean addDeletionReaction);
RestAction<Message> build(MessageChannel channel,
SubstitutionsMap globalSubstitutions, SubstitutionsMap globalSubstitutions,
Message triggerMessage, Message triggerMessage,
List<T> entries); List<T> entries);
default void build(MessageChannel channel, JanitorBot bot, Message triggerMessage, List<T> entries) { default RestAction<Message> build(MessageChannel channel, JanitorBot bot, Message triggerMessage, List<T> entries) {
build(channel, bot.getSubstitutions(), triggerMessage, entries); return build(channel, bot.getSubstitutions(), triggerMessage, entries);
} }
} }
} }

View File

@ -1,8 +1,10 @@
package sciwhiz12.janitor.api.messages.emote; package sciwhiz12.janitor.api.messages.emote;
import net.dv8tion.jda.api.entities.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.entities.MessageReaction.ReactionEmote;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.api.requests.RestAction;
import sciwhiz12.janitor.api.JanitorBot; import sciwhiz12.janitor.api.JanitorBot;
import java.util.Map; import java.util.Map;
@ -19,7 +21,7 @@ public interface ReactionMessage {
ReactionMessage owner(long ownerID); ReactionMessage owner(long ownerID);
void create(); RestAction<Message> create(Message message);
long getOwnerID(); long getOwnerID();

View File

@ -22,7 +22,8 @@ public class CmdListCommand extends BaseCommand {
.with("commands_prefix", () -> config(ctx.getSource()).forGuild(COMMAND_PREFIX)) .with("commands_prefix", () -> config(ctx.getSource()).forGuild(COMMAND_PREFIX))
.setEntryApplier((command, subs) -> subs.with("command", () -> command)) .setEntryApplier((command, subs) -> subs.with("command", () -> command))
.build(ctx.getSource().getChannel(), getBot(), ctx.getSource().getMessage(), .build(ctx.getSource().getChannel(), getBot(), ctx.getSource().getMessage(),
Lists.newArrayList(getRegistry().registry.keySet())); Lists.newArrayList(getRegistry().registry.keySet()))
.queue();
return 1; return 1;
}); });
} }

View File

@ -34,19 +34,20 @@ public class HelloCommand extends BaseCommand {
final List<Member> memberList = getMembers("member", ctx).fromGuild(ctx.getSource().getGuild()); final List<Member> memberList = getMembers("member", ctx).fromGuild(ctx.getSource().getGuild());
if (memberList.size() == 1) { if (memberList.size() == 1) {
final Member member = memberList.get(0); final Member member = memberList.get(0);
ctx.getSource().getChannel().sendMessage("Hello " + member.getAsMention() + "!").queue( ctx.getSource().getChannel().sendMessage("Hello " + member.getAsMention() + "!")
success -> { .flatMap(message ->
JANITOR.debug("Sent greeting message to {}, on cmd of {}", Util.toString(member.getUser()), getBot().getReactions().newMessage(message)
Util.toString(ctx.getSource().getAuthor())); .add("\u274C", (msg, event) -> message.delete()
getBot().getReactions().newMessage(success)
.add("\u274C", (msg, event) -> success.delete()
.flatMap(v -> event.getChannel() .flatMap(v -> event.getChannel()
.deleteMessageById(ctx.getSource().getMessageIdLong())) .deleteMessageById(ctx.getSource().getMessageIdLong()))
.queue() .queue()
) )
.owner(ctx.getSource().getAuthor().getIdLong()) .owner(ctx.getSource().getAuthor().getIdLong())
.create(); .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 {}", err -> JANITOR.error("Error while sending greeting message to {}, on cmd of {}",
Util.toString(member.getUser()), Util.toString(ctx.getSource().getAuthor())) Util.toString(member.getUser()), Util.toString(ctx.getSource().getAuthor()))
); );

View File

@ -9,8 +9,9 @@ import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel; import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role; 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.ListingMessage;
import sciwhiz12.janitor.api.messages.emote.ReactionMessage;
import sciwhiz12.janitor.api.messages.substitution.ModifiableSubstitutor; import sciwhiz12.janitor.api.messages.substitution.ModifiableSubstitutor;
import sciwhiz12.janitor.api.messages.substitution.SubstitutionsMap; import sciwhiz12.janitor.api.messages.substitution.SubstitutionsMap;
import sciwhiz12.janitor.api.messages.substitution.Substitutor; import sciwhiz12.janitor.api.messages.substitution.Substitutor;
@ -31,6 +32,7 @@ public class ListingMessageBuilder<T> implements ListingMessage.Builder<T> {
private final ListingMessage message; private final ListingMessage message;
private final Map<String, Supplier<String>> customSubstitutions; private final Map<String, Supplier<String>> customSubstitutions;
private int amountPerPage = 6; private int amountPerPage = 6;
private boolean addDeletionReaction = true;
private BiConsumer<T, ModifiableSubstitutor<?>> entryApplier = (entry, sub) -> {}; private BiConsumer<T, ModifiableSubstitutor<?>> entryApplier = (entry, sub) -> {};
public ListingMessageBuilder(ListingMessage message, Map<String, Supplier<String>> customSubstitutions) { public ListingMessageBuilder(ListingMessage message, Map<String, Supplier<String>> customSubstitutions) {
@ -62,7 +64,12 @@ public class ListingMessageBuilder<T> implements ListingMessage.Builder<T> {
return this; return this;
} }
public void build(MessageChannel channel, public ListingMessageBuilder<T> deletionReaction(final boolean addDeletionReaction) {
this.addDeletionReaction = addDeletionReaction;
return this;
}
public RestAction<Message> build(MessageChannel channel,
SubstitutionsMap globalSubstitutions, SubstitutionsMap globalSubstitutions,
Message triggerMessage, Message triggerMessage,
List<T> entries) { List<T> entries) {
@ -71,8 +78,9 @@ public class ListingMessageBuilder<T> implements ListingMessage.Builder<T> {
final ImmutableList<T> list = ImmutableList.copyOf(entries); final ImmutableList<T> list = ImmutableList.copyOf(entries);
final PagedMessage pagedMessage = new PagedMessage(message, list, amountPerPage); final PagedMessage pagedMessage = new PagedMessage(message, list, amountPerPage);
channel.sendMessage(pagedMessage.createMessage(customSubs, entryApplier)) return channel.sendMessage(pagedMessage.createMessage(customSubs, entryApplier))
.queue(listMsg -> globalSubstitutions.getBot().getReactions().newMessage(listMsg) .flatMap(listMsg -> {
ReactionMessage reactionMsg = globalSubstitutions.getBot().getReactions().newMessage(listMsg)
.owner(triggerMessage.getAuthor().getIdLong()) .owner(triggerMessage.getAuthor().getIdLong())
.removeEmotes(true) .removeEmotes(true)
.add("\u2b05", (msg, event) -> { // PREVIOUS .add("\u2b05", (msg, event) -> { // PREVIOUS
@ -83,8 +91,10 @@ public class ListingMessageBuilder<T> implements ListingMessage.Builder<T> {
) )
.queue(); .queue();
} }
}) });
.add("\u274c", (msg, event) -> { // CLOSE
if (addDeletionReaction) {
reactionMsg.add("\u274c", (msg, event) -> { // CLOSE
event.getChannel().deleteMessageById(event.getMessageIdLong()) event.getChannel().deleteMessageById(event.getMessageIdLong())
.flatMap(v -> !triggerMessage.isFromGuild() || .flatMap(v -> !triggerMessage.isFromGuild() ||
event.getGuild().getSelfMember() event.getGuild().getSelfMember()
@ -92,8 +102,10 @@ public class ListingMessageBuilder<T> implements ListingMessage.Builder<T> {
Permission.MESSAGE_MANAGE), Permission.MESSAGE_MANAGE),
v -> triggerMessage.delete()) v -> triggerMessage.delete())
.queue(); .queue();
}) });
.add("\u27a1", (msg, event) -> { // NEXT }
reactionMsg.add("\u27a1", (msg, event) -> { // NEXT
if (pagedMessage.advancePage(PageDirection.NEXT)) { if (pagedMessage.advancePage(PageDirection.NEXT)) {
event.retrieveMessage() event.retrieveMessage()
.flatMap(eventMsg -> eventMsg.editMessage( .flatMap(eventMsg -> eventMsg.editMessage(
@ -101,13 +113,11 @@ public class ListingMessageBuilder<T> implements ListingMessage.Builder<T> {
) )
.queue(); .queue();
} }
}) });
.create()
);
}
public void build(MessageChannel channel, JanitorBot bot, Message triggerMessage, List<T> entries) { return reactionMsg.create(listMsg);
build(channel, bot.getSubstitutions(), triggerMessage, entries); }
);
} }
class PagedMessage { class PagedMessage {

View File

@ -2,18 +2,21 @@ package sciwhiz12.janitor.messages.emote;
import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.MessageDeleteEvent; 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 net.dv8tion.jda.api.hooks.ListenerAdapter;
import sciwhiz12.janitor.JanitorBotImpl; import sciwhiz12.janitor.JanitorBotImpl;
import sciwhiz12.janitor.api.messages.emote.ReactionManager; import sciwhiz12.janitor.api.messages.emote.ReactionManager;
import sciwhiz12.janitor.api.messages.emote.ReactionMessage; import sciwhiz12.janitor.api.messages.emote.ReactionMessage;
import sciwhiz12.janitor.utils.Pair;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public class ReactionManagerImpl extends ListenerAdapter implements ReactionManager { public class ReactionManagerImpl extends ListenerAdapter implements ReactionManager {
private final JanitorBotImpl bot; private final JanitorBotImpl bot;
private final Map<Long, ReactionMessage> messageMap = new HashMap<>(); private final Map<Long, ReactionMessageImpl> messageMap = new HashMap<>();
public ReactionManagerImpl(JanitorBotImpl bot) { public ReactionManagerImpl(JanitorBotImpl bot) {
this.bot = bot; this.bot = bot;
@ -23,7 +26,7 @@ public class ReactionManagerImpl extends ListenerAdapter implements ReactionMana
if (messageMap.containsKey(message.getIdLong())) { if (messageMap.containsKey(message.getIdLong())) {
throw new IllegalArgumentException("Reaction message already exists for message with id " + 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); messageMap.put(message.getIdLong(), msg);
return msg; return msg;
} }
@ -33,7 +36,9 @@ public class ReactionManagerImpl extends ListenerAdapter implements ReactionMana
} }
public Map<Long, ReactionMessage> getRegisteredMessages() { public Map<Long, ReactionMessage> getRegisteredMessages() {
return messageMap; return messageMap.entrySet().stream()
.map(entry -> Pair.<Long, ReactionMessage>of(entry.getKey(), entry.getValue()))
.collect(Collectors.toMap(Pair::getLeft, Pair::getRight));
} }
@Override @Override
@ -41,6 +46,14 @@ public class ReactionManagerImpl extends ListenerAdapter implements ReactionMana
return bot; 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 @Override
public void onMessageDelete(@Nonnull MessageDeleteEvent event) { public void onMessageDelete(@Nonnull MessageDeleteEvent event) {
if (messageMap.containsKey(event.getMessageIdLong())) { if (messageMap.containsKey(event.getMessageIdLong())) {

View File

@ -5,35 +5,35 @@ import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageReaction.ReactionEmote; import net.dv8tion.jda.api.entities.MessageReaction.ReactionEmote;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.api.exceptions.ErrorHandler; 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.ErrorResponse;
import net.dv8tion.jda.api.requests.RestAction;
import sciwhiz12.janitor.JanitorBotImpl; import sciwhiz12.janitor.JanitorBotImpl;
import sciwhiz12.janitor.api.messages.emote.ReactionMessage; import sciwhiz12.janitor.api.messages.emote.ReactionMessage;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.Nonnull;
import static net.dv8tion.jda.api.Permission.MESSAGE_MANAGE; 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 JanitorBotImpl bot;
private final Message message; private long messageID;
private final Map<ReactionEmote, ReactionListener> emotes = new LinkedHashMap<>(); private final Map<ReactionEmote, ReactionListener> emotes = new LinkedHashMap<>();
private boolean removeEmotes = true; private boolean removeEmotes = true;
private long ownerID; private long ownerID;
private boolean onlyOwner; 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.bot = bot;
this.message = message;
this.ownerID = ownerID; this.ownerID = ownerID;
this.onlyOwner = onlyOwner; this.onlyOwner = onlyOwner;
} }
public ReactionMessageImpl(JanitorBotImpl bot, Message message) { public ReactionMessageImpl(JanitorBotImpl bot) {
this(bot, message, false, 0); this(bot, false, 0);
} }
public ReactionMessageImpl add(ReactionEmote emote, ReactionListener listener) { public ReactionMessageImpl add(ReactionEmote emote, ReactionListener listener) {
@ -60,21 +60,22 @@ public class ReactionMessageImpl extends ListenerAdapter implements ReactionMess
return this; return this;
} }
public void create() { public RestAction<Message> create(Message message) {
List<RestAction<Void>> reactionList = new ArrayList<>();
for (ReactionEmote reaction : emotes.keySet()) { for (ReactionEmote reaction : emotes.keySet()) {
if (reaction.isEmote()) { if (reaction.isEmote()) {
message.addReaction(reaction.getEmote()).queue(); reactionList.add(message.addReaction(reaction.getEmote()));
} else { } 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 void acceptReaction(MessageReactionAddEvent event) {
public void onMessageReactionAdd(@Nonnull MessageReactionAddEvent event) { if (event.getMessageIdLong() != messageID) return;
if (event.getMessageIdLong() != message.getIdLong()) return;
if (event.getUserIdLong() == bot.getDiscord().getSelfUser().getIdLong()) return;
if (onlyOwner && event.getUserIdLong() != ownerID) return; if (onlyOwner && event.getUserIdLong() != ownerID) return;
emotes.keySet().stream() emotes.keySet().stream()

View File

@ -214,7 +214,8 @@ public class NoteCommand extends ModBaseCommand {
.filter(predicate) .filter(predicate)
.sorted(Comparator.<Map.Entry<Integer, NoteEntry>>comparingInt(Map.Entry::getKey).reversed()) .sorted(Comparator.<Map.Entry<Integer, NoteEntry>>comparingInt(Map.Entry::getKey).reversed())
.collect(ImmutableList.toImmutableList()) .collect(ImmutableList.toImmutableList())
); )
.queue();
} }
return 1; return 1;
} }

View File

@ -110,7 +110,8 @@ public class WarnListCommand extends ModBaseCommand {
.filter(predicate) .filter(predicate)
.sorted(Comparator.<Map.Entry<Integer, WarningEntry>>comparingInt(Map.Entry::getKey).reversed()) .sorted(Comparator.<Map.Entry<Integer, WarningEntry>>comparingInt(Map.Entry::getKey).reversed())
.collect(ImmutableList.toImmutableList()) .collect(ImmutableList.toImmutableList())
); )
.queue();
} }
return 1; return 1;
} }