Ejemplo n.º 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();
     }
   }
 }
Ejemplo n.º 2
0
  /**
   * CMD: Server owners can view and alter settings. Please see the documentation in the code for
   * more detail.
   *
   * @param message The message that this command got triggered off.
   */
  public static void run(IMessage message) {
    Server server = ServerManager.getServer(message.getGuild().getID());
    if ((server.getAdminRole() != null
            && message
                .getAuthor()
                .getRolesForGuild(message.getGuild())
                .contains(message.getGuild().getRolesByName(server.getAdminRole()).get(0)))
        || message.getAuthor() == message.getGuild().getOwner()
        || Constants.adminUser.contains(message.getAuthor().getID())) {
      if (message.getContent().split(" ").length == 1) { // No arguments: Show list of the settings.
        StringBuilder sb = new StringBuilder();

        sb.append(
            MessageFormat.format(
                "Settings for **{0}** | ID: `{1}`\n\n```",
                message.getGuild().getName(), message.getGuild().getID()));

        int longest = 0;

        for (SettingsInfo setting : SettingsInfo.values()) {
          int i = setting.toString().length();
          if (i > longest) {
            longest = i;
          }
        }
        if ("Setting".length() > longest) {
          longest = "Setting".length();
        }
        sb.append("Setting")
            .append(StringUtils.repeat(" ", longest - "Setting".length()))
            .append(" | Value\n");
        sb.append(StringUtils.repeat("-", longest))
            .append("-|-")
            .append(StringUtils.repeat("-", longest))
            .append("\n");
        for (SettingsInfo setting : SettingsInfo.values()) {
          sb.append(
              MessageFormat.format(
                  "{0} | {1}\n",
                  setting.toString()
                      + StringUtils.repeat(" ", longest - setting.toString().length()),
                  ((((setting == SettingsInfo.WELCOME_MESSAGE
                                  || setting == SettingsInfo.GOODBYE_MESSAGE)
                              && server.getSetting(setting) != null)
                          && server.getSetting(setting).length() > longest)
                      ? "<hidden - view info>"
                      : server.getSetting(setting))));
        }

        sb.append(
            "```\nType `!settings <name>` to view more info about it. Type `!settings <name> <value>` to change it's value!");

        MessagesUtils.sendPlain(sb.toString(), message.getChannel(), false);

      } else if (message.getContent().split(" ").length
          == 2) { // 1 argument: Show the setting that got specified in more detail.

        try {
          SettingsInfo setting =
              SettingsInfo.valueOf(message.getContent().split(" ")[1].toUpperCase());
          MessagesUtils.sendPlain(
              MessageFormat.format(
                  "```\n"
                      + "Setting:       {0}\n"
                      + "Description:   {1}\n"
                      + "Value:         {2}\n"
                      + "Default Value: {3}\n"
                      + "Value Type:    {4}\n"
                      + "```\n"
                      + "To alter the value type `!settings {5} <value>`.",
                  setting.toString(),
                  setting.getDescription(),
                  ServerManager.getServer(message.getGuild().getID()).getSetting(setting),
                  setting.getDefaultValue(),
                  setting.getType(),
                  setting.toString()),
              message.getChannel(),
              false);
        } catch (IllegalArgumentException e) {
          MessagesUtils.sendError(
              "Unknown setting: " + message.getContent().split(" ")[1].toUpperCase(),
              message.getChannel());
        }

      } else if (message.getContent().split(" ").length
          > 2) { // 2 or more arguments: Change the specified setting to the specified value.

        SettingsInfo setting = null;
        try {
          setting = SettingsInfo.valueOf(message.getContent().split(" ")[1].toUpperCase());
        } catch (IllegalArgumentException e) {
          MessagesUtils.sendError(
              "Unknown setting: " + message.getContent().split(" ")[1].toUpperCase(),
              message.getChannel());
          return;
        }

        if (setting == SettingsInfo.TWITCH_CHANNEL
            && !message.getContent().split(" ")[2].equalsIgnoreCase("null")) {
          SkuddUser su =
              ProfileManager.getDiscord(
                  message.getAuthor().getID(), message.getGuild().getID(), true);
          if (su.getTwitchUsername() == null
              && !Constants.adminUser.contains(message.getAuthor().getID())) {
            MessagesUtils.sendError(
                "You do not have a Twitch Account linked, type '!twitch' to get started with linking!",
                message.getChannel());
            return;
          }
          if (!su.getTwitchUsername().equalsIgnoreCase(message.getContent().split(" ")[2])
              && !Constants.adminUser.contains(message.getAuthor().getID())) {
            MessagesUtils.sendError(
                "You can only set this value to your linked Twitch Account, which is "
                    + su.getTwitchUsername()
                    + "! (If this is incorrect, please contact Tim.)",
                message.getChannel());
            return;
          }
        }
        String value = message.getContent().split(" ")[2];
        if (setting == SettingsInfo.WELCOME_MESSAGE
            || setting == SettingsInfo.GOODBYE_MESSAGE
            || setting == SettingsInfo.ADMIN_ROLE
            || setting == SettingsInfo.ROLE_ON_JOIN) {
          StringBuilder sb = new StringBuilder();
          String[] args = message.getContent().split(" ");
          for (int i = 2; i < args.length; i++) {
            sb.append(args[i]).append(" ");
          }
          value = sb.toString().trim();
        }
        String result = null;
        if (setting == SettingsInfo.TWITCH_CHANNEL) {
          server.setTwitch(value);
        } else {
          result = ServerManager.getServer(message.getGuild().getID()).setSetting(setting, value);
        }

        if (result == null) {
          MessagesUtils.sendSuccess(
              MessageFormat.format(
                  "Setting `{0}` has been updated to `{1}`!", setting.toString(), value),
              message.getChannel());
        } else {
          MessagesUtils.sendError(result, message.getChannel());
        }
      }
    } else {
      Logger.info(
          message.getAuthor().getName()
              + " attempted to do something they don't have permission for.");
    }
  }
Ejemplo n.º 3
0
 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 -?`";
   }
 }