1
0
mirror of https://github.com/sciwhiz12/Janitor.git synced 2024-11-13 00:51:26 +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.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<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,
Message triggerMessage,
List<T> entries);
default void build(MessageChannel channel, JanitorBot bot, Message triggerMessage, List<T> entries) {
build(channel, bot.getSubstitutions(), triggerMessage, entries);
default RestAction<Message> build(MessageChannel channel, JanitorBot bot, Message triggerMessage, List<T> entries) {
return build(channel, bot.getSubstitutions(), triggerMessage, entries);
}
}
}

View File

@ -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<Message> create(Message message);
long getOwnerID();

View File

@ -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;
});
}

View File

@ -34,22 +34,23 @@ public class HelloCommand extends BaseCommand {
final List<Member> 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;

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.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<T> implements ListingMessage.Builder<T> {
private final ListingMessage message;
private final Map<String, Supplier<String>> customSubstitutions;
private int amountPerPage = 6;
private boolean addDeletionReaction = true;
private BiConsumer<T, ModifiableSubstitutor<?>> entryApplier = (entry, sub) -> {};
public ListingMessageBuilder(ListingMessage message, Map<String, Supplier<String>> customSubstitutions) {
@ -62,7 +64,12 @@ public class ListingMessageBuilder<T> implements ListingMessage.Builder<T> {
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,
Message triggerMessage,
List<T> entries) {
@ -71,43 +78,46 @@ public class ListingMessageBuilder<T> implements ListingMessage.Builder<T> {
final ImmutableList<T> 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<T> 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 {

View File

@ -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<Long, ReactionMessage> messageMap = new HashMap<>();
private final Map<Long, ReactionMessageImpl> 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<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
@ -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())) {

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.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<ReactionEmote, ReactionListener> 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<Message> create(Message message) {
List<RestAction<Void>> 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()

View File

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

View File

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