Search in sources :

Example 6 with DBGuild

use of me.shadorc.shadbot.data.db.DBGuild in project Shadbot by Shadorc.

the class AutoRoleSetting method execute.

@Override
public void execute(Context context, String arg) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!BotUtils.hasPermissions(context.getChannel(), Permissions.MANAGE_ROLES)) {
        BotUtils.sendMessage(TextUtils.missingPerm(Permissions.MANAGE_ROLES), context.getChannel());
        LogUtils.infof("{Guild ID: %d} Shadbot wasn't allowed to manage roles.", context.getGuild().getLongID());
        return;
    }
    if (arg == null) {
        throw new MissingArgumentException();
    }
    List<String> splitArgs = StringUtils.split(arg);
    if (splitArgs.size() != 2) {
        throw new MissingArgumentException();
    }
    Action action = Utils.getValueOrNull(Action.class, splitArgs.get(0));
    if (action == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid action. %s", splitArgs.get(0), FormatUtils.formatOptions(Action.class)));
    }
    List<IRole> mentionedRoles = context.getMessage().getRoleMentions();
    if (mentionedRoles.isEmpty()) {
        throw new MissingArgumentException();
    }
    DBGuild dbGuild = Database.getDBGuild(context.getGuild());
    List<Long> roles = dbGuild.getAutoRoles();
    if (Action.ADD.equals(action)) {
        for (IRole role : mentionedRoles) {
            if (!PermissionUtils.hasHierarchicalPermissions(context.getGuild(), context.getOurUser(), Arrays.asList(role))) {
                throw new IllegalCmdArgumentException(String.format("%s is a higher role in the role hierarchy than mine, I can't auto-assign it.", role.mention()));
            }
        }
        roles.addAll(mentionedRoles.stream().map(IRole::getLongID).collect(Collectors.toList()));
        BotUtils.sendMessage(String.format("New comers will now have role(s): %s", FormatUtils.format(roles, role -> context.getGuild().getRoleByID(role).mention(), ", ")), context.getChannel());
    } else {
        roles.removeAll(mentionedRoles.stream().map(IRole::getLongID).collect(Collectors.toList()));
        BotUtils.sendMessage(String.format("%s removed from auto-assigned roles.", FormatUtils.format(mentionedRoles, IRole::mention, ", ")), context.getChannel());
    }
    dbGuild.setSetting(SettingEnum.AUTO_ROLE, new JSONArray(roles));
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) DBGuild(me.shadorc.shadbot.data.db.DBGuild) IRole(sx.blah.discord.handle.obj.IRole) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONArray(org.json.JSONArray)

Example 7 with DBGuild

use of me.shadorc.shadbot.data.db.DBGuild in project Shadbot by Shadorc.

the class BlacklistSettingCmd method execute.

@Override
public void execute(Context context, String arg) throws MissingArgumentException, IllegalCmdArgumentException {
    if (arg == null) {
        throw new MissingArgumentException();
    }
    List<String> splitArgs = StringUtils.split(arg, 2);
    if (splitArgs.size() != 2) {
        throw new MissingArgumentException();
    }
    Action action = Utils.getValueOrNull(Action.class, splitArgs.get(0));
    if (action == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid action. %s", splitArgs.get(0), FormatUtils.formatOptions(Action.class)));
    }
    List<String> commands = StringUtils.split(splitArgs.get(1).toLowerCase());
    List<String> unknownCmds = commands.stream().filter(cmd -> CommandManager.getCommand(cmd) == null).collect(Collectors.toList());
    if (!unknownCmds.isEmpty()) {
        throw new IllegalCmdArgumentException(String.format("Command %s doesn't exist.", FormatUtils.format(unknownCmds, cmd -> String.format("`%s`", cmd), ", ")));
    }
    DBGuild dbGuild = Database.getDBGuild(context.getGuild());
    List<String> blacklist = dbGuild.getBlacklistedCmd();
    if (Action.ADD.equals(action)) {
        blacklist.addAll(commands);
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Command(s) `%s` added to the blacklist.", FormatUtils.format(commands, cmd -> String.format("`%s`", cmd), ", ")), context.getChannel());
    } else if (Action.REMOVE.equals(action)) {
        blacklist.removeAll(commands);
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Command(s) `%s` removed from the blacklist.", FormatUtils.format(commands, cmd -> String.format("`%s`", cmd), ", ")), context.getChannel());
    }
    dbGuild.setSetting(this.getSetting(), new JSONArray(blacklist));
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) SettingEnum(me.shadorc.shadbot.command.admin.setting.core.SettingEnum) DBGuild(me.shadorc.shadbot.data.db.DBGuild) AbstractSetting(me.shadorc.shadbot.command.admin.setting.core.AbstractSetting) CommandManager(me.shadorc.shadbot.core.command.CommandManager) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) Collectors(java.util.stream.Collectors) BotUtils(me.shadorc.shadbot.utils.BotUtils) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) StringUtils(me.shadorc.shadbot.utils.StringUtils) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) List(java.util.List) Context(me.shadorc.shadbot.core.command.Context) Database(me.shadorc.shadbot.data.db.Database) Setting(me.shadorc.shadbot.command.admin.setting.core.Setting) Emoji(me.shadorc.shadbot.utils.object.Emoji) EmbedUtils(me.shadorc.shadbot.utils.embed.EmbedUtils) JSONArray(org.json.JSONArray) Utils(me.shadorc.shadbot.utils.Utils) DBGuild(me.shadorc.shadbot.data.db.DBGuild) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONArray(org.json.JSONArray)

