Search in sources :

Example 1 with EmoticonSignUpMessageListener

use of pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener in project pokeraidbot by magnusmickelsson.

the class AlterRaidCommand method getListenerForGroup.

private EmoticonSignUpMessageListener getListenerForGroup(Raid raid, RaidGroup raidGroup) {
    for (Object o : botService.getBot().getRegisteredListeners()) {
        if (o instanceof EmoticonSignUpMessageListener) {
            EmoticonSignUpMessageListener listener = (EmoticonSignUpMessageListener) o;
            final String raidId = raid.getId();
            final boolean isCorrectRaid = raidId.equals(listener.getRaidId());
            final boolean isCorrectGroup = raidGroup.getEmoteMessageId().equals(listener.getEmoteMessageId()) && raidGroup.getInfoMessageId().equals(listener.getInfoMessageId());
            if (isCorrectRaid && isCorrectGroup) {
                return listener;
            }
        }
    }
    return null;
}
Also used : EmoticonSignUpMessageListener(pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener)

Example 2 with EmoticonSignUpMessageListener

use of pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener in project pokeraidbot by magnusmickelsson.

the class AlterRaidCommand method changeGroupTime.

private boolean changeGroupTime(CommandEvent commandEvent, Config config, User user, String userName, Raid raid, LocalDateTime newDateTime) {
    boolean groupChanged = false;
    final Set<RaidGroup> groups = raidRepository.getGroups(raid);
    final Set<EmoticonSignUpMessageListener> listenersToCheck = getListenersToCheck(commandEvent, config, user, raid, groups);
    for (EmoticonSignUpMessageListener listener : listenersToCheck) {
        final String raidId = raid.getId();
        final LocalDateTime currentStartAt = listener.getStartAt();
        if (currentStartAt != null && currentStartAt.equals(newDateTime)) {
            LOGGER.info("Group is already at input time.");
            // This group is already at the time to change to
            commandEvent.reactError();
        } else if (currentStartAt != null) {
            LOGGER.info("Changing group time from " + currentStartAt + " to " + newDateTime);
            RaidGroup raidGroup = raidRepository.changeGroup(user, raidId, listener.getUserId(), currentStartAt, newDateTime);
            raidRepository.moveAllSignUpsForTimeToNewTime(raidId, currentStartAt, newDateTime, user);
            listener.setStartAt(newDateTime);
            groupChanged = true;
            replyBasedOnConfigAndRemoveAfter(config, commandEvent, localeService.getMessageFor(LocaleService.MOVED_GROUP, localeService.getLocaleForUser(user), printTimeIfSameDay(currentStartAt), printTimeIfSameDay(newDateTime), raid.getGym().getName()), BotServerMain.timeToRemoveFeedbackInSeconds);
            LOGGER.info("Group time changed. Group: " + raidGroup);
            commandEvent.reactSuccess();
        } else {
            LOGGER.info("Group is about to get cleaned up.");
            commandEvent.reactError();
            // This group is about to get cleaned up since its start time is null
            replyBasedOnConfigAndRemoveAfter(config, commandEvent, localeService.getMessageFor(LocaleService.GROUP_CLEANING_UP, localeService.getLocaleForUser(user)), BotServerMain.timeToRemoveFeedbackInSeconds);
            return true;
        }
    }
    if (!groupChanged) {
        throw new UserMessedUpException(userName, localeService.getMessageFor(LocaleService.BAD_SYNTAX, localeService.getLocaleForUser(user), "!raid change group 10:00 solna platform"));
    }
    return false;
}
Also used : LocalDateTime(java.time.LocalDateTime) EmoticonSignUpMessageListener(pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener) UserMessedUpException(pokeraidbot.domain.errors.UserMessedUpException) RaidGroup(pokeraidbot.infrastructure.jpa.raid.RaidGroup)

Example 3 with EmoticonSignUpMessageListener

use of pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener in project pokeraidbot by magnusmickelsson.

the class StartUpEventListener method attachToGroupMessage.

