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));
}
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));
}
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());
}
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()]));
});
}
}
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());
}
}
Aggregations