| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,18 @@ public class ModerationConfig extends GuildConfigItem { | |||
| 28 | 28 | private long adminRoleId = 0; | |
| 29 | 29 | private long expertRoleId = 0; | |
| 30 | 30 | ||
| 31 | + /** | ||
| 32 | + * The time window, in seconds, that the cross-channel spam automod looks back over when | ||
| 33 | + * counting a user's recent messages. If this is {@code 0}, the cross-channel spam automod | ||
| 34 | + * is disabled. | ||
| 35 | + */ | ||
| 36 | + private int crossChannelSpamWindowSeconds = 0; | ||
| 37 | + | ||
| 38 | + /** | ||
| 39 | + * The number of distinct channels a user must post in within | ||
| 40 | + * {@link #crossChannelSpamWindowSeconds} seconds before the cross-channel spam automod acts. | ||
| 41 | + */ | ||
| 42 | + private int crossChannelSpamMinChannels = 3; | ||
| 31 | 43 | /** | |
| 32 | 44 | * ID of the share-knowledge channel. | |
| 33 | 45 | */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,10 +39,10 @@ public void onMessageUpdate(@NotNull MessageUpdateEvent event) { | |||
| 39 | 39 | CachedMessage before; | |
| 40 | 40 | if (optional.isPresent()) { | |
| 41 | 41 | CachedMessage inCache= optional.get(); | |
| 42 | - before = new CachedMessage(inCache.getMessageId(), inCache.getAuthorId(), inCache.getMessageContent(), inCache.getAttachments()); | ||
| 42 | + before = new CachedMessage(inCache.getMessageId(), inCache.getAuthorId(), inCache.getChannelId(),inCache.getMessageContent(), inCache.getAttachments()); | ||
| 43 | 43 | inCache.init(event.getMessage()); | |
| 44 | 44 | } else { | |
| 45 | - before = new CachedMessage(event.getMessageIdLong(), event.getAuthor().getIdLong(), "[unknown content]", List.of()); | ||
| 45 | + before = new CachedMessage(event.getMessageIdLong(), event.getAuthor().getIdLong(), event.getChannel().getIdLong(),"[unknown content]", List.of()); | ||
| 46 | 46 | messageCache.cache(event.getMessage()); | |
| 47 | 47 | } | |
| 48 | 48 | messageCache.sendUpdatedMessageToLog(event.getMessage(), before); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,14 +31,15 @@ public class MessageCacheRepository { | |||
| 31 | 31 | * @throws SQLException If an error occurs. | |
| 32 | 32 | */ | |
| 33 | 33 | public void insertList(@NotNull List<CachedMessage> messages) throws DataAccessException { | |
| 34 | - jdbcTemplate.batchUpdate("MERGE INTO message_cache (message_id, author_id, message_content) VALUES (?, ?, ?)", | ||
| 34 | + jdbcTemplate.batchUpdate("MERGE INTO message_cache (message_id, author_id, channel_id, message_content) VALUES (?, ?, ?, ?)", | ||
| 35 | 35 | new BatchPreparedStatementSetter() { | |
| 36 | 36 | @Override | |
| 37 | 37 | public void setValues(PreparedStatement stmt, int i) throws SQLException { | |
| 38 | 38 | CachedMessage msg = messages.get(i); | |
| 39 | 39 | stmt.setLong(1, msg.getMessageId()); | |
| 40 | 40 | stmt.setLong(2, msg.getAuthorId()); | |
| 41 | - stmt.setString(3, msg.getMessageContent()); | ||
| 41 | + stmt.setLong(3,msg.getChannelId()); | ||
| 42 | + stmt.setString(4, msg.getMessageContent()); | ||
| 42 | 43 | stmt.executeUpdate(); | |
| 43 | 44 | } | |
| 44 | 45 | ||
@@ -88,7 +89,7 @@ public List<CachedMessage> getAll() throws DataAccessException { | |||
| 88 | 89 | messages.merge(msg.getMessageId(), msg, (oldValue, value) -> { | |
| 89 | 90 | ArrayList<String> attachments = new ArrayList<>(oldValue.getAttachments()); | |
| 90 | 91 | attachments.addAll(value.getAttachments()); | |
| 91 | - return new CachedMessage(oldValue.getMessageId(), oldValue.getAuthorId(), oldValue.getMessageContent(), attachments); | ||
| 92 | + return new CachedMessage(oldValue.getMessageId(), oldValue.getAuthorId(), oldValue.getChannelId(),oldValue.getMessageContent(), attachments); | ||
| 92 | 93 | }); | |
| 93 | 94 | } | |
| 94 | 95 | return new ArrayList<>(messages.values()); | |
@@ -119,6 +120,7 @@ private CachedMessage read(ResultSet rs) throws SQLException { | |||
| 119 | 120 | return new CachedMessage( | |
| 120 | 121 | rs.getLong("message_cache.message_id"), | |
| 121 | 122 | rs.getLong("author_id"), | |
| 123 | + rs.getLong("channel_id"), | ||
| 122 | 124 | rs.getString("message_content"), | |
| 123 | 125 | attachments); | |
| 124 | 126 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,25 +19,29 @@ | |||
| 19 | 19 | public class CachedMessage { | |
| 20 | 20 | private final long messageId; | |
| 21 | 21 | private final long authorId; | |
| 22 | + private final long channelId; | ||
| 22 | 23 | private String messageContent; | |
| 23 | 24 | private List<String> attachments=new ArrayList<>(); | |
| 24 | 25 | ||
| 25 | - private CachedMessage(long messageId, long authorId) { | ||
| 26 | + private CachedMessage(long messageId, long authorId,long channelId) { | ||
| 26 | 27 | this.messageId = messageId; | |
| 27 | 28 | this.authorId = authorId; | |
| 29 | + this.channelId = channelId; | ||
| 28 | 30 | } | |
| 29 | 31 | ||
| 30 | 32 | /** | |
| 31 | 33 | * Creates a {@link CachedMessage} with the given information. | |
| 32 | 34 | * @param messageId The Discord ID of the message | |
| 33 | 35 | * @param authorId The Discord ID of the message author | |
| 36 | + * @param channelId The Discord ID of the message channel | ||
| 34 | 37 | * @param messageContent the textual content of the message | |
| 35 | 38 | * @param attachments The attachment URLs | |
| 36 | 39 | */ | |
| 37 | - public CachedMessage(long messageId, long authorId, String messageContent, List<String> attachments) { | ||
| 40 | + public CachedMessage(long messageId, long authorId, long channelId, String messageContent, List<String> attachments) { | ||
| 38 | 41 | super(); | |
| 39 | 42 | this.messageId = messageId; | |
| 40 | 43 | this.authorId = authorId; | |
| 44 | + this.channelId = channelId; | ||
| 41 | 45 | this.messageContent = messageContent; | |
| 42 | 46 | this.attachments = List.copyOf(attachments); | |
| 43 | 47 | } | |
@@ -49,7 +53,7 @@ public CachedMessage(long messageId, long authorId, String messageContent, List< | |||
| 49 | 53 | * @return The built {@link CachedMessage}. | |
| 50 | 54 | */ | |
| 51 | 55 | public static CachedMessage of(Message message) { | |
| 52 | - CachedMessage cachedMessage = new CachedMessage(message.getIdLong(), message.getAuthor().getIdLong()); | ||
| 56 | + CachedMessage cachedMessage = new CachedMessage(message.getIdLong(), message.getAuthor().getIdLong(),message.getChannelIdLong()); | ||
| 53 | 57 | cachedMessage.init(message); | |
| 54 | 58 | return cachedMessage; | |
| 55 | 59 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ | |||
| 3 | 3 | import lombok.extern.slf4j.Slf4j; | |
| 4 | 4 | import net.discordjug.javabot.data.config.BotConfig; | |
| 5 | 5 | import net.discordjug.javabot.data.h2db.message_cache.MessageCache; | |
| 6 | + import net.discordjug.javabot.data.h2db.message_cache.model.CachedMessage; | ||
| 6 | 7 | import net.discordjug.javabot.systems.moderation.warn.model.WarnSeverity; | |
| 7 | 8 | import net.discordjug.javabot.systems.notification.NotificationService; | |
| 8 | 9 | import net.discordjug.javabot.util.ExceptionLogger; | |
@@ -24,9 +25,7 @@ | |||
| 24 | 25 | import java.net.URL; | |
| 25 | 26 | import java.time.Duration; | |
| 26 | 27 | import java.time.temporal.ChronoUnit; | |
| 27 | - import java.util.Collections; | ||
| 28 | - import java.util.List; | ||
| 29 | - import java.util.Scanner; | ||
| 28 | + import java.util.*; | ||
| 30 | 29 | import java.util.concurrent.TimeUnit; | |
| 31 | 30 | import java.util.regex.Matcher; | |
| 32 | 31 | import java.util.regex.Pattern; | |
@@ -50,18 +49,19 @@ public class AutoMod extends ListenerAdapter { | |||
| 50 | 49 | private final MessageCache messageCache; | |
| 51 | 50 | ||
| 52 | 51 | /** | |
| 53 | - * Constructor of the class, that creates a list of strings with potential spam/scam urls. | ||
| 52 | + * Constructor of the class, that creates a list of strings with potential spam/scam URLs. | ||
| 53 | + * | ||
| 54 | 54 | * @param notificationService The {@link QOTWPointsService} | |
| 55 | - * @param botConfig The main configuration of the bot | ||
| 56 | - * @param moderationService Service object for moderating members | ||
| 57 | - * @param messageCache service for retrieving cached messages | ||
| 55 | + * @param botConfig The main configuration of the bot | ||
| 56 | + * @param moderationService Service object for moderating members | ||
| 57 | + * @param messageCache service for retrieving cached messages | ||
| 58 | 58 | */ | |
| 59 | 59 | public AutoMod(NotificationService notificationService, BotConfig botConfig, ModerationService moderationService, MessageCache messageCache) { | |
| 60 | 60 | this.notificationService = notificationService; | |
| 61 | 61 | this.botConfig = botConfig; | |
| 62 | 62 | this.moderationService = moderationService; | |
| 63 | 63 | this.messageCache = messageCache; | |
| 64 | - try(Scanner scan = new Scanner(new URL("https://raw.githubusercontent.com/DevSpen/scam-links/master/src/links.txt").openStream()).useDelimiter("\\A")) { | ||
| 64 | + try (Scanner scan = new Scanner(new URL("https://raw.githubusercontent.com/DevSpen/scam-links/master/src/links.txt").openStream()).useDelimiter("\\A")) { | ||
| 65 | 65 | String response = scan.next(); | |
| 66 | 66 | spamUrls = List.of(response.split("\n")); | |
| 67 | 67 | } catch (IOException e) { | |
@@ -105,21 +105,46 @@ private boolean canBypassAutomod(Member member) { | |||
| 105 | 105 | private void checkNewMessageAutomod(@Nonnull Message message) { | |
| 106 | 106 | // spam | |
| 107 | 107 | long spamCount = messageCache.getMessagesAfter(message.getTimeCreated().minusSeconds(6)) | |
| 108 | - .stream() | ||
| 109 | - .filter(cached -> cached.getMessageId() != message.getIdLong()) // exclude new/current message | ||
| 110 | - .filter(cached -> cached.getAuthorId() == message.getAuthor().getIdLong()) | ||
| 111 | - .filter(cached -> | ||
| 112 | - // only java files -> not spam | ||
| 113 | - cached.getAttachments().isEmpty() || | ||
| 114 | - cached.getAttachments().stream() | ||
| 115 | - .anyMatch(attachment -> !attachment.contains(".java?"))) | ||
| 116 | - .count() + 1; // include new message | ||
| 117 | - | ||
| 108 | + .stream() | ||
| 109 | + .filter(cached -> cached.getMessageId() != message.getIdLong()) // exclude new/current message | ||
| 110 | + .filter(cached -> cached.getAuthorId() == message.getAuthor().getIdLong()) | ||
| 111 | + .filter(cached -> | ||
| 112 | + // only java files -> not spam | ||
| 113 | + cached.getAttachments().isEmpty() || | ||
| 114 | + cached.getAttachments().stream() | ||
| 115 | + .anyMatch(attachment -> !attachment.contains(".java?"))) | ||
| 116 | + .count() + 1; // include new message | ||
| 117 | + | ||
| 118 | 118 | if (spamCount >= 5) { | |
| 119 | - handleSpam(message, message.getMember()); | ||
| 119 | + handleSpam(message); | ||
| 120 | 120 | } | |
| 121 | - | ||
| 122 | 121 | checkContentAutomod(message); | |
| 122 | + checkCrossChannelSpam(message); | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + private void checkCrossChannelSpam(@Nonnull Message message) { | ||
| 126 | + int spamWindowSeconds = (botConfig.get(message.getGuild()).getModerationConfig()).getCrossChannelSpamWindowSeconds(); | ||
| 127 | + Set<Long> channelIds = new HashSet<>(); | ||
| 128 | + List<CachedMessage> spamMessages = new ArrayList<>(); | ||
| 129 | + | ||
| 130 | + if (spamWindowSeconds <= 0) { | ||
| 131 | + return; | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + for (CachedMessage cachedMessage : messageCache.getMessagesAfter(message.getTimeCreated().minusSeconds(spamWindowSeconds))) { | ||
| 135 | + if (cachedMessage.getMessageId() == message.getIdLong()) { | ||
| 136 | + continue; | ||
| 137 | + } | ||
| 138 | + if (cachedMessage.getAuthorId() != message.getAuthor().getIdLong()) { | ||
| 139 | + continue; | ||
| 140 | + } | ||
| 141 | + channelIds.add(cachedMessage.getChannelId()); | ||
| 142 | + spamMessages.add(cachedMessage); | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + if (channelIds.size() >= (botConfig.get(message.getGuild()).getModerationConfig()).getCrossChannelSpamMinChannels()) { | ||
| 146 | + handleSpam(spamMessages, message); | ||
| 147 | + } | ||
| 123 | 148 | } | |
| 124 | 149 | ||
| 125 | 150 | /** | |
@@ -130,7 +155,7 @@ private void checkNewMessageAutomod(@Nonnull Message message) { | |||
| 130 | 155 | private void checkContentAutomod(@Nonnull Message message) { | |
| 131 | 156 | //Check for Advertising Links | |
| 132 | 157 | if (hasAdvertisingLink(message)) { | |
| 133 | - doAutomodActions(message,"Advertising"); | ||
| 158 | + doAutomodActions(message, "Advertising"); | ||
| 134 | 159 | } | |
| 135 | 160 | ||
| 136 | 161 | //Check for suspicious Links | |
@@ -158,20 +183,29 @@ private void doAutomodActions(Message message, String reason) { | |||
| 158 | 183 | /** | |
| 159 | 184 | * Handles detected spam messages. | |
| 160 | 185 | * | |
| 161 | - * @param msg the (last) spam message | ||
| 162 | - * @param member the member to be potentially warned | ||
| 186 | + * @param msg the (last) spam message | ||
| 163 | 187 | */ | |
| 164 | - private void handleSpam(@Nonnull Message msg, Member member) { | ||
| 188 | + private void handleSpam(@Nonnull Message msg) { | ||
| 189 | + timeoutForSpam(msg); | ||
| 190 | + msg.delete().queue(); | ||
| 191 | + } | ||
| 192 | + | ||
| 193 | + private void handleSpam(@Nonnull List<CachedMessage> cachedMessages, Message message) { | ||
| 194 | + timeoutForSpam(message); | ||
| 195 | + cachedMessages.forEach(cachedMessage -> message.getGuild().getTextChannelById(cachedMessage.getChannelId()) | ||
| 196 | + .deleteMessageById(cachedMessage.getMessageId()).queue()); | ||
| 197 | + } | ||
| 198 | + | ||
| 199 | + private void timeoutForSpam(@Nonnull Message message) { | ||
| 165 | 200 | moderationService | |
| 166 | 201 | .timeout( | |
| 167 | - member.getUser(), | ||
| 202 | + message.getAuthor(), | ||
| 168 | 203 | "Automod: Spam", | |
| 169 | - msg.getGuild().getSelfMember(), | ||
| 204 | + message.getGuild().getSelfMember(), | ||
| 170 | 205 | Duration.of(6, ChronoUnit.HOURS), | |
| 171 | - msg.getChannel(), | ||
| 206 | + message.getChannel(), | ||
| 172 | 207 | false | |
| 173 | 208 | ); | |
| 174 | - msg.delete().queue(); | ||
| 175 | 209 | } | |
| 176 | 210 | ||
| 177 | 211 | /** | |
@@ -237,5 +271,5 @@ private boolean isSuggestionsChannel(@NotNull MessageChannelUnion channel) { | |||
| 237 | 271 | return channel.getType().isGuild() && | |
| 238 | 272 | channel.getIdLong() == botConfig.get(channel.asGuildMessageChannel().getGuild()).getModerationConfig().getSuggestionChannel().getIdLong(); | |
| 239 | 273 | } | |
| 240 | - | ||
| 274 | + | ||
| 241 | 275 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + ALTER TABLE message_cache ADD COLUMN channel_id BIGINT DEFAULT -1; | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments