Пример #1
0
  private void ban(MessageContext context, String args) {
    if (args.isEmpty()) {
      return;
    }
    String[] split = args.split(" ");
    List<String> banned = new ArrayList<>();
    List<String> failed = new ArrayList<>();
    Channel channel = context.getChannel();
    for (String userStr : split) {
      User user = findUser(context, userStr);
      String userId = user.getId();
      if (user == NO_USER) {
        userId = userStr;
      }

      if (banChecked(channel, context.getAuthor(), user, context.getServer())) {
        banned.add(userId + " " + user.getUsername());
      } else {
        failed.add(userId + " " + user.getUsername());
      }
    }

    if (channel.getId() != null) {
      StringJoiner joiner = new StringJoiner("\n");
      for (String s : banned) {
        String[] pair = s.split(" ", 2);
        joiner.add(loc.localize("commands.mod.ban.response", pair[1], pair[0]));
      }
      apiClient.sendMessage(joiner.toString(), channel);
    }
  }
Пример #2
0
 private void find(MessageContext context, String args) {
   DiscordApiClient apiClient = context.getApiClient();
   Channel channel = context.getChannel();
   Pattern pattern;
   try {
     pattern = Pattern.compile(args);
   } catch (PatternSyntaxException pse) {
     apiClient.sendMessage(loc.localize("commands.admin.find.response.invalid"), channel);
     return;
   }
   Predicate<String> matcher = pattern.asPredicate();
   List<User> results =
       context
           .getServer()
           .getMembers()
           .stream()
           .map(Member::getUser)
           .filter(u -> matcher.test(u.getUsername()))
           .collect(Collectors.toList());
   int size = results.size();
   if (size > 20) {
     apiClient.sendMessage(loc.localize("commands.admin.find.response.oversize", size), channel);
     return;
   }
   StringJoiner resultJoiner = new StringJoiner(", ");
   results.stream().map(this::userToResult).forEach(resultJoiner::add);
   apiClient.sendMessage(
       loc.localize("commands.admin.find.response.format", size, resultJoiner.toString()),
       channel);
 }
Пример #3
0
 private String formatDuration(Duration duration) {
   StringJoiner joiner = new StringJoiner(", ");
   long days = duration.toDays();
   if (days > 0) {
     long years = days / 365;
     if (years > 0) {
       long centuries = years / 100;
       if (centuries > 0) {
         long millenia = centuries / 10;
         joiner.add(loc.localize("time.millenia", millenia));
         centuries = centuries % 10;
         if (centuries > 0) {
           joiner.add(loc.localize("time.centuries", centuries));
         }
       }
       years = years % 100;
       if (years > 0) {
         joiner.add(loc.localize("time.years", years));
       }
     }
     days = days % 365;
     joiner.add(loc.localize("time.days", days));
   }
   long hours = duration.toHours() % 24L;
   if (hours > 0) {
     joiner.add(loc.localize("time.hours", hours));
   }
   long minutes = duration.toMinutes() % 60L;
   if (minutes > 0) {
     joiner.add(loc.localize("time.minutes", minutes));
   }
   long seconds = duration.getSeconds() % 60L;
   if (seconds > 0) {
     joiner.add(loc.localize("time.seconds", seconds));
   }
   return joiner.toString();
 }