Search in sources :

Example 1 with MessageHistory

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();
        }
    }
}
Also used : MessageHistory(net.dv8tion.jda.core.entities.MessageHistory) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Message(net.dv8tion.jda.core.entities.Message)

Example 2 with MessageHistory

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);
        }
    }
}
Also used : java.util(java.util) RaidersList(com.chill.projects.bot.raiders.RaidersList) RolesID(com.chill.projects.bot.roles.RolesID) MessageHistory(net.dv8tion.jda.core.entities.MessageHistory) FrostRoles(com.chill.projects.bot.roles.FrostRoles) ListenerAdapter(net.dv8tion.jda.core.hooks.ListenerAdapter) Collectors(java.util.stream.Collectors) Message(net.dv8tion.jda.core.entities.Message) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) DiscordSet(com.chill.projects.bot.container.DiscordSet) FrostGuildList(com.chill.projects.bot.guilds.FrostGuildList) RaidLimits(com.chill.projects.bot.raiders.RaidLimits) FrostDirectMessage(com.chill.projects.bot.messaging.FrostDirectMessage) Message(net.dv8tion.jda.core.entities.Message) FrostDirectMessage(com.chill.projects.bot.messaging.FrostDirectMessage) FrostDirectMessage(com.chill.projects.bot.messaging.FrostDirectMessage) MessageHistory(net.dv8tion.jda.core.entities.MessageHistory) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder)

Example 3 with MessageHistory

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())));
}
Also used : MessageHistory(net.dv8tion.jda.core.entities.MessageHistory) TextChannel(net.dv8tion.jda.core.entities.TextChannel) Message(net.dv8tion.jda.core.entities.Message) JDA(net.dv8tion.jda.core.JDA) ArrayList(java.util.ArrayList) Member(net.dv8tion.jda.core.entities.Member)

Aggregations

Message (net.dv8tion.jda.core.entities.Message)3 MessageHistory (net.dv8tion.jda.core.entities.MessageHistory)3 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)2 DiscordSet (com.chill.projects.bot.container.DiscordSet)1 FrostGuildList (com.chill.projects.bot.guilds.FrostGuildList)1 FrostDirectMessage (com.chill.projects.bot.messaging.FrostDirectMessage)1 RaidLimits (com.chill.projects.bot.raiders.RaidLimits)1 RaidersList (com.chill.projects.bot.raiders.RaidersList)1 FrostRoles (com.chill.projects.bot.roles.FrostRoles)1 RolesID (com.chill.projects.bot.roles.RolesID)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Collectors (java.util.stream.Collectors)1 JDA (net.dv8tion.jda.core.JDA)1 Member (net.dv8tion.jda.core.entities.Member)1 TextChannel (net.dv8tion.jda.core.entities.TextChannel)1 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)1 ListenerAdapter (net.dv8tion.jda.core.hooks.ListenerAdapter)1