Search in sources :

Example 1 with GuildSettings

use of com.greatmancode.legendarybot.api.server.GuildSettings in project legendarybot by greatman.

the class WoWLinkPlugin method setDiscordRank.

/**
 * Set the Discord rank of a player. If the bot doesn't find the rank in the Discord Guild or can't set the rank, nothing happens.
 * @param user The user to set the rank to.
 * @param guild The guild we want to set the rank in
 * @param rank The rank we want to set.
 */
public void setDiscordRank(User user, Guild guild, String rank) {
    if (rank == null) {
        return;
    }
    List<Role> botRole = guild.getMember(getBot().getJDA(guild).getSelfUser()).getRoles();
    final int[] botRoleRank = { -999 };
    botRole.forEach(r -> {
        if (r.getPosition() > botRoleRank[0]) {
            botRoleRank[0] = r.getPosition();
        }
    });
    List<Role> rolesToAdd = guild.getRolesByName(rank, true);
    if (rolesToAdd.isEmpty()) {
        return;
    }
    if (rolesToAdd.get(0).getPosition() > botRoleRank[0]) {
        // Can't set a rank higher than us.
        return;
    }
    // We try to load all the other ranks so we do a cleanup at the same time.
    Set<String> allRanks = new HashSet<>();
    GuildSettings settings = getBot().getGuildSettings(guild);
    for (int i = 0; i <= 9; i++) {
        String entry = settings.getSetting(SETTING_RANK_PREFIX + i);
        if (entry != null) {
            allRanks.add(entry);
        }
    }
    if (allRanks.contains(rank)) {
        allRanks.remove(rank);
    }
    List<Role> rolesToRemove = new ArrayList<>();
    List<Role> memberRoles = guild.getMember(user).getRoles();
    for (String rankEntry : allRanks) {
        List<Role> roles = guild.getRolesByName(rankEntry, false);
        if (!roles.isEmpty()) {
            if (memberRoles.contains(roles.get(0))) {
                if (roles.get(0).getPosition() < botRoleRank[0]) {
                    rolesToRemove.add(roles.get(0));
                } else {
                    log.info("The bot can't remove rank " + rankEntry + " on user " + user.getName());
                }
            } else {
                log.info("Role " + rankEntry + " not on user" + user.getName());
            }
        } else {
            log.info("Role " + rankEntry + " not found!");
        }
    }
    try {
        guild.getController().modifyMemberRoles(guild.getMember(user), rolesToAdd, rolesToRemove).reason("LegendaryBot - Rank Sync with WoW Guild.").queue();
        log.info("User " + user.getName());
        log.info("Adding ranks:");
        rolesToAdd.forEach(v -> log.info(v.getName()));
        log.info("Removing ranks:");
        rolesToRemove.forEach(v -> log.info(v.getName()));
    } catch (PermissionException e) {
        e.printStackTrace();
        getBot().getStacktraceHandler().sendStacktrace(e);
    }
}
Also used : Role(net.dv8tion.jda.core.entities.Role) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) GuildSettings(com.greatmancode.legendarybot.api.server.GuildSettings)

Example 2 with GuildSettings

use of com.greatmancode.legendarybot.api.server.GuildSettings in project legendarybot by greatman.

the class SetupMessageListener method onMessageReceived.

