use of net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent 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();
}
}
Aggregations