Search in sources :

Example 11 with DBGuild

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

the class ChannelSetting 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);
    if (splitArgs.size() < 2) {
        throw new MissingArgumentException();
    }
    List<IChannel> mentionedChannels = context.getMessage().getChannelMentions();
    if (mentionedChannels.isEmpty()) {
        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)));
    }
    DBGuild dbGuild = Database.getDBGuild(context.getGuild());
    List<Long> allowedChannelsList = dbGuild.getAllowedChannels();
    if (Action.ADD.equals(action)) {
        if (allowedChannelsList.isEmpty() && mentionedChannels.stream().noneMatch(channel -> channel.getLongID() == context.getChannel().getLongID())) {
            BotUtils.sendMessage(Emoji.WARNING + " You did not mentioned this channel. " + "I will not reply here until this channel is added to the list of allowed channels.", context.getChannel());
        }
        allowedChannelsList.addAll(mentionedChannels.stream().map(IChannel::getLongID).filter(channelID -> !allowedChannelsList.contains(channelID)).collect(Collectors.toList()));
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Channel %s added to allowed channels.", FormatUtils.format(mentionedChannels, IChannel::mention, ", ")), context.getChannel());
    } else {
        allowedChannelsList.removeAll(mentionedChannels.stream().map(IChannel::getLongID).collect(Collectors.toList()));
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Channel %s removed from allowed channels.", FormatUtils.format(mentionedChannels, IChannel::mention, ", ")), context.getChannel());
    }
    dbGuild.setSetting(this.getSetting(), new JSONArray(allowedChannelsList));
}
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) 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) IChannel(sx.blah.discord.handle.obj.IChannel) 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) IChannel(sx.blah.discord.handle.obj.IChannel) DBGuild(me.shadorc.shadbot.data.db.DBGuild) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONArray(org.json.JSONArray)

Example 12 with DBGuild

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

the class AutoMessageSetting method updateJoinMessage.

private void updateJoinMessage(Context context, Action action, List<String> args) throws MissingArgumentException {
    DBGuild dbGuild = Database.getDBGuild(context.getGuild());
    if (Action.ENABLE.equals(action)) {
        if (args.size() < 3) {
            throw new MissingArgumentException();
        }
        String message = args.get(2);
        dbGuild.setSetting(SettingEnum.JOIN_MESSAGE, message);
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Join message set to `%s`", message), context.getChannel());
    } else if (Action.DISABLE.equals(action)) {
        dbGuild.removeSetting(SettingEnum.JOIN_MESSAGE);
        BotUtils.sendMessage(Emoji.CHECK_MARK + " Join message disabled.", context.getChannel());
    }
}
Also used : DBGuild(me.shadorc.shadbot.data.db.DBGuild) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException)

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