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;
}
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;
}
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;
}
Aggregations