private boolean attachToGroupMessage(Guild guild, Config config, RaidGroup raidGroup) {
    MessageChannel channel = null;
    try {
        final List<TextChannel> textChannels = guild.getTextChannels();
        for (TextChannel textChannel : textChannels) {
            if (textChannel.getName().equalsIgnoreCase(raidGroup.getChannel())) {
                channel = textChannel;
                break;
            }
        }
        final Locale locale = config.getLocale();
        Raid raid = raidRepository.getById(raidGroup.getRaidId());
        final EmoticonSignUpMessageListener emoticonSignUpMessageListener = new EmoticonSignUpMessageListener(botService, localeService, serverConfigRepository, raidRepository, pokemonRepository, gymRepository, raid.getId(), raidGroup.getStartsAt(), raidGroup.getCreatorId());
        emoticonSignUpMessageListener.setEmoteMessageId(raidGroup.getEmoteMessageId());
        emoticonSignUpMessageListener.setInfoMessageId(raidGroup.getInfoMessageId());
        final int delayTime = raid.isExRaid() ? 1 : 15;
        final TimeUnit delayTimeUnit = raid.isExRaid() ? TimeUnit.MINUTES : TimeUnit.SECONDS;
        final Callable<Boolean> groupEditTask = NewRaidGroupCommand.getMessageRefreshingTaskToSchedule(channel, raid, emoticonSignUpMessageListener, raidGroup.getInfoMessageId(), locale, raidRepository, localeService, clockService, executorService, botService, delayTimeUnit, delayTime, raidGroup.getId());
        executorService.submit(groupEditTask);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Found group message for raid " + raid + " in channel " + (channel == null ? "N/A" : channel.getName()) + " (server " + guild.getName() + "). Attaching to it.");
        }
        return true;
    } catch (UserMessedUpException e) {
        if (channel != null)
            channel.sendMessage(e.getMessage()).queue(m -> {
                m.delete().queueAfter(BotServerMain.timeToRemoveFeedbackInSeconds, TimeUnit.SECONDS);
            });
    } catch (ErrorResponseException e) {
        // We couldn't find the message in this channel or had permission issues, ignore
        LOGGER.info("Caught exception during startup: " + e.getMessage());
        LOGGER.warn("Cleaning up raidgroup...");
        cleanUpRaidGroup(raidGroup);
        LOGGER.debug("Stacktrace:", e);
    } catch (Throwable e) {
        LOGGER.warn("Cleaning up raidgroup due to exception: " + e.getMessage());
        cleanUpRaidGroup(raidGroup);
    }
    return false;
}
Also used : Locale(java.util.Locale) Raid(pokeraidbot.domain.raid.Raid) TextChannel(net.dv8tion.jda.core.entities.TextChannel) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) EmoticonSignUpMessageListener(pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener) TimeUnit(java.util.concurrent.TimeUnit) UserMessedUpException(pokeraidbot.domain.errors.UserMessedUpException) ErrorResponseException(net.dv8tion.jda.core.exceptions.ErrorResponseException)

Example 4 with EmoticonSignUpMessageListener

use of pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener in project pokeraidbot by magnusmickelsson.

the class NewRaidGroupCommand method createRaidGroup.

