Search in sources :

Example 1 with ReactionOperation

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

the class PollLobby method startPoll.

public void startPoll() {
    try {
        if (!isCompilant) {
            getChannel().sendMessage(EmoteReference.WARNING + "This poll cannot build. " + "**Remember that the maximum amount of options are 9, the minimum is 2 and that the maximum timeout is 10m and the minimum timeout is 30s.**\n" + "Options are separated with a comma, for example `1,2,3`. For spaced stuff use commas at the start and end of the sentence.").queue();
            getRunningPolls().remove(getChannel());
            return;
        }
        if (isPollAlreadyRunning(getChannel())) {
            getChannel().sendMessage(EmoteReference.WARNING + "There seems to be another poll running here...").queue();
            return;
        }
        if (!event.getGuild().getSelfMember().hasPermission(getChannel(), Permission.MESSAGE_ADD_REACTION)) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "Seems like I cannot add reactions here...").queue();
            getRunningPolls().remove(getChannel());
            return;
        }
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData data = dbGuild.getData();
        AtomicInteger at = new AtomicInteger();
        data.setRanPolls(data.getRanPolls() + 1L);
        dbGuild.save();
        String toShow = Stream.of(options).map(opt -> String.format("#%01d.- %s", at.incrementAndGet(), opt)).collect(Collectors.joining("\n"));
        EmbedBuilder builder = new EmbedBuilder().setAuthor(String.format("Poll #%1d created by %s", data.getRanPolls(), event.getAuthor().getName()), null, event.getAuthor().getAvatarUrl()).setDescription("**Poll started. React to the number to vote.**\n*" + name + "*").addField("Options", "```md\n" + toShow + "```", false).setColor(event.getMember().getColor()).setThumbnail("https://cdn.pixabay.com/photo/2012/04/14/16/26/question-34499_960_720.png").setFooter("You have " + Utils.getDurationMinutes(timeout) + " minutes to vote.", event.getAuthor().getAvatarUrl());
        getChannel().sendMessage(builder.build()).queue(message -> ReactionOperations.create(message, TimeUnit.MILLISECONDS.toSeconds(timeout), new ReactionOperation() {

            @Override
            public boolean run(MessageReactionAddEvent e) {
                int i = e.getReactionEmote().getName().charAt(0) - '0';
                if (i < 1 || i > options.length)
                    return false;
                return false;
            }

            @Override
            public void onExpire() {
                EmbedBuilder embedBuilder = new EmbedBuilder().setTitle("Poll results").setDescription("**Showing results for the poll started by " + event.getAuthor().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);
                event.getChannel().sendMessage(embedBuilder.build()).queue();
            }
        }, reactions(options.length)));
    } catch (Exception e) {
        getChannel().sendMessage(EmoteReference.ERROR + "An unknown error has occurred while setting up a poll. Maybe try again?").queue();
    }
}
Also used : MessageReactionAddEvent(net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent) TextChannel(net.dv8tion.jda.core.entities.TextChannel) Utils(net.kodehawa.mantarobot.utils.Utils) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ReactionOperations(net.kodehawa.mantarobot.core.listeners.operations.ReactionOperations) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Lobby(net.kodehawa.mantarobot.commands.interaction.Lobby) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Stream(java.util.stream.Stream) Permission(net.dv8tion.jda.core.Permission) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) Map(java.util.Map) ReactionOperation(net.kodehawa.mantarobot.core.listeners.operations.ReactionOperation) MantaroData(net.kodehawa.mantarobot.data.MantaroData) Assert(org.junit.Assert) ReactionOperation(net.kodehawa.mantarobot.core.listeners.operations.ReactionOperation) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) MessageReactionAddEvent(net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)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.core.EmbedBuilder)1 Permission (net.dv8tion.jda.core.Permission)1 TextChannel (net.dv8tion.jda.core.entities.TextChannel)1 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)1 MessageReactionAddEvent (net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent)1 Lobby (net.kodehawa.mantarobot.commands.interaction.Lobby)1 ReactionOperation (net.kodehawa.mantarobot.core.listeners.operations.ReactionOperation)1 ReactionOperations (net.kodehawa.mantarobot.core.listeners.operations.ReactionOperations)1 MantaroData (net.kodehawa.mantarobot.data.MantaroData)1 DBGuild (net.kodehawa.mantarobot.data.entities.DBGuild)1 GuildData (net.kodehawa.mantarobot.data.entities.helpers.GuildData)1 Utils (net.kodehawa.mantarobot.utils.Utils)1 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)1