Search in sources :

Example 1 with UserConfig

use of pokeraidbot.infrastructure.jpa.config.UserConfig in project pokeraidbot by magnusmickelsson.

the class AdminCommands method execute.

@Override
protected void execute(CommandEvent event) {
    final User user = event.getAuthor();
    if (user == null || user.getId() == null || (!user.getId().equals(BotServerMain.BOT_CREATOR_USERID))) {
        event.replyInDM("This command is reserved only for bot creator. Hands off! ;p Your user ID was: " + String.valueOf(user.getId()));
        return;
    } else {
        final String eventArgs = event.getArgs();
        if (eventArgs.startsWith("userconfig")) {
            String userId = eventArgs.replaceAll("userconfig\\s{1,3}", "");
            final UserConfig userConfig = userConfigRepository.findOne(userId);
            if (userConfig == null) {
                event.replyInDM("No user with ID " + userId);
                return;
            } else {
                userConfigRepository.delete(userConfig);
                event.replyInDM("Removed user configuration for user with ID " + userId);
                return;
            }
        } else if (eventArgs.startsWith("permissions")) {
            final JDA bot = botService.getBot();
            final List<Guild> guilds = bot.getGuilds();
            StringBuilder sb = new StringBuilder();
            sb.append("**Permissions for bot across servers:**\n\n");
            for (Guild guild : guilds) {
                final Member member = guild.getMember(bot.getSelfUser());
                if (member == null) {
                    event.replyInDM("Could not get bot as servermember!");
                    return;
                }
                sb.append("*").append(guild.getName()).append("*\n");
                for (Permission p : member.getPermissions()) {
                    sb.append(p.getName()).append("(Guild: ").append(p.isGuild()).append(" Channel: ").append(p.isChannel()).append(")\n");
                }
                sb.append("\n\n");
            }
            event.replyInDM(sb.toString());
            return;
        } else if (eventArgs.startsWith("clear tracking")) {
            trackingCommandListener.clearCache();
            event.replyInDM("Cleared tracking cache.");
            return;
        } else if (eventArgs.startsWith("announce")) {
            final JDA bot = botService.getBot();
            final List<Guild> guilds = bot.getGuilds();
            StringBuilder sb = new StringBuilder();
            for (Guild guild : guilds) {
                try {
                    guild.getDefaultChannel().sendMessage(eventArgs.replaceAll("announce\\s{1,3}", "")).queue();
                    sb.append("Sent message for guild ").append(guild.getName()).append("\n");
                } catch (Throwable t) {
                    sb.append("Failed to send message for guild ").append(guild.getName()).append(": ").append(t.getMessage()).append("\n");
                }
            }
            event.replyInDM(sb.toString());
            return;
        } else if (eventArgs.startsWith("ismember")) {
            String userIdAndGuildName = eventArgs.replaceAll("ismember\\s{1,3}", "");
            String[] args = userIdAndGuildName.split(" ");
            if (args.length < 2) {
                event.reply("Bad syntax, should be something like: !raid admin ismember {userid} {guildname}");
                return;
            } else {
                final JDA bot = botService.getBot();
                Guild guild = null;
                final List<Guild> guilds = bot.getGuilds();
                String guildName = StringUtils.join(ArrayUtils.remove(args, 0), " ");
                for (Guild guildToCheck : guilds) {
                    if (guildToCheck.getName().equalsIgnoreCase(guildName)) {
                        guild = guildToCheck;
                    }
                }
                if (guild != null) {
                    final Member memberById = guild.getMemberById(args[0]);
                    if (memberById != null) {
                        event.reply("User is a member of server " + guild.getName());
                    } else {
                        event.reply("User is not a member of server " + guild.getName());
                    }
                } else {
                    event.reply("There was no server the user is a member of.");
                }
                return;
            }
        } else if (eventArgs.startsWith("member")) {
            String userIdAndGuildName = eventArgs.replaceAll("member\\s{1,3}", "");
            String[] args = userIdAndGuildName.split(" ");
            if (args.length < 1 || args.length > 2) {
                event.reply("Bad syntax, should be something like: !raid admin member {userid}");
                return;
            } else {
                StringBuilder sb = new StringBuilder();
                final JDA bot = botService.getBot();
                final List<Guild> guilds = bot.getGuilds();
                sb.append("User with ID ").append(args[0]).append(" is a member of the following servers:\n\n");
                if (guilds.size() == 0) {
                    sb.append("-");
                }
                for (Guild guild : guilds) {
                    final Member memberById = guild.getMemberById(args[0]);
                    if (memberById != null) {
                        sb.append(guild.getName()).append(" (Username ").append(memberById.getUser().getName()).append(")\n");
                    }
                }
                event.reply(sb.toString());
                return;
            }
        } else if (eventArgs.startsWith("guilds")) {
            final JDA bot = botService.getBot();
            final List<Guild> guilds = bot.getGuilds();
            StringBuilder sb = new StringBuilder();
            for (Guild guildToCheck : guilds) {
                sb.append(guildToCheck.getName().toLowerCase()).append("\n");
            }
            event.reply(sb.toString());
            return;
        } else if (eventArgs.startsWith("test")) {
            final Config configForServer = serverConfigRepository.getConfigForServer(event.getGuild().getName().toLowerCase());
            String[] args = eventArgs.replaceAll("test\\s{1,3}", "").trim().split(" ");
            String pokemon = args[0];
            LocalDateTime startsAt = LocalDateTime.of(LocalDate.now(), Utils.parseTime(user, args[1], localeService));
            String gymName = StringUtils.join(ArrayUtils.removeElements(args, 0, 1), " ").trim();
            final String region = configForServer.getRegion();
            Raid raid = new Raid(pokemonRepository.search(pokemon, user), startsAt.plusMinutes(Utils.RAID_DURATION_IN_MINUTES), gymRepository.search(user, gymName, region), localeService, region);
            final Raid createdRaid = raidRepository.newRaid(botService.getBot().getSelfUser(), raid, event.getGuild(), configForServer, event.getMessage().getRawContent());
            event.reply("Bot created your test raid: " + createdRaid);
            return;
        } else if (eventArgs.startsWith("tier5")) {
            String[] bosses = eventArgs.replaceAll("tier5\\s{1,3}", "").trim().split(";");
            if (bosses == null || bosses.length < 1) {
                event.reply("Bad syntax, should be: !raid admin tier5 Boss1;Boss2;Boss3");
                return;
            } else {
                final CopyOnWriteArrayList<String> currentTier5Bosses = new CopyOnWriteArrayList<>();
                currentTier5Bosses.addAll(Arrays.asList(bosses));
                BotService.currentTier5Bosses = currentTier5Bosses;
                event.reply("Set current tier5 boss list: " + StringUtils.join(bosses, ", "));
                return;
            }
        }
    }
    event.reply("No such command. Existing ones are:\n- userconfig {userid}\n- permissions\n" + "- clear tracking\n- announce {message}\n- ismember {userid} {guild name}\n- guilds\n" + " - member {userid}\n - test {pokemon} {start time} {gym}\n- tier5 {list of bosses ;-separated}");
}
Also used : LocalDateTime(java.time.LocalDateTime) User(net.dv8tion.jda.core.entities.User) JDA(net.dv8tion.jda.core.JDA) UserConfig(pokeraidbot.infrastructure.jpa.config.UserConfig) Config(pokeraidbot.infrastructure.jpa.config.Config) UserConfig(pokeraidbot.infrastructure.jpa.config.UserConfig) Guild(net.dv8tion.jda.core.entities.Guild) Raid(pokeraidbot.domain.raid.Raid) Permission(net.dv8tion.jda.core.Permission) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Member(net.dv8tion.jda.core.entities.Member) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with UserConfig

