diff --git a/lists/referral_domains.txt b/lists/referral_domains.txt index 2354f03..40636ec 100644 --- a/lists/referral_domains.txt +++ b/lists/referral_domains.txt @@ -40,6 +40,7 @@ yoütu.be yoütübe.co yoütübe.com +youtubeshort.watch // Ad sites adblade.com diff --git a/src/main/java/com/jagrosh/vortex/automod/AutoMod.java b/src/main/java/com/jagrosh/vortex/automod/AutoMod.java index b888090..2ea5533 100644 --- a/src/main/java/com/jagrosh/vortex/automod/AutoMod.java +++ b/src/main/java/com/jagrosh/vortex/automod/AutoMod.java @@ -23,6 +23,7 @@ import com.jagrosh.vortex.automod.URLResolver.*; import com.jagrosh.vortex.utils.FixedCache; import com.jagrosh.vortex.utils.OtherUtil; +import com.jagrosh.vortex.utils.Usage; import com.typesafe.config.Config; import java.time.OffsetDateTime; import java.time.temporal.ChronoUnit; @@ -35,6 +36,7 @@ import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.entities.*; import net.dv8tion.jda.core.entities.Guild.VerificationLevel; @@ -70,7 +72,7 @@ private final CopypastaResolver copypastaResolver = new CopypastaResolver(); private final FixedCache spams = new FixedCache<>(3000); private final HashMap latestGuildJoin = new HashMap<>(); - private final HashMap usage = new HashMap<>(); + private final Usage usage = new Usage(); public AutoMod(Vortex vortex, Config config) { @@ -95,7 +97,7 @@ this.refLinkList = OtherUtil.readLines("referral_domains"); } - public HashMap getUsage() + public Usage getUsage() { return usage; } @@ -251,7 +253,7 @@ try { - usage.put(member.getGuild().getIdLong(), usage.getOrDefault(member.getGuild().getIdLong(), 0) + 1); + usage.increment(member.getGuild().getIdLong()); OtherUtil.dehoist(member, settings.dehoistChar); } catch(Exception ignore) {} @@ -278,7 +280,7 @@ List inviteWhitelist = !preventInvites ? Collections.emptyList() : vortex.getDatabase().inviteWhitelist.readWhitelist(message.getGuild()); List filters = vortex.getDatabase().filters.getFilters(message.getGuild()); - usage.put(message.getGuild().getIdLong(), usage.getOrDefault(message.getGuild().getIdLong(), 0) + 1); + usage.increment(message.getGuild().getIdLong()); boolean shouldDelete = false; String channelWarning = null; @@ -520,16 +522,29 @@ private void purgeMessages(Guild guild, Predicate predicate) { - vortex.getMessageCache().getMessages(guild, predicate).forEach(m -> + vortex.getMessageCache().getMessages(guild, predicate).stream() + .collect(Collectors.groupingBy(CachedMessage::getTextChannelId)).entrySet().forEach(entry -> { try { + TextChannel mtc = guild.getTextChannelById(entry.getKey()); + if(mtc != null) + mtc.purgeMessagesById(entry.getValue().stream().map(CachedMessage::getId).collect(Collectors.toList())); + } + catch(PermissionException ignore) {} + catch(Exception ex) { LOG.error("Error in purging messages: ", ex); } + });//*/ + /*vortex.getMessageCache().getMessages(guild, predicate).forEach(m -> + { + + try + { TextChannel mtc = m.getTextChannel(guild); if(mtc!=null) mtc.deleteMessageById(m.getIdLong()).queue(s->{}, f->{}); } catch(PermissionException ignore) {} - }); + });//*/ } private boolean isReferralUrl(String url) diff --git a/src/main/java/com/jagrosh/vortex/commands/CommandExceptionListener.java b/src/main/java/com/jagrosh/vortex/commands/CommandExceptionListener.java index 2a17ad8..9b7051a 100644 --- a/src/main/java/com/jagrosh/vortex/commands/CommandExceptionListener.java +++ b/src/main/java/com/jagrosh/vortex/commands/CommandExceptionListener.java @@ -18,7 +18,7 @@ import com.jagrosh.jdautilities.command.Command; import com.jagrosh.jdautilities.command.CommandEvent; import com.jagrosh.jdautilities.command.CommandListener; -import java.util.HashMap; +import com.jagrosh.vortex.utils.Usage; import net.dv8tion.jda.core.entities.ChannelType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,7 +30,7 @@ public class CommandExceptionListener implements CommandListener { private final Logger log = LoggerFactory.getLogger("Command"); - private final HashMap usage = new HashMap<>(); + private final Usage usage = new Usage(); @Override public void onCommandException(CommandEvent event, Command command, Throwable throwable) @@ -63,10 +63,10 @@ public void onCommand(CommandEvent event, Command command) { if(event.isFromType(ChannelType.TEXT)) - usage.put(event.getGuild().getIdLong(), usage.getOrDefault(event.getGuild().getIdLong(), 0) + 1); + usage.increment(event.getGuild().getIdLong()); } - public HashMap getUsage() + public Usage getUsage() { return usage; } diff --git a/src/main/java/com/jagrosh/vortex/logging/BasicLogger.java b/src/main/java/com/jagrosh/vortex/logging/BasicLogger.java index a4b48c6..6b28b47 100644 --- a/src/main/java/com/jagrosh/vortex/logging/BasicLogger.java +++ b/src/main/java/com/jagrosh/vortex/logging/BasicLogger.java @@ -19,6 +19,7 @@ import com.jagrosh.vortex.logging.MessageCache.CachedMessage; import com.jagrosh.vortex.utils.FormatUtil; import com.jagrosh.vortex.utils.LogUtil; +import com.jagrosh.vortex.utils.Usage; import com.typesafe.config.Config; import java.awt.Color; import java.time.OffsetDateTime; @@ -62,6 +63,7 @@ private final Vortex vortex; private final AvatarSaver avatarSaver; + private final Usage usage = new Usage(); public BasicLogger(Vortex vortex, Config config) { @@ -69,10 +71,16 @@ this.avatarSaver = new AvatarSaver(config); } + public Usage getUsage() + { + return usage; + } + private void log(OffsetDateTime now, TextChannel tc, String emote, String message, MessageEmbed embed) { try { + usage.increment(tc.getGuild().getIdLong()); tc.sendMessage(new MessageBuilder() .append(FormatUtil.filterEveryone(LogUtil.basiclogFormat(now, vortex.getDatabase().settings.getSettings(tc.getGuild()).getTimezone(), emote, message))) .setEmbed(embed) @@ -85,6 +93,7 @@ { try { + usage.increment(tc.getGuild().getIdLong()); tc.sendFile(file, filename, new MessageBuilder() .append(FormatUtil.filterEveryone(LogUtil.basiclogFormat(now, vortex.getDatabase().settings.getSettings(tc.getGuild()).getTimezone(), emote, message))) .build()).queue(); diff --git a/src/main/java/com/jagrosh/vortex/logging/MessageCache.java b/src/main/java/com/jagrosh/vortex/logging/MessageCache.java index 7835068..ab87e79 100644 --- a/src/main/java/com/jagrosh/vortex/logging/MessageCache.java +++ b/src/main/java/com/jagrosh/vortex/logging/MessageCache.java @@ -113,6 +113,11 @@ return g.getTextChannelById(channel); } + public long getTextChannelId() + { + return channel; + } + public TextChannel getTextChannel(Guild guild) { return guild.getTextChannelById(channel); diff --git a/src/main/java/com/jagrosh/vortex/utils/Usage.java b/src/main/java/com/jagrosh/vortex/utils/Usage.java new file mode 100644 index 0000000..b0803a6 --- /dev/null +++ b/src/main/java/com/jagrosh/vortex/utils/Usage.java @@ -0,0 +1,46 @@ +/* + * Copyright 2020 John Grosh (john.a.grosh@gmail.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jagrosh.vortex.utils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +/** + * + * @author John Grosh (john.a.grosh@gmail.com) + */ +public class Usage +{ + private final HashMap map = new HashMap<>(); + + public void increment(long key) + { + map.put(key, map.getOrDefault(key, 0) + 1); + } + + public Map getMap() + { + return map; + } + + public List> higher(int val) + { + return map.entrySet().stream().filter(e -> e.getValue() >= val).collect(Collectors.toList()); + } +}