Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.mcmoddev.mmdbot.MMDBot;
import com.mcmoddev.mmdbot.modules.logging.misc.EventReactionAdded;
import com.mcmoddev.mmdbot.modules.logging.misc.ScamDetector;
import com.mcmoddev.mmdbot.modules.logging.users.EventNicknameChanged;
import com.mcmoddev.mmdbot.modules.logging.users.EventRoleAdded;
import com.mcmoddev.mmdbot.modules.logging.users.EventRoleRemoved;
Expand Down Expand Up @@ -50,7 +51,8 @@ public static void setupLoggingModule() {
new EventRoleAdded(),
new EventRoleRemoved(),
new EventReactionAdded(),
new UserBanned());
new UserBanned(),
new ScamDetector());
MMDBot.LOGGER.warn("Event logging module enabled and loaded.");
} else {
MMDBot.LOGGER.warn("Event logging module disabled via config, Discord event logging won't work right now!");
Expand Down
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();
}

@Override
public void onGuildMessageReceived(@NotNull final GuildMessageReceivedEvent 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");
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();
msgChannel.sendMessage(String.format("%s did you *really* think that would work?", member.getAsMention())).queue();
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");
msg.delete().queue($ -> {
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();
});
}
}

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();
for (final var link : SCAM_LINKS) {
if (msgContent.contains(link)) {
return true;
}
}
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()));
} catch (final IOException e) {
MMDBot.LOGGER.error("Error while setting up scam links!", e);
}
}
}