use of pokeraidbot.infrastructure.jpa.config.UserConfig in project pokeraidbot by magnusmickelsson.

the class TrackingService method removeFromDbAndCollection.

private void removeFromDbAndCollection(PokemonTrackingTarget trackingTarget, User user) {
    UserConfig userConfig = userConfigRepository.findOne(user.getId());
    if (userConfig != null) {
        userConfig.removeTrackingFor(trackingTarget.getPokemon());
        userConfigRepository.save(userConfig);
    }
    trackingTargets.remove(trackingTarget);
}
Also used : UserConfig(pokeraidbot.infrastructure.jpa.config.UserConfig)

Example 3 with UserConfig

use of pokeraidbot.infrastructure.jpa.config.UserConfig in project pokeraidbot by magnusmickelsson.

the class TrackingService method addToDbAndCollection.

private void addToDbAndCollection(PokemonTrackingTarget trackingTarget, User user, Config config) {
    UserConfig userConfig = userConfigRepository.findOne(user.getId());
    if (userConfig != null) {
        if (userConfig.hasFreeTrackingSpot()) {
            userConfig.setNextTrackingSpot(trackingTarget.getPokemon());
        } else {
            throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.TRACKING_NONE_FREE, localeService.getLocaleForUser(user)));
        }
    } else {
        // Per default, let user have the same locale as the server
        userConfig = new UserConfig(user.getId(), trackingTarget.getPokemon(), null, null, config.getLocale());
    }
    userConfigRepository.save(userConfig);
    trackingTargets.add(trackingTarget);
}
Also used : UserMessedUpException(pokeraidbot.domain.errors.UserMessedUpException) UserConfig(pokeraidbot.infrastructure.jpa.config.UserConfig)

