/** * Finds the {@link IUser} associated with the string. Checks in various formats. * * @param s String to be processed, either as the full name, the user ID, or the * {@literal @mention} for the user. * @return The specified user in the bot's {@link IGuild}. If no user was found, returns null. */ public static IUser getUser(String s) { final List<IGuild> current = Bot.getClient().getGuilds(); for (IGuild g : current) { for (IUser u : g.getUsers()) { if (u.getName().equals(s) || u.getID().equals(s) || ("<@" + u.getID() + ">").equals(s)) { return u; } } } return null; }
private String editUserPermission(Operation op, Permission permission, IUser user) { DiscordUser u = cacheService.findUserById(user.getID()).orElseGet(() -> new DiscordUser(user)); changePermission(op, permission, u); u = cacheService.saveUser(u); log.info("Saving new user permission settings: {}", u); permissionService.evict(); return String.format( "Modified user %s: %s permission %s", u.getName(), op.name().toLowerCase(), permission.getName()); }
private String editPermission( Operation op, IMessage message, Permission permission, List<String> args) { String first = args.size() >= 1 ? args.get(0) : ""; String second = args.size() >= 2 ? args.get(1) : ""; String third = args.size() >= 3 ? args.get(2) : ""; String fourth = args.size() >= 4 ? args.get(3) : ""; if (first.equalsIgnoreCase("this")) { // this <channel|guild> if (second.equalsIgnoreCase("channel")) { // this channel return editChannelPermission(op, permission, message.getChannel()); } else if (second.equalsIgnoreCase("server")) { // this guild if (message.getChannel().isPrivate()) { return "Not a valid call from private channels"; } IGuild guild = message.getChannel().getGuild(); return editGuildPermission(op, permission, guild); } else { return "Use `this channel` or `this server`"; } } else if (first.equalsIgnoreCase("user")) { // user <name or id> List<IUser> matching = discordService .getClient() .getGuilds() .stream() .flatMap(g -> g.getUsers().stream()) .filter(u -> u.getName().equalsIgnoreCase(second) || u.getID().equals(second)) .distinct() .collect(Collectors.toList()); if (matching.size() > 1) { StringBuilder builder = new StringBuilder("Multiple users matched, please narrow search or use ID\n"); for (IUser user : matching) { builder.append(user.getName()).append(" has id `").append(user.getID()).append("`\n"); } return builder.toString(); } else if (matching.isEmpty()) { return "User " + second + " not found in cache"; } else { return editUserPermission(op, permission, matching.get(0)); } } else if (first.equalsIgnoreCase("channel")) { // channel <name or id> List<IChannel> matching = discordService .getClient() .getGuilds() .stream() .flatMap(g -> g.getChannels().stream()) .filter(c -> c.getName().equalsIgnoreCase(second) || c.getID().equals(second)) .distinct() .collect(Collectors.toList()); if (matching.size() > 1) { StringBuilder builder = new StringBuilder("Multiple channels matched, please narrow search or use ID\n"); for (IChannel channel : matching) { builder .append(channel.getName()) .append(" has id `") .append(channel.getID()) .append("`\n"); } return builder.toString(); } else if (matching.isEmpty()) { return "Channel " + second + " not found in cache"; } else { return editChannelPermission(op, permission, matching.get(0)); } } else if (first.equalsIgnoreCase("server")) { // guild <name or id> List<IGuild> matching = discordService .getClient() .getGuilds() .stream() .filter(g -> g.getName().equalsIgnoreCase(second) || g.getID().equals(second)) .distinct() .collect(Collectors.toList()); if (matching.size() > 1) { StringBuilder builder = new StringBuilder("Multiple servers matched, please narrow search or use ID\n"); for (IGuild guild : matching) { builder.append(guild.getName()).append(" has id `").append(guild.getID()).append("`\n"); } return builder.toString(); } else if (matching.isEmpty()) { return "Server " + second + " not found in cache"; } else { return editGuildPermission(op, permission, matching.get(0)); } } else if (first.equalsIgnoreCase("role")) { // role <name or id> [in <guild name or id>] // when a guild name/id is not specified: // if triggered from a private channel, all case insensitive matches are picked up // if triggered from a public channel, only the channel's guild is searched boolean isPrivate = message.getChannel().isPrivate(); boolean specific = third.equalsIgnoreCase("in") && !fourth.isEmpty(); List<IRole> matching = discordService .getClient() .getGuilds() .stream() .filter( g -> { if (isPrivate) { return !specific || g.getName().equalsIgnoreCase(fourth) || g.getID().equals(fourth); } else { if (!specific) { return g.equals(message.getChannel().getGuild()); } else { return g.getName().equalsIgnoreCase(fourth) || g.getID().equals(fourth); } } }) .flatMap(g -> g.getRoles().stream()) .filter(r -> r.getName().equalsIgnoreCase(second) || r.getID().equals(second)) .distinct() .collect(Collectors.toList()); if (matching.size() > 1) { StringBuilder builder = new StringBuilder("Multiple role matched, please narrow search or use ID\n"); for (IRole role : matching) { builder.append(role.getName()).append(" has id `").append(role.getID()).append("`\n"); } return builder.toString(); } else if (matching.isEmpty()) { return "Role " + second + " not found in cache"; } else { return editRolePermission(op, permission, matching.get(0)); } } else { return "Invalid argument format! Check details with `.perm -?`"; } }