@Override public void action(String[] args, GuildMessageReceivedEvent event) throws ParseException, IOException { event.getMessage().delete().queue(); if(core.permissionHandler.check(2, event)) return; if (args.length < 1) util.embedSender.error().setDescription(help()).setTitle("ERROR!").build(); int numb = getInt(args[0]); if (numb > 1 && numb <= 100) { try{ MessageHistory history = new MessageHistory(event.getChannel()); List<Message> msgs; msgs = history.retrievePast(numb).complete(); event.getChannel().deleteMessages(msgs).queue(); Message msg = event.getChannel().sendMessage(new EmbedBuilder() .setColor(Color.GREEN) .setDescription(":bomb: Deleted " + args[0] + " Messages!") .build() ).complete(); new Timer().schedule(new TimerTask() { @Override public void run() { msg.delete().queue(); } }, 3000); }catch (Exception e){ e.printStackTrace(); } }else { util.embedSender.error().setDescription(help() + "Max. 100 Message and Min. 1 Message").setTitle("ERROR!").build(); } }
private void loadMessages(MessageHistory history, int toDelete, List<Message> messages, List<Long> userIds, int loops, Consumer<List<Message>> consumer) { long maxMessageAge = (System.currentTimeMillis() - TimeUnit.DAYS.toMillis(14) - MiscUtil.DISCORD_EPOCH) << MiscUtil.TIMESTAMP_OFFSET; history.retrievePast(100).queue(historyMessages -> { if (historyMessages.isEmpty()) { consumer.accept(messages); return; } for (Message historyMessage : historyMessages) { if (historyMessage.isPinned() || historyMessage.getIdLong() < maxMessageAge) { continue; } if (userIds != null && !userIds.contains(historyMessage.getAuthor().getIdLong())) { continue; } if (messages.size() >= toDelete || loops > MAX_HISTORY_LOOPS) { consumer.accept(messages); return; } messages.add(historyMessage); } loadMessages(history, toDelete, messages, userIds, loops + 1, consumer); }); }