-
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 11 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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/mcmoddev/mmdbot/modules/commands/bot/management/CmdRefreshScamLinks.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,33 @@ | ||
| package com.mcmoddev.mmdbot.modules.commands.bot.management; | ||
|
|
||
| import com.jagrosh.jdautilities.command.Command; | ||
| import com.jagrosh.jdautilities.command.CommandEvent; | ||
| import com.mcmoddev.mmdbot.modules.logging.misc.ScamDetector; | ||
|
|
||
| /** | ||
| * Refreshes the {@link com.mcmoddev.mmdbot.modules.logging.misc.ScamDetector#SCAM_LINKS} | ||
| * | ||
| * @author matyrobbrt | ||
| */ | ||
| public class CmdRefreshScamLinks extends Command { | ||
|
|
||
| public CmdRefreshScamLinks() { | ||
| name = "refresh-scam-links"; | ||
| aliases = new String[]{"refreshscamlinks"}; | ||
| help = "Refreshes the scam links"; | ||
| category = new Category("Management"); | ||
| hidden = true; | ||
| guildOnly = false; | ||
| ownerCommand = true; | ||
| } | ||
|
|
||
| @Override | ||
| protected void execute(final CommandEvent event) { | ||
| event.getMessage().reply("Refreshing scam links...").mentionRepliedUser(false).queue(msg -> { | ||
| new Thread(() -> { | ||
| ScamDetector.setupScamLinks(); | ||
| msg.editMessage("Scam links successfully refreshed!").queue(); | ||
Matyrobbrt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, "Refreshing scam links").start(); | ||
| }); | ||
| } | ||
| } | ||
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
156 changes: 156 additions & 0 deletions
156
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,156 @@ | ||
| /* | ||
| * 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.google.gson.JsonElement; | ||
| import com.mcmoddev.mmdbot.MMDBot; | ||
| import com.mcmoddev.mmdbot.utilities.Utils; | ||
| import com.mcmoddev.mmdbot.utilities.console.MMDMarkers; | ||
| import net.dv8tion.jda.api.EmbedBuilder; | ||
| import net.dv8tion.jda.api.Permission; | ||
| import net.dv8tion.jda.api.entities.Guild; | ||
| import net.dv8tion.jda.api.entities.Member; | ||
| import net.dv8tion.jda.api.entities.Message; | ||
| import net.dv8tion.jda.api.entities.MessageEmbed; | ||
| 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 net.dv8tion.jda.api.utils.MarkdownUtil; | ||
| 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.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.Locale; | ||
| import java.util.Set; | ||
| import java.util.function.Consumer; | ||
| import java.util.stream.StreamSupport; | ||
|
|
||
| /** | ||
| * Scam detection system | ||
| * @author matyrobbrt | ||
| */ | ||
| 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 Set<String> SCAM_LINKS = Collections.synchronizedSet(new HashSet<>()); | ||
|
|
||
| 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(); | ||
| final var member = msg.getMember(); | ||
| if (member == null || msg.getAuthor().isBot() || msg.getAuthor().isSystem() || | ||
| member.hasPermission(Permission.MANAGE_CHANNEL)) { | ||
| return; | ||
| } | ||
| if (containsScam(msg)) { | ||
| final var guild = msg.getGuild(); | ||
| final var embed = getLoggingEmbed(msg, ""); | ||
| msg.delete().reason("Scam link").queue($ -> { | ||
| executeInLoggingChannel(channel -> channel.sendMessageEmbeds(embed).queue()); | ||
| mute(guild, member); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onGuildMessageUpdate(@NotNull final GuildMessageUpdateEvent event) { | ||
| final var msg = event.getMessage(); | ||
| final var member = msg.getMember(); | ||
| if (member == null || msg.getAuthor().isBot() || msg.getAuthor().isSystem() || | ||
| member.hasPermission(Permission.MANAGE_CHANNEL)) { | ||
| return; | ||
| } | ||
| if (containsScam(msg)) { | ||
| final var guild = msg.getGuild(); | ||
| final var embed = getLoggingEmbed(msg, ", by editing an old message"); | ||
| msg.delete().reason("Scam link").queue($ -> { | ||
| executeInLoggingChannel(channel -> channel.sendMessageEmbeds(embed).queue()); | ||
| mute(guild, member); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| private static void mute(final Guild guild, final Member member) { | ||
| final var mutedRoleID = MMDBot.getConfig().getRole("muted"); | ||
| // TODO once JDA is updated, maybe timeout the user. It seems a better idea | ||
| final var mutedRole = guild.getRoleById(mutedRoleID); | ||
| if (mutedRole == null) { | ||
| MMDBot.LOGGER.error(MMDMarkers.MUTING, "Unable to find muted role {}", mutedRoleID); | ||
| return; | ||
| } | ||
| guild.addRoleToMember(member, mutedRole).queue(); | ||
| } | ||
|
|
||
| private static MessageEmbed getLoggingEmbed(final Message message, final String extraDescription) { | ||
| final var member = message.getMember(); | ||
| return new EmbedBuilder().setTitle("Scam link detected!") | ||
| .setDescription(String.format("User %s sent a scam link in %s%s. Their message was deleted, and they were muted.", member.getUser().getAsTag(), | ||
| message.getTextChannel().getAsMention(), extraDescription)) | ||
| .addField("Message Content", MarkdownUtil.codeblock(message.getContentRaw()), false) | ||
| .setColor(Color.RED) | ||
| .setTimestamp(Instant.now()) | ||
| .setFooter("User ID: " + member.getIdLong()) | ||
| .setThumbnail(member.getEffectiveAvatarUrl()).build(); | ||
| } | ||
|
|
||
| private static void executeInLoggingChannel(Consumer<TextChannel> channel) { | ||
| Utils.getChannelIfPresent(MMDBot.getConfig().getChannel("events.requests_deletion"), channel); | ||
| } | ||
|
|
||
| public static boolean containsScam(final Message message) { | ||
| final String msgContent = message.getContentRaw().toLowerCase(Locale.ROOT); | ||
| synchronized (SCAM_LINKS) { | ||
| for (final var link : SCAM_LINKS) { | ||
| if (msgContent.contains(link)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
KiriCattus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return false; | ||
| } | ||
|
|
||
| public static void setupScamLinks() { | ||
| SCAM_LINKS.clear(); | ||
Matyrobbrt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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); | ||
| SCAM_LINKS.addAll(StreamSupport.stream(GSON.fromJson(result, JsonArray.class).spliterator(), false) | ||
| .map(JsonElement::getAsString).filter(s -> !s.contains("discordapp.co")).toList()); | ||
sciwhiz12 marked this conversation as resolved.
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.