public static void createRaidGroup(MessageChannel channel, Guild guild, Config config, User user, Locale locale, LocalTime startAtTime, String raidId, LocaleService localeService, RaidRepository raidRepository, BotService botService, ServerConfigRepository serverConfigRepository, PokemonRepository pokemonRepository, GymRepository gymRepository, ClockService clockService, ExecutorService executorService, PokemonRaidStrategyService pokemonRaidStrategyService) {
    assertAllParametersNotNullOrEmpty(channel, config, user, locale, startAtTime, raidId, localeService, raidRepository, botService, serverConfigRepository, pokemonRepository, gymRepository, clockService, executorService);
    Raid raid = raidRepository.getById(raidId);
    final LocalDate raidDate = raid.getEndOfRaid().toLocalDate();
    final LocalDateTime raidStart = Utils.getStartOfRaid(raid.getEndOfRaid(), raid.isExRaid());
    LocalDateTime startAt = LocalDateTime.of(raidDate, startAtTime);
    checkValidInput(user, locale, startAtTime, localeService, raidRepository, raid, raidStart, startAt);
    final EmoticonSignUpMessageListener emoticonSignUpMessageListener = new EmoticonSignUpMessageListener(botService, localeService, serverConfigRepository, raidRepository, pokemonRepository, gymRepository, raid.getId(), startAt, user);
    TimeUnit delayTimeUnit = raid.isExRaid() ? TimeUnit.MINUTES : TimeUnit.SECONDS;
    int delay = raid.isExRaid() ? 1 : 15;
    final MessageEmbed messageEmbed = getRaidGroupMessageEmbed(startAt, raid.getId(), localeService, clockService, locale, delayTimeUnit, delay, raidRepository);
    channel.sendMessage(messageEmbed).queue(embed -> {
        final String messageId = embed.getId();
        emoticonSignUpMessageListener.setInfoMessageId(messageId);
        emoticonSignUpMessageListener.setEmoteMessageId(messageId);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Thread: " + Thread.currentThread().getId() + " - Adding event listener and emotes for emote message with ID: " + messageId);
        }
        final MessageChannel embedChannel = embed.getChannel();
        RaidGroup group = new RaidGroup(config.getServer(), embedChannel.getName(), messageId, messageId, user.getId(), startAt);
        group = raidRepository.newGroupForRaid(user, group, raid, guild, config);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Created group in channel " + channel.getName() + " for emote message with ID: " + messageId + " - " + group);
        }
        // Add number icons for signups
        embedChannel.addReactionById(messageId, Emotes.ONE).queue();
        embedChannel.addReactionById(messageId, Emotes.TWO).queue();
        embedChannel.addReactionById(messageId, Emotes.THREE).queue();
        embedChannel.addReactionById(messageId, Emotes.FOUR).queue();
        embedChannel.addReactionById(messageId, Emotes.FIVE).queue();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Eventlistener and emotes added for emote message with ID: " + messageId);
        }
        if (config.isPinGroups()) {
            embedChannel.pinMessageById(embed.getId()).queueAfter(50, TimeUnit.MILLISECONDS);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Pinning info message for raid group. ID is: " + embed.getId());
            }
        }
        final Callable<Boolean> refreshEditThreadTask = getMessageRefreshingTaskToSchedule(channel, raid, emoticonSignUpMessageListener, messageId, locale, raidRepository, localeService, clockService, executorService, botService, delayTimeUnit, delay, group.getId());
        executorService.submit(refreshEditThreadTask);
    });
}
Also used : LocalDateTime(java.time.LocalDateTime) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) EmoticonSignUpMessageListener(pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener) TimeUnit(java.util.concurrent.TimeUnit) RaidGroup(pokeraidbot.infrastructure.jpa.raid.RaidGroup) Raid(pokeraidbot.domain.raid.Raid) LocalDate(java.time.LocalDate)

Example 5 with EmoticonSignUpMessageListener

use of pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener in project pokeraidbot by magnusmickelsson.

the class AlterRaidCommand method getListenersForUser.

private Set<EmoticonSignUpMessageListener> getListenersForUser(User user, Raid raid) {
    Set<EmoticonSignUpMessageListener> listenersToCheck;
    listenersToCheck = new HashSet<>();
    for (Object o : botService.getBot().getRegisteredListeners()) {
        if (o instanceof EmoticonSignUpMessageListener) {
            EmoticonSignUpMessageListener listener = (EmoticonSignUpMessageListener) o;
            final String raidId = raid.getId();
            final boolean isCorrectRaid = raidId.equals(listener.getRaidId());
            final boolean isUsersGroup = user.getId().equals(listener.getUserId());
            if (isCorrectRaid && isUsersGroup) {
                listenersToCheck.add(listener);
                // If we found user's group, that's the one to change primarily
                break;
            }
        }
    }
    return listenersToCheck;
}
Also used : EmoticonSignUpMessageListener(pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener)

Aggregations

EmoticonSignUpMessageListener (pokeraidbot.domain.raid.signup.EmoticonSignUpMessageListener)8 UserMessedUpException (pokeraidbot.domain.errors.UserMessedUpException)4 Raid (pokeraidbot.domain.raid.Raid)4 RaidGroup (pokeraidbot.infrastructure.jpa.raid.RaidGroup)4 LocalDateTime (java.time.LocalDateTime)3 MessageChannel (net.dv8tion.jda.core.entities.MessageChannel)3 TimeUnit (java.util.concurrent.TimeUnit)2 Gym (pokeraidbot.domain.gym.Gym)2 LocalDate (java.time.LocalDate)1 LocalTime (java.time.LocalTime)1 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)1 TextChannel (net.dv8tion.jda.core.entities.TextChannel)1 ErrorResponseException (net.dv8tion.jda.core.exceptions.ErrorResponseException)1