Search in sources :

Example 1 with ReactionOperation

use of net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation in project MantaroBot by Mantaro.

the class ReactionOperations method createOrGet.

public static Future<Void> createOrGet(Message message, long timeoutSeconds, ReactionOperation operation, String... defaultReactions) {
    // We should be getting Mantaro's messages
    if (!message.getAuthor().equals(message.getJDA().getSelfUser()))
        throw new IllegalArgumentException("Must provide a message sent by the bot");
    Future<Void> f = createOrGet(message.getIdLong(), timeoutSeconds, operation);
    if (defaultReactions.length > 0) {
        AtomicInteger index = new AtomicInteger();
        AtomicReference<Consumer<Void>> c = new AtomicReference<>();
        // Ignore errors (Like unknown message).
        Consumer<Throwable> ignore = (t) -> {
        };
        c.set(ignored -> {
            if (f.isCancelled())
                return;
            int i = index.incrementAndGet();
            if (i < defaultReactions.length) {
                message.addReaction(reaction(defaultReactions[i])).queue(c.get(), ignore);
            }
        });
        message.addReaction(reaction(defaultReactions[0])).queue(c.get(), ignore);
    }
    return f;
}
Also used : MessageReactionAddEvent(net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent) ExpiringMap(net.jodah.expiringmap.ExpiringMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Message(net.dv8tion.jda.core.entities.Message) MessageReactionRemoveEvent(net.dv8tion.jda.core.events.message.react.MessageReactionRemoveEvent) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(net.dv8tion.jda.core.events.Event) ReactionOperation(net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation) EventListener(net.dv8tion.jda.core.hooks.EventListener) MessageReactionRemoveAllEvent(net.dv8tion.jda.core.events.message.react.MessageReactionRemoveAllEvent) Operation(net.kodehawa.mantarobot.core.listeners.operations.core.Operation) Consumer(java.util.function.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 2 with ReactionOperation

use of net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation in project MantaroBot by Mantaro.

the class Poll method createPoll.

private Future<Void> createPoll(Message message) {
    runningPoll = ReactionOperations.create(message, TimeUnit.MILLISECONDS.toSeconds(timeout), new ReactionOperation() {

        @Override
        public int add(MessageReactionAddEvent e) {
            int i = e.getReactionEmote().getName().charAt(0) - '\u0030';
            if (i < 1 || i > options.length)
                return Operation.IGNORED;
            // always return false anyway lul
            return Operation.IGNORED;
        }

        @Override
        public void onExpire() {
            if (getChannel() == null)
                return;
            EmbedBuilder embedBuilder = new EmbedBuilder().setTitle("Poll results").setDescription("**Showing results for the poll started by " + MantaroBot.getInstance().getUserById(owner).getName() + "** with name: *" + name + "*").setFooter("Thanks for your vote", null);
            AtomicInteger react = new AtomicInteger(0);
            AtomicInteger counter = new AtomicInteger(0);
            String votes = new ArrayList<>(getChannel().getMessageById(message.getIdLong()).complete().getReactions()).stream().filter(r -> react.getAndIncrement() <= options.length).map(r -> "+Registered " + (r.getCount() - 1) + " votes for option " + options[counter.getAndIncrement()]).collect(Collectors.joining("\n"));
            embedBuilder.addField("Results", "```diff\n" + votes + "```", false);
            getChannel().sendMessage(embedBuilder.build()).queue();
            getRunningPolls().remove(getChannel().getId());
        }

        @Override
        public void onCancel() {
            getChannel().sendMessage(EmoteReference.CORRECT + "Cancelled poll").queue();
            onExpire();
        }
    }, reactions(options.length));
    return runningPoll;
}
Also used : ReactionOperation(net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation) MessageReactionAddEvent(net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) TextChannel(net.dv8tion.jda.core.entities.TextChannel) Utils(net.kodehawa.mantarobot.utils.Utils) HashMap(java.util.HashMap) Message(net.dv8tion.jda.core.entities.Message) Lobby(net.kodehawa.mantarobot.commands.interaction.Lobby) ArrayList(java.util.ArrayList) MantaroBot(net.kodehawa.mantarobot.MantaroBot) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Future(java.util.concurrent.Future) Permission(net.dv8tion.jda.core.Permission) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) InteractiveOperations(net.kodehawa.mantarobot.core.listeners.operations.InteractiveOperations) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) Collectors(java.util.stream.Collectors) ReactionOperations(net.kodehawa.mantarobot.core.listeners.operations.ReactionOperations) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) java.awt(java.awt) TimeUnit(java.util.concurrent.TimeUnit) Stream(java.util.stream.Stream) User(net.dv8tion.jda.core.entities.User) GuildData(net.kodehawa.mantarobot.db.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) ReactionOperation(net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation) Operation(net.kodehawa.mantarobot.core.listeners.operations.core.Operation) MessageReactionAddEvent(net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList)

Example 3 with ReactionOperation

use of net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation in project MantaroBot by Mantaro.

the class ReactionOperations method create.

public static Future<Void> create(Message message, long timeoutSeconds, ReactionOperation operation, String... defaultReactions) {
    if (!message.getAuthor().equals(message.getJDA().getSelfUser()))
        throw new IllegalArgumentException("Must provide a message sent by the bot");
    Future<Void> f = create(message.getIdLong(), timeoutSeconds, operation);
    if (defaultReactions.length > 0) {
        AtomicInteger index = new AtomicInteger();
        AtomicReference<Consumer<Void>> c = new AtomicReference<>();
        Consumer<Throwable> ignore = (t) -> {
        };
        c.set(ignored -> {
            // Ignore this if we already cancelled this operation.
            if (f.isCancelled())
                return;
            int i = index.incrementAndGet();
            if (i < defaultReactions.length) {
                if (message.getGuild() != null && message.getGuild().getSelfMember() != null) {
                    message.addReaction(reaction(defaultReactions[i])).queue(c.get(), ignore);
                }
            }
        });
        message.addReaction(reaction(defaultReactions[0])).queue(c.get(), ignore);
    }
    return f;
}
Also used : MessageReactionAddEvent(net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent) ExpiringMap(net.jodah.expiringmap.ExpiringMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Message(net.dv8tion.jda.core.entities.Message) MessageReactionRemoveEvent(net.dv8tion.jda.core.events.message.react.MessageReactionRemoveEvent) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(net.dv8tion.jda.core.events.Event) ReactionOperation(net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation) EventListener(net.dv8tion.jda.core.hooks.EventListener) MessageReactionRemoveAllEvent(net.dv8tion.jda.core.events.message.react.MessageReactionRemoveAllEvent) Operation(net.kodehawa.mantarobot.core.listeners.operations.core.Operation) Consumer(java.util.function.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

Future (java.util.concurrent.Future)3 TimeUnit (java.util.concurrent.TimeUnit)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Message (net.dv8tion.jda.core.entities.Message)3 MessageReactionAddEvent (net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent)3 Operation (net.kodehawa.mantarobot.core.listeners.operations.core.Operation)3 ReactionOperation (net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Consumer (java.util.function.Consumer)2 Event (net.dv8tion.jda.core.events.Event)2 MessageReactionRemoveAllEvent (net.dv8tion.jda.core.events.message.react.MessageReactionRemoveAllEvent)2 MessageReactionRemoveEvent (net.dv8tion.jda.core.events.message.react.MessageReactionRemoveEvent)2 EventListener (net.dv8tion.jda.core.hooks.EventListener)2 ExpiringMap (net.jodah.expiringmap.ExpiringMap)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 java.awt (java.awt)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1