@Override
public void onMessageReceived(MessageReceivedEvent event) {
    if (event.isFromType(ChannelType.PRIVATE) || event.getAuthor().isBot()) {
        return;
    }
    SetupHandler setupHandler = plugin.getSetupHandler(event.getGuild());
    if (setupHandler == null) {
        return;
    }
    if (!setupHandler.getUser().equals(event.getAuthor())) {
        return;
    }
    if (!setupHandler.getChannel().equals(event.getChannel())) {
        return;
    }
    if (event.getMessage().getContentDisplay().contains("setup")) {
        return;
    }
    if (event.getMessage().getContentDisplay().equalsIgnoreCase("cancel")) {
        plugin.setupDone(event.getGuild());
        event.getChannel().sendMessage("Setup canceled. The bot may behave weirdly if you left right in the middle of the setup wizard.").queue();
        return;
    }
    if (setupHandler.getState() == SetupState.STEP_PREFIX) {
        String prefix = event.getMessage().getContentDisplay();
        plugin.getBot().getGuildSettings(event.getGuild()).setSetting("PREFIX", prefix);
        setupHandler.setState(SetupState.STEP_REGION);
        event.getChannel().sendMessage("Awesome! I will now answer with the prefix ``" + prefix + "`` . Example: ``" + prefix + "help``").queue();
        StringBuilder builder = new StringBuilder();
        builder.append("Let's move to the next setting.\n");
        builder.append("I have multiple commands that supports a World of Warcraft region.\n");
        builder.append("The regions I support are **US** (North America / Oceanic / Latin America / Brazil) or **EU**\n\n");
        builder.append("**Default Value**: ``None``\n");
        String region = plugin.getBot().getGuildSettings(event.getGuild()).getSetting("WOW_REGION_NAME");
        if (region == null) {
            region = "None";
        }
        builder.append("**Current Setting**: ``" + region + "``\n");
        builder.append("**Examples**: ``US`` ``EU``\n");
        builder.append("So, which **region** you want to use?");
        event.getChannel().sendMessage(builder.toString()).queue();
        return;
    }
    if (setupHandler.getState() == SetupState.STEP_REGION) {
        String region = event.getMessage().getContentDisplay();
        if (!region.equalsIgnoreCase("us") && !region.equalsIgnoreCase("eu")) {
            event.getChannel().sendMessage("I only support ``US`` or ``EU``. Please type one or the other.").queue();
            return;
        }
        plugin.getBot().getGuildSettings(event.getGuild()).setSetting("WOW_REGION_NAME", region.toUpperCase());
        setupHandler.setState(SetupState.STEP_REALM);
        event.getChannel().sendMessage("Perfect! I will remember that your Discord server plays on the **" + region.toUpperCase() + "** realms.").queue();
        StringBuilder builder = new StringBuilder();
        builder.append("Let's move to the next setting.\n");
        builder.append("Same thing as the previous setting, but with your **Realm**\n");
        builder.append("A **Realm** is the primary World of Warcraft server you play on.\n");
        builder.append("Don't worry about how you type it. I should find it without a problem!\n\n");
        builder.append("**Default Value**: ``None``\n");
        String realm = plugin.getBot().getGuildSettings(event.getGuild()).getSetting("WOW_SERVER_NAME");
        if (realm == null) {
            realm = "None";
        }
        builder.append("**Current Setting**: ``" + realm + "``\n");
        builder.append("**Examples**: ``Emerald Dream``\n");
        builder.append("So, which **Realm** do you play on?");
        event.getChannel().sendMessage(builder.toString()).queue();
        return;
    }
    if (setupHandler.getState() == SetupState.STEP_REALM) {
        String realm = event.getMessage().getContentDisplay();
        try {
            String realmData = WoWUtils.getRealmInformation(plugin.getBot(), plugin.getBot().getGuildSettings(event.getGuild()).getRegionName(), realm);
            if (realmData == null) {
                event.getChannel().sendMessage("I did not found a Realm with the name of " + realm + ". Did you make a typo?").queue();
                return;
            }
            JSONParser jsonParser = new JSONParser();
            JSONObject source = (JSONObject) jsonParser.parse(realmData);
            String serverSlug = (String) source.get("slug");
            String timezone = (String) source.get("timezone");
            String name = (String) source.get("name");
            String pvp = (String) source.get("type");
            LocalDateTime time = LocalDateTime.now(ZoneId.of(timezone));
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
            setupHandler.setTempValue(serverSlug);
            event.getChannel().sendMessage("I found the Realm **" + name + "**. The type of the Realm is **" + pvp.toUpperCase() + "** and the current Realm time is **" + formatter.format(time) + "**. Is this correct?").queue();
            event.getChannel().sendMessage("Say ``yes`` or ``no``.").queue();
            setupHandler.setState(SetupState.STEP_REALM_CONFIRM);
        } catch (ParseException e) {
            e.printStackTrace();
            plugin.getBot().getStacktraceHandler().sendStacktrace(e, "guildid:" + event.getGuild().getId(), "channel:" + event.getChannel().getName(), "realm:" + realm + "setupwizard:" + setupHandler.getState());
            event.getChannel().sendMessage("I did not found a Realm with the name of " + realm + ". Did you make a typo?").queue();
        }
        return;
    }
    if (setupHandler.getState() == SetupState.STEP_REALM_CONFIRM) {
        String confirmation = event.getMessage().getContentDisplay();
        if (confirmation.equalsIgnoreCase("no")) {
            event.getChannel().sendMessage("Please type the realm name you want to configure.").queue();
            setupHandler.setState(SetupState.STEP_REALM);
        } else if (confirmation.equalsIgnoreCase("yes")) {
            plugin.getBot().getGuildSettings(event.getGuild()).setSetting("WOW_SERVER_NAME", setupHandler.getTempValue());
            StringBuilder builder = new StringBuilder();
            builder.append("Wonderful! I will now remember that **" + setupHandler.getTempValue() + "** is your preferred realm!\n\n");
            builder.append("We got 1 last setting to configure.\n");
            builder.append("LegendaryBot supports World of Warcraft **Guilds**. It makes commands like ``!log`` find the latest Warcraft Logs entry of your guild.\n\n");
            builder.append("**Default Value**: ``None``\n");
            String guild = plugin.getBot().getGuildSettings(event.getGuild()).getSetting("GUILD_NAME");
            if (guild == null) {
                guild = "None";
            }
            builder.append("**Current Setting**: ``" + guild + "``\n");
            builder.append("So, what is your **Guild name**? If you don't want to configure one, you can simply exit the setup with the word ``cancel``");
            event.getChannel().sendMessage(builder.toString()).queue();
            setupHandler.setState(SetupState.STEP_GUILD);
        } else {
            event.getChannel().sendMessage("Please say ``yes`` or ``no``.").queue();
        }
        return;
    }
    if (setupHandler.getState() == SetupState.STEP_GUILD) {
        String guild = event.getMessage().getContentDisplay();
        GuildSettings settings = plugin.getBot().getGuildSettings(event.getGuild());
        try {
            if (guildExist(settings.getRegionName(), settings.getWowServerName(), guild)) {
                settings.setSetting("GUILD_NAME", guild);
                event.getChannel().sendMessage("Such a great Guild name! I will remember that your guild is **" + guild + "**").queue();
                event.getChannel().sendMessage("That's it! We have finished the setup. For further help, feel free to use the ``" + plugin.getBot().getGuildSettings(event.getGuild()).getSetting("PREFIX") + "help`` command or visit the LegendaryBot server @ https://discord.gg/Cr7G28H").queue();
                plugin.setupDone(event.getGuild());
            } else {
                event.getChannel().sendMessage("Guild **" + guild + "** not found! Did you make a typo?").queue();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return;
    }
// TODO other features setup
}
Also used : LocalDateTime(java.time.LocalDateTime) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException) DateTimeFormatter(java.time.format.DateTimeFormatter) GuildSettings(com.greatmancode.legendarybot.api.server.GuildSettings)

Aggregations

GuildSettings (com.greatmancode.legendarybot.api.server.GuildSettings)2 IOException (java.io.IOException)1 LocalDateTime (java.time.LocalDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Role (net.dv8tion.jda.core.entities.Role)1 PermissionException (net.dv8tion.jda.core.exceptions.PermissionException)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1 ParseException (org.json.simple.parser.ParseException)1