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); } }
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); } }