Search in sources :

Example 1 with MessageReactionAddEvent

use of net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent in project MantaroBot by Mantaro.

the class Poll method createPoll.

private void createPoll(Context ctx, Message message, I18nContext languageContext) {
    runningPoll = ReactionOperations.create(message, TimeUnit.MILLISECONDS.toSeconds(timeout), new ReactionOperation() {

        @Override
        public int add(MessageReactionAddEvent e) {
            // always return false anyway lul
            return Operation.IGNORED;
        }

        @Override
        public void onExpire() {
            if (getChannel() == null)
                return;
            var user = ctx.getAuthor();
            var embedBuilder = new EmbedBuilder().setTitle(languageContext.get("commands.poll.result_header")).setDescription(String.format(languageContext.get("commands.poll.result_screen"), user.getName(), name)).setFooter(languageContext.get("commands.poll.thank_note"), null);
            var react = new AtomicInteger(0);
            var counter = new AtomicInteger(0);
            getChannel().retrieveMessageById(message.getIdLong()).queue(message -> {
                var votes = message.getReactions().stream().filter(r -> react.getAndIncrement() <= options.length).map(r -> String.format(languageContext.get("commands.poll.vote_results"), r.getCount() - 1, options[counter.getAndIncrement()])).collect(Collectors.joining("\n"));
                embedBuilder.addField(languageContext.get("commands.poll.results"), "```diff\n" + votes + "```", false);
                getChannel().sendMessageEmbeds(embedBuilder.build()).queue();
            });
            getRunningPolls().remove(getChannel().getId());
        }

        @Override
        public void onCancel() {
            getChannel().sendMessageFormat(languageContext.get("commands.poll.cancelled"), EmoteReference.CORRECT).queue();
            onExpire();
        }
    }, reactions(options.length));
}
Also used : ReactionOperation(net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation) Message(net.dv8tion.jda.api.entities.Message) Color(java.awt.Color) InteractiveOperations(net.kodehawa.mantarobot.core.listeners.operations.InteractiveOperations) Permission(net.dv8tion.jda.api.Permission) Utils(net.kodehawa.mantarobot.utils.Utils) HashMap(java.util.HashMap) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) TextChannel(net.dv8tion.jda.api.entities.TextChannel) MessageReactionAddEvent(net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent) Collectors(java.util.stream.Collectors) ReactionOperations(net.kodehawa.mantarobot.core.listeners.operations.ReactionOperations) Lobby(net.kodehawa.mantarobot.commands.interaction.Lobby) TimeUnit(java.util.concurrent.TimeUnit) Future(java.util.concurrent.Future) Stream(java.util.stream.Stream) I18nContext(net.kodehawa.mantarobot.core.modules.commands.i18n.I18nContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) Map(java.util.Map) MantaroData(net.kodehawa.mantarobot.data.MantaroData) ReactionOperation(net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation) Context(net.kodehawa.mantarobot.core.modules.commands.base.Context) Operation(net.kodehawa.mantarobot.core.listeners.operations.core.Operation) MessageReactionAddEvent(net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

Color (java.awt.Color)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 Permission (net.dv8tion.jda.api.Permission)1 Message (net.dv8tion.jda.api.entities.Message)1 TextChannel (net.dv8tion.jda.api.entities.TextChannel)1 MessageReactionAddEvent (net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent)1 Lobby (net.kodehawa.mantarobot.commands.interaction.Lobby)1 InteractiveOperations (net.kodehawa.mantarobot.core.listeners.operations.InteractiveOperations)1 ReactionOperations (net.kodehawa.mantarobot.core.listeners.operations.ReactionOperations)1 Operation (net.kodehawa.mantarobot.core.listeners.operations.core.Operation)1 ReactionOperation (net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation)1 Context (net.kodehawa.mantarobot.core.modules.commands.base.Context)1 I18nContext (net.kodehawa.mantarobot.core.modules.commands.i18n.I18nContext)1