Example 4 with UserConfig

use of pokeraidbot.infrastructure.jpa.config.UserConfig in project pokeraidbot by magnusmickelsson.

the class UserConfigCommand method setNickForUser.

private UserConfig setNickForUser(Config config, User user, UserConfig userConfig, String value) {
    if (value.length() > 10 || value.length() < 3) {
        throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.USER_NICK_INVALID, localeService.getLocaleForUser(user)));
    }
    if (userConfig == null) {
        final UserConfig entity = new UserConfig(user.getId(), null, null, null, config.getLocale());
        entity.setNick(value);
        userConfig = userConfigRepository.save(entity);
    } else {
        userConfig.setNick(value);
        userConfig = userConfigRepository.save(userConfig);
    }
    return userConfig;
}
Also used : UserMessedUpException(pokeraidbot.domain.errors.UserMessedUpException) UserConfig(pokeraidbot.infrastructure.jpa.config.UserConfig)

Example 5 with UserConfig

use of pokeraidbot.infrastructure.jpa.config.UserConfig in project pokeraidbot by magnusmickelsson.

the class UserConfigCommand method executeWithConfig.

@Override
protected void executeWithConfig(CommandEvent commandEvent, Config serverConfig) {
    final User user = commandEvent.getAuthor();
    UserConfig userConfig = userConfigRepository.findOne(user.getId());
    final String[] arguments = commandEvent.getArgs().split(" ");
    if (arguments.length > 1 || arguments.length < 1) {
        throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.USER_CONFIG_BAD_SYNTAX, localeService.getLocaleForUser(user)));
    }
    if ("show".equalsIgnoreCase(arguments[0])) {
        if (userConfig == null) {
            userConfig = userConfigRepository.save(new UserConfig(user.getId(), null, null, null, serverConfig.getLocale()));
        }
        replyBasedOnConfig(serverConfig, commandEvent, String.valueOf(userConfig));
    } else {
        final String[] paramAndValue = arguments[0].split("=");
        if (paramAndValue.length > 2 || paramAndValue.length < 2) {
            throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.USER_CONFIG_BAD_SYNTAX, localeService.getLocaleForUser(user)));
        }
        String param = paramAndValue[0];
        String value = paramAndValue[1];
        switch(param.toLowerCase()) {
            case "locale":
                final Locale newLocale = setLocaleForUser(user, userConfig, value);
                replyBasedOnConfigAndRemoveAfter(serverConfig, commandEvent, localeService.getMessageFor(LocaleService.LOCALE_SET, localeService.getLocaleForUser(user), newLocale.getLanguage()), BotServerMain.timeToRemoveFeedbackInSeconds);
                break;
            case "nick":
                userConfig = setNickForUser(serverConfig, user, userConfig, value);
                replyBasedOnConfig(serverConfig, commandEvent, String.valueOf(userConfig));
                break;
            default:
                throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.USER_CONFIG_BAD_PARAM, localeService.getLocaleForUser(user), param));
        }
    }
}
Also used : Locale(java.util.Locale) User(net.dv8tion.jda.core.entities.User) UserMessedUpException(pokeraidbot.domain.errors.UserMessedUpException) UserConfig(pokeraidbot.infrastructure.jpa.config.UserConfig)

Aggregations

UserConfig (pokeraidbot.infrastructure.jpa.config.UserConfig)5 UserMessedUpException (pokeraidbot.domain.errors.UserMessedUpException)3 User (net.dv8tion.jda.core.entities.User)2 LocalDateTime (java.time.LocalDateTime)1 List (java.util.List)1 Locale (java.util.Locale)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 JDA (net.dv8tion.jda.core.JDA)1 Permission (net.dv8tion.jda.core.Permission)1 Guild (net.dv8tion.jda.core.entities.Guild)1 Member (net.dv8tion.jda.core.entities.Member)1 Raid (pokeraidbot.domain.raid.Raid)1 Config (pokeraidbot.infrastructure.jpa.config.Config)1