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); }
private void vanish(MessageContext context, String args) { Message message = context.getMessage(); if (args.isEmpty()) { return; } try { final int cap = 100; final int capPages = 10; String[] vals = args.split(" "); String userId; int numMsgs; if (vals.length == 2) { User user = findUser(context, vals[0]); userId = user.getId(); if (user == NO_USER) { userId = vals[0]; } numMsgs = Math.max(1, Math.min(cap, Integer.parseInt(vals[1]))); } else if (vals.length == 1) { userId = ""; numMsgs = Math.max(1, Math.min(cap, Integer.parseInt(vals[0]))); } else { userId = ""; numMsgs = 10; } int limit = numMsgs; String before = message.getId(); // Limit search to 10 pages (500 msgs) // Instead of deleting them when we find them, we build a list instead List<String> messagesToDelete = new ArrayList<>(limit); for (int k = 0; k < capPages && limit > 0; k++) { Map<String, String> headers = new HashMap<>(); headers.put(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType()); headers.put(HttpHeaders.AUTHORIZATION, apiClient.getToken()); Map<String, Object> queryParams = new HashMap<>(); queryParams.put("limit", 50); queryParams.put("before", before); HttpResponse<JsonNode> response = Unirest.get(ApiConst.CHANNELS_ENDPOINT + message.getChannelId() + "/messages") .headers(headers) .queryString(queryParams) .asJson(); JSONArray ret = response.getBody().getArray(); if (userId.isEmpty()) { for (int i = 0; i < ret.length() && limit > 0; i++) { JSONObject msg = ret.getJSONObject(i); String mid = msg.getString("id"); before = mid; messagesToDelete.add(mid); limit--; } } else { for (int i = 0; i < ret.length() && limit > 0; i++) { JSONObject msg = ret.getJSONObject(i); String mid = msg.getString("id"); JSONObject msgUsr = msg.getJSONObject("author"); String uid = msgUsr.getString("id"); before = mid; if (userId.equals(uid)) { messagesToDelete.add(mid); limit--; } } } } LOGGER.info("Deleting {} messages", messagesToDelete.size()); // Using bulk delete endpoint apiClient.bulkDeleteMessages( context.getChannel().getId(), messagesToDelete.toArray(new String[messagesToDelete.size()])); } catch (Exception e) { LOGGER.warn("Unable to get messages", e); } }