-
Notifications
You must be signed in to change notification settings - Fork 10
Add a scam link detector #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
23fc87c
Revert "Commented out some broken things until later."
Matyrobbrt 033f118
Revert "Moved the dependencies and versions to a new system"
Matyrobbrt 55a4f78
Add scam link detector
Matyrobbrt 8ff23ef
Revert "Revert "Moved the dependencies and versions to a new system""
Matyrobbrt f80ac19
Revert "Revert "Commented out some broken things until later.""
Matyrobbrt 7f98fa1
Move some stuff into a separate method, and add the message's content…
Matyrobbrt 20eeaad
Change the logging channel
Matyrobbrt 2b2bc9b
Things
Matyrobbrt a3e8bd6
Add author check
Matyrobbrt aee8bda
Add message deletion reason
Matyrobbrt 6345e31
Add a filter in order to prevent https://cdn.discordapp.com/ (emoji l…
Matyrobbrt f6e5aa5
Remove some duplicate methods
Matyrobbrt a253343
Link the scam link refresher to a task scheduler
Matyrobbrt 2c1cce7
Update CmdRefreshScamLinks.java
Matyrobbrt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
src/main/java/com/mcmoddev/mmdbot/modules/logging/misc/ScamDetector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /* | ||
| * MMDBot - https://github.com/MinecraftModDevelopment/MMDBot | ||
| * Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment> | ||
| * | ||
| * This library is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 2.1 of the License, or (at your option) any later version. | ||
| * | ||
| * This library is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public | ||
| * License along with this library; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | ||
| * USA | ||
| * https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html | ||
| */ | ||
| package com.mcmoddev.mmdbot.modules.logging.misc; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
| import com.google.gson.JsonArray; | ||
| import com.mcmoddev.mmdbot.MMDBot; | ||
| import net.dv8tion.jda.api.EmbedBuilder; | ||
| import net.dv8tion.jda.api.entities.Guild; | ||
| import net.dv8tion.jda.api.entities.Message; | ||
| import net.dv8tion.jda.api.entities.TextChannel; | ||
| import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; | ||
| import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent; | ||
| import net.dv8tion.jda.api.hooks.ListenerAdapter; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.awt.Color; | ||
| import java.io.IOException; | ||
| import java.net.URL; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class ScamDetector extends ListenerAdapter { | ||
|
|
||
| public static final Gson GSON = new GsonBuilder().disableHtmlEscaping().create(); | ||
| public static final String SCAM_LINKS_DATA_URL = "https://phish.sinking.yachts/v2/all"; | ||
|
|
||
| public static final List<String> SCAM_LINKS = Collections.synchronizedList(new ArrayList<>()); | ||
|
|
||
| static { | ||
| new Thread(ScamDetector::setupScamLinks, "Scam link collector").start(); | ||
Matyrobbrt marked this conversation as resolved.
Show resolved
Hide resolved
Matyrobbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public void onGuildMessageReceived(@NotNull final GuildMessageReceivedEvent event) { | ||
Matyrobbrt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final var msg = event.getMessage(); | ||
| if (containsScam(msg)) { | ||
| final var member = msg.getMember(); | ||
| final var msgChannel = msg.getTextChannel(); | ||
| final var guild = msg.getGuild(); | ||
| final var mutedRoleID = MMDBot.getConfig().getRole("muted"); | ||
| msg.delete().queue($ -> { | ||
| final var embed = new EmbedBuilder() | ||
| .setTitle("Scam link detected!") | ||
| .setDescription(String.format("User %s sent a scam link in %s! Their message was deleted, and they were muted!", member.getAsMention(), | ||
| msgChannel.getAsMention())) | ||
| .setColor(Color.RED) | ||
| .setTimestamp(Instant.now()) | ||
| .setFooter("User ID: " + member.getIdLong()) | ||
| .setThumbnail(member.getEffectiveAvatarUrl()); | ||
| getLoggingChannel(guild).sendMessageEmbeds(embed.build()).queue(); | ||
Matyrobbrt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| msgChannel.sendMessage(String.format("%s did you *really* think that would work?", member.getAsMention())).queue(); | ||
Matyrobbrt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| guild.addRoleToMember(member, guild.getRoleById(mutedRoleID)).queue(); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onGuildMessageUpdate(@NotNull final GuildMessageUpdateEvent event) { | ||
| final var msg = event.getMessage(); | ||
| if (containsScam(msg)) { | ||
| final var member = msg.getMember(); | ||
| final var msgChannel = msg.getTextChannel(); | ||
| final var guild = msg.getGuild(); | ||
| final var mutedRoleID = MMDBot.getConfig().getRole("muted"); | ||
Matyrobbrt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| msg.delete().queue($ -> { | ||
Matyrobbrt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final var embed = new EmbedBuilder() | ||
| .setTitle("Scam link detected!") | ||
| .setDescription(String.format("User %s sent a scam link in %s, by editing an old message! Their message was deleted, and they were muted!", member.getAsMention(), | ||
| msgChannel.getAsMention())) | ||
| .setColor(Color.RED) | ||
| .setTimestamp(Instant.now()) | ||
| .setFooter("User ID: " + member.getIdLong()) | ||
| .setThumbnail(member.getEffectiveAvatarUrl()); | ||
| getLoggingChannel(guild).sendMessageEmbeds(embed.build()).queue(); | ||
| msgChannel.sendMessage(String.format("%s did you *really* think that would work?", member.getAsMention())).queue(); | ||
| guild.addRoleToMember(member, guild.getRoleById(mutedRoleID)).queue(); | ||
| }); | ||
| } | ||
Matyrobbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private static TextChannel getLoggingChannel(final Guild guild) { | ||
| return guild.getTextChannelById(MMDBot.getConfig().getChannel("events.important")); | ||
| } | ||
|
|
||
| public static boolean containsScam(final Message message) { | ||
| final String msgContent = message.getContentRaw().toLowerCase(); | ||
Matyrobbrt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (final var link : SCAM_LINKS) { | ||
| if (msgContent.contains(link)) { | ||
| return true; | ||
| } | ||
| } | ||
Matyrobbrt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return false; | ||
| } | ||
|
|
||
| private static void setupScamLinks() { | ||
| MMDBot.LOGGER.debug("Setting up scam links! Receiving data from {}.", SCAM_LINKS_DATA_URL); | ||
| try (var is = new URL(SCAM_LINKS_DATA_URL).openStream()) { | ||
| final String result = new String(is.readAllBytes(), StandardCharsets.UTF_8); | ||
| GSON.fromJson(result, JsonArray.class) | ||
| .forEach(link -> SCAM_LINKS.add(link.getAsString())); | ||
Matyrobbrt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (final IOException e) { | ||
| MMDBot.LOGGER.error("Error while setting up scam links!", e); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.