use of net.dv8tion.jda.core.entities.MessageHistory in project c0debaseBot by Biospheere.
the class ClearCommand method execute.
@Override
public void execute(String[] args, Message msg) {
if (msg.getMember().hasPermission(Permission.ADMINISTRATOR) || msg.getMember().hasPermission(Permission.BAN_MEMBERS)) {
if (args.length == 0) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setFooter("@" + msg.getMember().getUser().getName() + "#" + msg.getMember().getUser().getDiscriminator(), msg.getMember().getUser().getEffectiveAvatarUrl());
embedBuilder.setColor(msg.getGuild().getSelfMember().getColor());
embedBuilder.appendDescription("!clear <Amount>");
msg.getTextChannel().sendMessage(embedBuilder.build()).queue();
} else {
int i = 0;
try {
i = Integer.valueOf(args[0]);
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
List<Message> msgs;
MessageHistory history = new MessageHistory(msg.getTextChannel());
msgs = history.retrievePast(i + 1).complete();
msg.getTextChannel().deleteMessages(msgs).queue();
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setFooter("@" + msg.getMember().getUser().getName() + "#" + msg.getMember().getUser().getDiscriminator(), msg.getMember().getUser().getEffectiveAvatarUrl());
embedBuilder.setColor(msg.getGuild().getSelfMember().getColor());
embedBuilder.appendDescription("Es wurden **" + (i) + "** Nachrichten gelöscht");
msg.getTextChannel().sendMessage(embedBuilder.build()).queue();
}
}
}
use of net.dv8tion.jda.core.entities.MessageHistory in project Frost by woojiahao.
the class SpamDetector method updateMap.
private void updateMap(DiscordSet set) {
String user = set.getUser().getId();
String guild = set.getGuild().getId();
int guildPos = 0;
if ((guildPos = hasGuildEntry(guild)) == -1) {
messageCount.add(new AbstractMap.SimpleEntry<>(guild, new HashMap<>()));
messageTimer.add(new AbstractMap.SimpleEntry<>(guild, new HashMap<>()));
guildPos = messageCount.size() - 1;
}
if (!hasUserEntry(user, guildPos)) {
messageCount.get(guildPos).getValue().put(user, 1L);
messageTimer.get(guildPos).getValue().put(user, System.currentTimeMillis());
int finalGuildPos = guildPos;
new Timer().schedule(new TimerTask() {
@Override
public void run() {
messageCount.get(finalGuildPos).getValue().remove(user);
messageTimer.get(finalGuildPos).getValue().remove(user);
}
}, RaidLimits.MESSAGE.getDuration());
return;
}
long currentMessageCount = messageCount.get(guildPos).getValue().get(user);
messageCount.get(guildPos).getValue().replace(user, currentMessageCount + 1);
if (messageCount.get(guildPos).getValue().get(user) > RaidLimits.MESSAGE.getMessageCount()) {
long firstMessageTime = messageTimer.get(guildPos).getValue().get(user);
long currentTime = System.currentTimeMillis();
if (currentTime - firstMessageTime <= RaidLimits.MESSAGE.getDuration()) {
FrostRoles.assignRole(set, RolesID.MUTED);
String loggingID = FrostGuildList.getFrostGuildByID(set.getGuild().getId()).getLoggingChannel();
set.getGuild().getTextChannelById(loggingID).sendMessage(set.getUser().getAsMention() + " was muted due " + "to spam").queue();
new FrostDirectMessage().sendPrivateMessage(set.getUser(), new EmbedBuilder().addField("Auto spam detection", "You have been muted for spam", false).build());
List<Message> toDelete = new MessageHistory(set.getFrom()).retrievePast(50).complete().stream().filter(m -> m.getAuthor().getId().equals(set.getUser().getId())).collect(Collectors.toList());
if (toDelete != null || toDelete.size() > 0) {
set.getGuild().getTextChannelById(set.getFrom().getId()).deleteMessages(toDelete).queue();
}
RaidersList.add(user, guild);
}
}
}
use of net.dv8tion.jda.core.entities.MessageHistory in project FredBoat by Frederikam.
the class ClearCommand method onInvoke.
// TODO: Redo this
// TODO: i18n this class
@Override
public void onInvoke(@Nonnull CommandContext context) {
JDA jda = context.guild.getJDA();
TextChannel channel = context.channel;
Member invoker = context.invoker;
if (!invoker.hasPermission(channel, Permission.MESSAGE_MANAGE) && !PermsUtil.checkPerms(PermissionLevel.BOT_ADMIN, invoker)) {
context.replyWithName("You must have Manage Messages to do that!");
return;
}
if (!context.guild.getSelfMember().hasPermission(channel, Permission.MESSAGE_HISTORY)) {
context.reply(context.i18n("permissionMissingBot") + " **" + Permission.MESSAGE_HISTORY.getName() + "**");
return;
}
MessageHistory history = new MessageHistory(channel);
history.retrievePast(50).queue(msgs -> {
Metrics.successfulRestActions.labels("retrieveMessageHistory").inc();
ArrayList<Message> toDelete = new ArrayList<>();
for (Message msg : msgs) {
if (msg.getAuthor().equals(jda.getSelfUser()) && youngerThanTwoWeeks(msg)) {
toDelete.add(msg);
}
}
if (toDelete.isEmpty()) {
context.reply("No messages found.");
} else if (toDelete.size() == 1) {
context.reply("Found one message, deleting.");
CentralMessaging.deleteMessage(toDelete.get(0));
} else {
if (!context.hasPermissions(Permission.MESSAGE_MANAGE)) {
context.reply("I must have the `Manage Messages` permission to delete my own messages in bulk.");
return;
}
context.reply("Deleting **" + toDelete.size() + "** messages.");
CentralMessaging.deleteMessages(channel, toDelete);
}
}, CentralMessaging.getJdaRestActionFailureHandler(String.format("Failed to retrieve message history in channel %s in guild %s", channel.getId(), context.guild.getId())));
}
Aggregations