Search in sources :

Example 11 with MessageReaction

use of net.dv8tion.jda.api.entities.MessageReaction in project Emolga by TecToast.

the class Giveaway method end.

public void end() {
    isEnded = true;
    DBManagers.GIVEAWAY.removeGiveaway(this);
    MessageBuilder mb = new MessageBuilder();
    mb.append("\uD83C\uDF89").append(" **GIVEAWAY ZU ENDE** ").append("\uD83C\uDF89");
    EmbedBuilder eb = new EmbedBuilder();
    // dark theme background
    eb.setColor(Color.CYAN);
    eb.setFooter((winners == 1 ? "" : winners + " Gewinner | ") + "Endete", null);
    eb.setTimestamp(end);
    if (prize != null)
        eb.setAuthor(prize, null, null);
    try {
        Optional<MessageReaction> opt;
        if (messageId == 775761399737352222L) {
            opt = emolgajda.getTextChannelById(channelId).retrieveMessageById(messageId).complete().getReactions().stream().filter(mr -> mr.getReactionEmote().isEmoji() && mr.getReactionEmote().getEmoji().equals("\uD83C\uDF89")).findFirst();
        } else {
            opt = emolgajda.getTextChannelById(channelId).retrieveMessageById(messageId).complete().getReactions().stream().filter(mr -> mr.getReactionEmote().isEmote() && (mr.getReactionEmote().getEmote().getId().equals("772191611487780934") || mr.getReactionEmote().getEmote().getId().equals("774817002636181535"))).findFirst();
        }
        ArrayList<Member> members = new ArrayList<>();
        opt.ifPresent(mr -> mr.retrieveUsers().complete().stream().filter(u -> !u.isBot() && u.getIdLong() != userId).forEach(u -> members.add(emolgajda.getTextChannelById(channelId).getGuild().retrieveMember(u).complete())));
        ArrayList<Member> wins = new ArrayList<>();
        if (members.size() > 0)
            for (int i = 0; i < winners; i++) {
                if (members.size() == 0)
                    break;
                wins.add(members.remove(new Random().nextInt(members.size())));
            }
        // restJDA.getReactionUsers(channelId, messageId, EncodingUtil.encodeUTF8(Constants.TADA))..submit().thenAcceptAsync(ids -> {
        // List<Long> wins = GiveawayUtil.selectWinners(ids, winners);
        String toSend;
        if (wins.isEmpty()) {
            eb.setDescription("Es konnte kein Gewinner ermittelt werden!");
            toSend = "Es konnte kein Gewinner ermittelt werden!";
        } else if (wins.size() == 1) {
            eb.setDescription("Gewinner: " + wins.get(0).getAsMention());
            toSend = "Herzlichen Glückwunsch " + wins.get(0).getAsMention() + "! Du hast" + (prize == null ? "" : " **" + prize + "**") + " gewonnen!";
        } else {
            eb.setDescription("Gewinner:");
            wins.forEach(w -> eb.appendDescription("\n").appendDescription(w.getAsMention()));
            toSend = "Herzlichen Glückwunsch " + wins.stream().map(Member::getAsMention).collect(Collectors.joining(", "));
            toSend += "! Ihr habt" + (prize == null ? "" : " **" + prize + "**") + " gewonnen!";
        }
        mb.setEmbeds(eb.appendDescription("\nGehostet von: <@" + userId + ">").build());
        EmolgaMain.todel.add(this);
        emolgajda.getTextChannelById(channelId).editMessageById(messageId, mb.build()).queue();
        emolgajda.getTextChannelById(channelId).sendMessage(toSend).queue();
    } catch (Exception e) {
        e.printStackTrace();
        mb.setEmbeds(eb.setDescription("Es konnte kein Gewinner festgestellt werden!\nGehostet von: <@" + userId + ">").build());
        emolgajda.getTextChannelById(channelId).editMessageById(messageId, mb.build()).queue();
        emolgajda.getTextChannelById(channelId).sendMessage("Es konnte kein Gewinner festgestellt werden!").queue();
    }
}
Also used : Message(net.dv8tion.jda.api.entities.Message) java.util(java.util) Logger(org.slf4j.Logger) ScheduledFuture(java.util.concurrent.ScheduledFuture) LoggerFactory(org.slf4j.LoggerFactory) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) GcreateCommand.secondsToTime(de.tectoast.emolga.commands.various.GcreateCommand.secondsToTime) EmolgaMain(de.tectoast.emolga.bot.EmolgaMain) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Member(net.dv8tion.jda.api.entities.Member) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) java.awt(java.awt) TimeUnit(java.util.concurrent.TimeUnit) ChronoUnit(java.time.temporal.ChronoUnit) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MessageReaction(net.dv8tion.jda.api.entities.MessageReaction) DBManagers(de.tectoast.emolga.utils.sql.DBManagers) EmolgaMain.emolgajda(de.tectoast.emolga.bot.EmolgaMain.emolgajda) MessageReaction(net.dv8tion.jda.api.entities.MessageReaction) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Member(net.dv8tion.jda.api.entities.Member)

Aggregations

MessageReaction (net.dv8tion.jda.api.entities.MessageReaction)11 ArrayList (java.util.ArrayList)5 Instant (java.time.Instant)4 Member (net.dv8tion.jda.api.entities.Member)4 Message (net.dv8tion.jda.api.entities.Message)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 Emote (net.dv8tion.jda.api.entities.Emote)3 Guild (net.dv8tion.jda.api.entities.Guild)3 User (net.dv8tion.jda.api.entities.User)3 MessageReactionAddEvent (net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent)3 ChronoUnit (java.time.temporal.ChronoUnit)2 java.util (java.util)2 Objects (java.util.Objects)2 TimeUnit (java.util.concurrent.TimeUnit)2 JDA (net.dv8tion.jda.api.JDA)2 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)2 Permission (net.dv8tion.jda.api.Permission)2 GuildMessageReactionAddEvent (net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent)2