Example 8 with DBGuild

use of me.shadorc.shadbot.data.db.DBGuild in project Shadbot by Shadorc.

the class BotUtils method isChannelAllowed.

public static boolean isChannelAllowed(IGuild guild, IChannel channel) {
    DBGuild dbGuild = Database.getDBGuild(guild);
    List<Long> allowedChannels = dbGuild.getAllowedChannels();
    // If no permission has been set, allow all channels
    if (allowedChannels.isEmpty()) {
        return true;
    }
    return allowedChannels.contains(channel.getLongID());
}
Also used : DBGuild(me.shadorc.shadbot.data.db.DBGuild)

Example 9 with DBGuild

use of me.shadorc.shadbot.data.db.DBGuild in project Shadbot by Shadorc.

the class GuildMemberListener method onUserJoinEvent.

private void onUserJoinEvent(UserJoinEvent event) {
    DBGuild dbGuild = Database.getDBGuild(event.getGuild());
    Long channelID = dbGuild.getMessageChannelID();
    String joinMsg = dbGuild.getJoinMessage();
    this.sendAutoMsg(event.getGuild(), channelID, joinMsg);
    List<IRole> roles = dbGuild.getAutoRoles().stream().map(event.getGuild()::getRoleByID).filter(Objects::nonNull).collect(Collectors.toList());
    if (BotUtils.hasPermissions(event.getGuild(), Permissions.MANAGE_ROLES) && BotUtils.canInteract(event.getGuild(), event.getUser()) && PermissionUtils.hasHierarchicalPermissions(event.getGuild(), event.getClient().getOurUser(), roles)) {
        RequestBuffer.request(() -> {
            event.getGuild().editUserRoles(event.getUser(), roles.toArray(new IRole[roles.size()]));
        });
    }
}
Also used : DBGuild(me.shadorc.shadbot.data.db.DBGuild) IRole(sx.blah.discord.handle.obj.IRole)

Example 10 with DBGuild

use of me.shadorc.shadbot.data.db.DBGuild in project Shadbot by Shadorc.

the class DatabaseCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    List<String> splitArgs = StringUtils.split(context.getArg());
    if (splitArgs.size() > 2) {
        throw new MissingArgumentException();
    }
    Long guildID = CastUtils.asPositiveLong(splitArgs.get(0));
    if (guildID == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid guild ID.", splitArgs.get(0)));
    }
    IGuild guild = context.getClient().getGuildByID(guildID);
    if (guild == null) {
        throw new IllegalCmdArgumentException("Guild not found.");
    }
    String json = null;
    if (splitArgs.size() == 1) {
        DBGuild dbGuild = Database.getDBGuild(guild);
        json = dbGuild.toJSON().toString(Config.JSON_INDENT_FACTOR);
    } else if (splitArgs.size() == 2) {
        Long userID = CastUtils.asPositiveLong(splitArgs.get(1));
        if (userID == null) {
            throw new IllegalCmdArgumentException(String.format("`%s` is not a valid user ID.", splitArgs.get(0)));
        }
        DBUser dbUser = new DBUser(guild, userID);
        json = dbUser.toJSON().toString(Config.JSON_INDENT_FACTOR);
    }
    if (json == null || json.length() == 2) {
        BotUtils.sendMessage(Emoji.MAGNIFYING_GLASS + " Nothing found.", context.getChannel());
    } else {
        BotUtils.sendMessage(json, context.getChannel());
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) DBGuild(me.shadorc.shadbot.data.db.DBGuild) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) DBUser(me.shadorc.shadbot.data.db.DBUser) IGuild(sx.blah.discord.handle.obj.IGuild)

Aggregations

DBGuild (me.shadorc.shadbot.data.db.DBGuild)12 MissingArgumentException (me.shadorc.shadbot.exception.MissingArgumentException)7 IllegalCmdArgumentException (me.shadorc.shadbot.exception.IllegalCmdArgumentException)4 JSONArray (org.json.JSONArray)4 IChannel (sx.blah.discord.handle.obj.IChannel)3 IRole (sx.blah.discord.handle.obj.IRole)3 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)3 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 AbstractSetting (me.shadorc.shadbot.command.admin.setting.core.AbstractSetting)2 Setting (me.shadorc.shadbot.command.admin.setting.core.Setting)2 SettingEnum (me.shadorc.shadbot.command.admin.setting.core.SettingEnum)2 Context (me.shadorc.shadbot.core.command.Context)2 Database (me.shadorc.shadbot.data.db.Database)2 BotUtils (me.shadorc.shadbot.utils.BotUtils)2 FormatUtils (me.shadorc.shadbot.utils.FormatUtils)2 StringUtils (me.shadorc.shadbot.utils.StringUtils)2 Utils (me.shadorc.shadbot.utils.Utils)2 EmbedUtils (me.shadorc.shadbot.utils.embed.EmbedUtils)2 Emoji (me.shadorc.shadbot.utils.object.Emoji)2