Пример #1
0
 public GenericEventContext(Event event) {
   timestamp = System.currentTimeMillis();
   clazz = event.getClass();
   Method[] methods = clazz.getMethods();
   for (Method m : methods) {
     try {
       if (m.getName().equalsIgnoreCase("getUser")) user = (IUser) m.invoke(event);
       else if (m.getName().equalsIgnoreCase("getChannel")) {
         channel = new RateLimitedChannel((IChannel) m.invoke(event));
         guild = channel.getGuild();
       } else if (m.getName().equalsIgnoreCase("getMessage")) {
         msg = (IMessage) m.invoke(event);
         user = msg.getAuthor();
         channel = new RateLimitedChannel(msg.getChannel());
         guild = channel.getGuild();
       }
     } catch (Exception ex) {
       ex.printStackTrace();
     }
   }
 }
 private String editChannelPermission(Operation op, Permission permission, IChannel channel) {
   if (channel.isPrivate()) {
     throw new IllegalArgumentException("No private channels allowed!");
   }
   DiscordGuild g =
       cacheService
           .findGuildById(channel.getGuild().getID())
           .orElseGet(() -> new DiscordGuild(channel.getGuild()));
   g = cacheService.saveGuild(g);
   DiscordChannel ch =
       cacheService.findChannelById(channel.getID()).orElseGet(() -> new DiscordChannel(channel));
   ch.setGuild(g);
   g.getChannels().add(ch);
   changePermission(op, permission, ch);
   ch = cacheService.saveChannel(ch);
   cacheService.saveGuild(g);
   log.info("Saving new permission data: {}", g);
   permissionService.evict();
   return String.format(
       "Modified channel %s: %s permission %s",
       ch.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 -?`";
   }
 }