From b288238f4d473c43ec1a85f77c68299cc8d12e88 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Mon, 14 Mar 2022 13:06:44 +0100 Subject: [PATCH 01/11] Adding UserInteractor to the core system it basically moves getName(), onButtonClick() and onSelectionMenu() from SlashCommand one level higher - so that also non-slash-commands can use it --- .../tjbot/commands/SlashCommand.java | 100 +++--------------- .../tjbot/commands/UserInteractor.java | 84 +++++++++++++++ .../commands/componentids/ComponentId.java | 6 +- .../componentids/ComponentIdStore.java | 4 +- .../tjbot/commands/system/BotCore.java | 69 ++++++++---- .../commands/SlashCommandAdapterTest.java | 2 +- .../commands/system/ComponentIdTest.java | 7 +- 7 files changed, 156 insertions(+), 116 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java index fd1c2c2c08..1e85a892a6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java @@ -1,15 +1,12 @@ package org.togetherjava.tjbot.commands; import net.dv8tion.jda.api.entities.Emoji; -import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; -import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; -import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent; +import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; +import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.interactions.commands.build.CommandData; -import net.dv8tion.jda.api.interactions.commands.build.Commands; -import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; +import net.dv8tion.jda.api.interactions.components.ButtonStyle; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; -import net.dv8tion.jda.api.interactions.components.buttons.Button; -import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.componentids.ComponentId; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; @@ -31,32 +28,19 @@ * is then to be returned by {@link #getData()} where the system will then pick it up from. *

* After registration, the system will notify a command whenever one of its corresponding slash - * commands ({@link #onSlashCommand(SlashCommandInteractionEvent)}), buttons - * ({@link #onButtonClick(ButtonInteractionEvent, List)}) or menus - * ({@link #onSelectionMenu(SelectMenuInteractionEvent, List)}) have been triggered. + * commands ({@link #onSlashCommand(SlashCommandEvent)}), buttons + * ({@link #onButtonClick(ButtonClickEvent, List)}) or menus + * ({@link #onSelectionMenu(SelectionMenuEvent, List)}) have been triggered. *

*

* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}. */ -public interface SlashCommand extends Feature { - - /** - * Gets the name of the command. - *

- * Requirements for this are documented in {@link Commands#slash(String, String)}. - *

- *

- * After registration of the command, the name must not change anymore. - * - * @return the name of the command - */ - @NotNull - String getName(); +public interface SlashCommand extends UserInteractor { /** * Gets the description of the command. *

- * Requirements for this are documented in {@link Commands#slash(String, String)}. + * Requirements for this are documented in {@link CommandData#CommandData(String, String)}. *

*

* After registration of the command, the description must not change anymore. @@ -92,7 +76,7 @@ public interface SlashCommand extends Feature { * @return the command data of this command */ @NotNull - SlashCommandData getData(); + CommandData getData(); /** * Triggered by the core system when a slash command corresponding to this implementation (based @@ -107,9 +91,9 @@ public interface SlashCommand extends Feature { *

* Buttons or menus have to be created with a component ID (see * {@link ComponentInteraction#getComponentId()}, - * {@link Button#of(ButtonStyle, String, Emoji)}}) in a very specific format, otherwise the core - * system will fail to identify the command that corresponded to the button or menu click event - * and is unable to route it back. + * {@link net.dv8tion.jda.api.interactions.components.Button#of(ButtonStyle, String, Emoji)}) in + * a very specific format, otherwise the core system will fail to identify the command that + * corresponded to the button or menu click event and is unable to route it back. *

* The component ID has to be a UUID-string (see {@link java.util.UUID}), which is associated to * a specific database entry, containing meta information about the command being executed. Such @@ -118,8 +102,8 @@ public interface SlashCommand extends Feature { * given to {@link #acceptComponentIdGenerator(ComponentIdGenerator)} during system setup. The * required {@link ComponentId} instance accepts optional extra arguments, which, if provided, * can be picked up during the corresponding event (see - * {@link #onButtonClick(ButtonInteractionEvent, List)}, - * {@link #onSelectionMenu(SelectMenuInteractionEvent, List)}). + * {@link #onButtonClick(ButtonClickEvent, List)}, + * {@link #onSelectionMenu(SelectionMenuEvent, List)}). *

* Alternatively, if {@link SlashCommandAdapter} has been extended, it also offers a handy * {@link SlashCommandAdapter#generateComponentId(String...)} method to ease the flow. @@ -132,57 +116,5 @@ public interface SlashCommand extends Feature { * * @param event the event that triggered this */ - void onSlashCommand(@NotNull SlashCommandInteractionEvent event); - - /** - * Triggered by the core system when a button corresponding to this implementation (based on - * {@link #getData()}) has been clicked. - *

- * This method may be called multi-threaded. In particular, there are no guarantees that it will - * be executed on the same thread repeatedly or on the same thread that other event methods have - * been called on. - *

- * Details are available in the given event and the event also enables implementations to - * respond to it. - *

- * This method will be called in a multi-threaded context and the event may not be hold valid - * forever. - * - * @param event the event that triggered this - * @param args the arguments transported with the button, see - * {@link #onSlashCommand(SlashCommandInteractionEvent)} for details on how these are - * created - */ - void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List args); - - /** - * Triggered by the core system when a selection menu corresponding to this implementation - * (based on {@link #getData()}) has been clicked. - *

- * This method may be called multi-threaded. In particular, there are no guarantees that it will - * be executed on the same thread repeatedly or on the same thread that other event methods have - * been called on. - *

- * Details are available in the given event and the event also enables implementations to - * respond to it. - *

- * This method will be called in a multi-threaded context and the event may not be hold valid - * forever. - * - * @param event the event that triggered this - * @param args the arguments transported with the selection menu, see - * {@link #onSlashCommand(SlashCommandInteractionEvent)} for details on how these are - * created - */ - void onSelectionMenu(@NotNull SelectMenuInteractionEvent event, @NotNull List args); - - /** - * Triggered by the core system during its setup phase. It will provide the command a component - * id generator through this method, which can be used to generate component ids, as used for - * button or selection menus. See {@link #onSlashCommand(SlashCommandInteractionEvent)} for - * details on how to use this. - * - * @param generator the provided component id generator - */ - void acceptComponentIdGenerator(@NotNull ComponentIdGenerator generator); + void onSlashCommand(@NotNull SlashCommandEvent event); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java new file mode 100644 index 0000000000..908fd3ac1a --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java @@ -0,0 +1,84 @@ +package org.togetherjava.tjbot.commands; + +import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; +import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; + +import java.util.List; + +/** + * Represents a feature that can interact with users. The most important implementation is + * {@link SlashCommand}. + *

+ * An interactor must have a unique name and can react to button clicks and selection menu actions. + */ +public interface UserInteractor extends Feature { + + /** + * Gets the name of the interactor. + *

+ * Requirements for this are documented in {@link CommandData#CommandData(String, String)}. + *

+ *

+ * After registration of the interactor, the name must not change anymore. + * + * @return the name of the interactor + */ + @NotNull + String getName(); + + /** + * Triggered by the core system when a button corresponding to this implementation (based on + * {@link #getName()}) has been clicked. + *

+ * This method may be called multi-threaded. In particular, there are no guarantees that it will + * be executed on the same thread repeatedly or on the same thread that other event methods have + * been called on. + *

+ * Details are available in the given event and the event also enables implementations to + * respond to it. + *

+ * This method will be called in a multi-threaded context and the event may not be hold valid + * forever. + * + * @param event the event that triggered this + * @param args the arguments transported with the button, see + * {@link SlashCommand#onSlashCommand(SlashCommandEvent)} for details on how these are + * created + */ + void onButtonClick(@NotNull ButtonClickEvent event, @NotNull List args); + + /** + * Triggered by the core system when a selection menu corresponding to this implementation + * (based on {@link #getName()}) has been clicked. + *

+ * This method may be called multi-threaded. In particular, there are no guarantees that it will + * be executed on the same thread repeatedly or on the same thread that other event methods have + * been called on. + *

+ * Details are available in the given event and the event also enables implementations to + * respond to it. + *

+ * This method will be called in a multi-threaded context and the event may not be hold valid + * forever. + * + * @param event the event that triggered this + * @param args the arguments transported with the selection menu, see + * {@link SlashCommand#onSlashCommand(SlashCommandEvent)} for details on how these are + * created + */ + void onSelectionMenu(@NotNull SelectionMenuEvent event, @NotNull List args); + + /** + * Triggered by the core system during its setup phase. It will provide the interactor a + * component id generator through this method, which can be used to generate component ids, as + * used for button or selection menus. See + * {@link SlashCommand#onSlashCommand(SlashCommandEvent)} for details on how to use this. + * + * @param generator the provided component id generator + */ + void acceptComponentIdGenerator(@NotNull ComponentIdGenerator generator); +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentId.java b/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentId.java index 4b31487515..49596eec7f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentId.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentId.java @@ -9,9 +9,9 @@ * {@link org.togetherjava.tjbot.commands.SlashCommand#onSlashCommand(SlashCommandInteractionEvent)} * for its usages. * - * @param commandName the name of the command that handles the event associated to this component - * ID, when triggered + * @param userInteractorName the name of the user interactor that handles the event associated to + * this component ID, when triggered * @param elements the additional elements to carry along this component ID, empty if not desired */ -public record ComponentId(@NotNull String commandName, @NotNull List elements) { +public record ComponentId(@NotNull String userInteractorName, @NotNull List elements) { } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdStore.java b/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdStore.java index ad4dcde67e..f611d74be7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdStore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdStore.java @@ -266,8 +266,8 @@ private void evictDatabase() { recordToDelete.delete(); evictedCounter.getAndIncrement(); logger.debug( - "Evicted component id with uuid '{}' from command '{}', last used '{}'", - uuid, componentId.commandName(), lastUsed); + "Evicted component id with uuid '{}' from user interactor '{}', last used '{}'", + uuid, componentId.userInteractorName(), lastUsed); // Remove them from the cache if still in there storeCache.invalidate(uuid); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index 55d3dc160b..1473d2b5fb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -55,7 +55,8 @@ public final class BotCore extends ListenerAdapter implements SlashCommandProvid private static final ScheduledExecutorService ROUTINE_SERVICE = Executors.newScheduledThreadPool(5); private final Config config; - private final Map nameToSlashCommands; + private final Map nameToInteractor; + private final Map nameToSlashCommand; private final ComponentIdParser componentIdParser; private final ComponentIdStore componentIdStore; private final Map channelNameToMessageReceiver = new HashMap<>(); @@ -104,22 +105,32 @@ public BotCore(@NotNull JDA jda, @NotNull Database database, @NotNull Config con } }); - // Slash commands - nameToSlashCommands = features.stream() + // User Interactors + nameToInteractor = features.stream() + .filter(UserInteractor.class::isInstance) + .map(UserInteractor.class::cast) + .collect(Collectors.toMap(UserInteractor::getName, Function.identity())); + + // Slash Commands + nameToSlashCommand = features.stream() .filter(SlashCommand.class::isInstance) .map(SlashCommand.class::cast) .collect(Collectors.toMap(SlashCommand::getName, Function.identity())); - if (nameToSlashCommands.containsKey(RELOAD_COMMAND)) { + // Reload Command + if (nameToInteractor.containsKey(RELOAD_COMMAND)) { throw new IllegalStateException( - "The 'reload' command is a special reserved command that must not be used by other commands"); + "The 'reload' command is a special reserved command that must not be used by other user interactors"); } - nameToSlashCommands.put(RELOAD_COMMAND, new ReloadCommand(this)); + SlashCommand reloadCommand = new ReloadCommand(this); + nameToInteractor.put(RELOAD_COMMAND, reloadCommand); + nameToSlashCommand.put(RELOAD_COMMAND, reloadCommand); + // Component Id Store componentIdStore = new ComponentIdStore(database); componentIdStore.addComponentIdRemovedListener(BotCore::onComponentIdRemoved); componentIdParser = uuid -> componentIdStore.get(UUID.fromString(uuid)); - nameToSlashCommands.values() + nameToInteractor.values() .forEach(slashCommand -> slashCommand .acceptComponentIdGenerator(((componentId, lifespan) -> { UUID uuid = UUID.randomUUID(); @@ -128,18 +139,18 @@ public BotCore(@NotNull JDA jda, @NotNull Database database, @NotNull Config con }))); if (logger.isInfoEnabled()) { - logger.info("Available commands: {}", nameToSlashCommands.keySet()); + logger.info("Available commands: {}", nameToSlashCommand.keySet()); } } @Override public @NotNull Collection getSlashCommands() { - return Collections.unmodifiableCollection(nameToSlashCommands.values()); + return Collections.unmodifiableCollection(nameToSlashCommand.values()); } @Override public @NotNull Optional getSlashCommand(@NotNull String name) { - return Optional.ofNullable(nameToSlashCommands.get(name)); + return Optional.ofNullable(nameToSlashCommand.get(name)); } @Override @@ -192,7 +203,8 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even public void onButtonInteraction(@NotNull ButtonInteractionEvent event) { logger.debug("Received button click '{}' (#{}) on guild '{}'", event.getComponentId(), event.getId(), event.getGuild()); - COMMAND_SERVICE.execute(() -> forwardComponentCommand(event, SlashCommand::onButtonClick)); + COMMAND_SERVICE + .execute(() -> forwardComponentCommand(event, UserInteractor::onButtonClick)); } @Override @@ -200,7 +212,7 @@ public void onSelectMenuInteraction(@NotNull SelectMenuInteractionEvent event) { logger.debug("Received selection menu event '{}' (#{}) on guild '{}'", event.getComponentId(), event.getId(), event.getGuild()); COMMAND_SERVICE - .execute(() -> forwardComponentCommand(event, SlashCommand::onSelectionMenu)); + .execute(() -> forwardComponentCommand(event, UserInteractor::onSelectionMenu)); } private void registerReloadCommand(@NotNull Guild guild) { @@ -221,32 +233,32 @@ private void registerReloadCommand(@NotNull Guild guild) { } /** - * Forwards the given component event to the associated slash command. + * Forwards the given component event to the associated user interactor. *

*

* An example call might look like: * *

      * {@code
-     * forwardComponentCommand(event, SlashCommand::onSelectionMenu);
+     * forwardComponentCommand(event, UserInteractor::onSelectionMenu);
      * }
      * 
* * @param event the component event that should be forwarded - * @param commandArgumentConsumer the action to trigger on the associated slash command, + * @param interactorArgumentConsumer the action to trigger on the associated user interactor, * providing the event and list of arguments for consumption * @param the type of the component interaction that should be forwarded */ private void forwardComponentCommand(@NotNull T event, - @NotNull TriConsumer> commandArgumentConsumer) { + @NotNull TriConsumer> interactorArgumentConsumer) { Optional componentIdOpt; try { componentIdOpt = componentIdParser.parse(event.getComponentId()); } catch (InvalidComponentIdFormatException e) { logger - .error("Unable to route event (#{}) back to its corresponding slash command. The component ID was in an unexpected format." + .error("Unable to route event (#{}) back to its corresponding user interactor. The component ID was in an unexpected format." + " All button and menu events have to use a component ID created in a specific format" - + " (refer to the documentation of SlashCommand). Component ID was: {}", + + " (refer to the documentation of UserInteractor). Component ID was: {}", event.getId(), event.getComponentId(), e); // Unable to forward, simply fade out the event return; @@ -261,10 +273,10 @@ private void forwardComponentCommand(@NotNull T } ComponentId componentId = componentIdOpt.orElseThrow(); - SlashCommand command = requireSlashCommand(componentId.commandName()); - logger.trace("Routing a component event with id '{}' back to command '{}'", - event.getComponentId(), command.getName()); - commandArgumentConsumer.accept(command, event, componentId.elements()); + UserInteractor interactor = requireUserInteractor(componentId.userInteractorName()); + logger.trace("Routing a component event with id '{}' back to user interactor '{}'", + event.getComponentId(), interactor.getName()); + interactorArgumentConsumer.accept(interactor, event, componentId.elements()); } /** @@ -275,7 +287,18 @@ private void forwardComponentCommand(@NotNull T * @throws NullPointerException if the command with the given name was not registered */ private @NotNull SlashCommand requireSlashCommand(@NotNull String name) { - return Objects.requireNonNull(nameToSlashCommands.get(name)); + return Objects.requireNonNull(nameToSlashCommand.get(name)); + } + + /** + * Gets the given user interactor by its name and requires that it exists. + * + * @param name the name of the user interactor to get + * @return the user interactor with the given name + * @throws NullPointerException if the user interactor with the given name was not registered + */ + private @NotNull UserInteractor requireUserInteractor(@NotNull String name) { + return Objects.requireNonNull(nameToInteractor.get(name)); } private void handleRegisterErrors(Throwable ex, Guild guild) { diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/SlashCommandAdapterTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/SlashCommandAdapterTest.java index a665fceb56..d5e579f825 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/SlashCommandAdapterTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/SlashCommandAdapterTest.java @@ -66,7 +66,7 @@ void generateComponentId() { // Test that the adapter uses the given generator SlashCommandAdapter adapter = createAdapter(); adapter.acceptComponentIdGenerator((componentId, lifespan) -> "%s;%s;%s" - .formatted(componentId.commandName(), componentId.elements().size(), lifespan)); + .formatted(componentId.userInteractorName(), componentId.elements().size(), lifespan)); // No lifespan given String[] elements = {"foo", "bar", "baz"}; diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/system/ComponentIdTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/system/ComponentIdTest.java index f36a1012b7..e1a70de0a1 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/system/ComponentIdTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/system/ComponentIdTest.java @@ -9,9 +9,10 @@ final class ComponentIdTest { @Test - void getCommandName() { - String commandName = "foo"; - assertEquals(commandName, new ComponentId(commandName, List.of()).commandName()); + void getUserInteractorName() { + String userInteractorName = "foo"; + assertEquals(userInteractorName, + new ComponentId(userInteractorName, List.of()).userInteractorName()); } @Test From 051526b42d4473769edc7d57d3d50ed22b19dee8 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Mon, 14 Mar 2022 13:00:02 +0100 Subject: [PATCH 02/11] Added scam detector and scam blocker --- PP.md | 2 +- application/config.json.template | 10 +- .../togetherjava/tjbot/commands/Features.java | 11 +- .../commands/moderation/scam/ScamBlocker.java | 364 ++++++++++++++++++ .../moderation/scam/ScamDetector.java | 105 +++++ .../scam/ScamHistoryPurgeRoutine.java | 36 ++ .../moderation/scam/ScamHistoryStore.java | 164 ++++++++ .../moderation/scam/package-info.java | 5 + .../tjbot/commands/utils/Hashing.java | 62 +++ .../org/togetherjava/tjbot/config/Config.java | 18 +- .../tjbot/config/ScamBlockerConfig.java | 126 ++++++ .../resources/db/V9__Add_Scam_History.sql | 11 + 12 files changed, 908 insertions(+), 6 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetector.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamHistoryPurgeRoutine.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamHistoryStore.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/package-info.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/utils/Hashing.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/config/ScamBlockerConfig.java create mode 100644 application/src/main/resources/db/V9__Add_Scam_History.sql diff --git a/PP.md b/PP.md index abad2fdbaa..a82deaac35 100644 --- a/PP.md +++ b/PP.md @@ -111,4 +111,4 @@ This policy is not applicable to any information collected by **bot** instances People may get in contact through e-mail at [together.java.tjbot@gmail.com](mailto:together.java.tjbot@gmail.com), or through **Together Java**'s [official Discord](https://discord.com/invite/XXFUXzK). -Other ways of support may be provided but are not guaranteed. \ No newline at end of file +Other ways of support may be provided but are not guaranteed. diff --git a/application/config.json.template b/application/config.json.template index 39e980ee7e..58bb204c19 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -22,5 +22,13 @@ "upVoteEmoteName": "peepo_yes", "downVoteEmoteName": "peepo_no" }, - "quarantinedRolePattern": "Quarantined" + "quarantinedRolePattern": "Quarantined", + "scamBlocker": { + "mode": "AUTO_DELETE_BUT_APPROVE_QUARANTINE", + "reportChannelPattern": "commands", + "hostWhitelist": ["discord.com", "discord.gg", "discord.media", "discordapp.com", "discordapp.net", "discordstatus.com"], + "hostBlacklist": ["bit.ly"], + "suspiciousHostKeywords": ["discord", "nitro", "premium"], + "isHostSimilarToKeywordDistanceThreshold": 2 + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java index 904c831439..095ee74662 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -2,10 +2,16 @@ import net.dv8tion.jda.api.JDA; import org.jetbrains.annotations.NotNull; -import org.togetherjava.tjbot.commands.basic.*; +import org.togetherjava.tjbot.commands.basic.PingCommand; +import org.togetherjava.tjbot.commands.basic.RoleSelectCommand; +import org.togetherjava.tjbot.commands.basic.SuggestionsUpDownVoter; +import org.togetherjava.tjbot.commands.basic.VcActivityCommand; import org.togetherjava.tjbot.commands.free.FreeCommand; import org.togetherjava.tjbot.commands.mathcommands.TeXCommand; import org.togetherjava.tjbot.commands.moderation.*; +import org.togetherjava.tjbot.commands.moderation.scam.ScamBlocker; +import org.togetherjava.tjbot.commands.moderation.scam.ScamHistoryPurgeRoutine; +import org.togetherjava.tjbot.commands.moderation.scam.ScamHistoryStore; import org.togetherjava.tjbot.commands.moderation.temp.TemporaryModerationRoutine; import org.togetherjava.tjbot.commands.reminder.RemindCommand; import org.togetherjava.tjbot.commands.reminder.RemindRoutine; @@ -52,6 +58,7 @@ public enum Features { TagSystem tagSystem = new TagSystem(database); ModerationActionsStore actionsStore = new ModerationActionsStore(database); ModAuditLogWriter modAuditLogWriter = new ModAuditLogWriter(config); + ScamHistoryStore scamHistoryStore = new ScamHistoryStore(database); // NOTE The system can add special system relevant commands also by itself, // hence this list may not necessarily represent the full list of all commands actually @@ -63,10 +70,12 @@ public enum Features { features.add(new TemporaryModerationRoutine(jda, actionsStore, config)); features.add(new TopHelpersPurgeMessagesRoutine(database)); features.add(new RemindRoutine(database)); + features.add(new ScamHistoryPurgeRoutine(scamHistoryStore)); // Message receivers features.add(new TopHelpersMessageListener(database, config)); features.add(new SuggestionsUpDownVoter(config)); + features.add(new ScamBlocker(actionsStore, scamHistoryStore, config)); // Event receivers features.add(new RejoinModerationRoleListener(actionsStore, config)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java new file mode 100644 index 0000000000..507010488c --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java @@ -0,0 +1,364 @@ +package org.togetherjava.tjbot.commands.moderation.scam; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.MessageBuilder; +import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; +import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import net.dv8tion.jda.api.exceptions.ErrorResponseException; +import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.Button; +import net.dv8tion.jda.api.interactions.components.ButtonStyle; +import net.dv8tion.jda.api.requests.ErrorResponse; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.commands.MessageReceiverAdapter; +import org.togetherjava.tjbot.commands.UserInteractor; +import org.togetherjava.tjbot.commands.componentids.ComponentId; +import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; +import org.togetherjava.tjbot.commands.componentids.Lifespan; +import org.togetherjava.tjbot.commands.moderation.ModerationAction; +import org.togetherjava.tjbot.commands.moderation.ModerationActionsStore; +import org.togetherjava.tjbot.commands.moderation.ModerationUtils; +import org.togetherjava.tjbot.commands.utils.MessageUtils; +import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.config.ScamBlockerConfig; + +import java.awt.Color; +import java.util.*; +import java.util.function.Consumer; +import java.util.function.Predicate; +import java.util.regex.Pattern; + +/** + * Listener that receives all sent messages from channels, checks them for scam and takes + * appropriate action. + *

+ * If scam is detected, depending on the configuration, the blockers actions range from deleting the + * message and banning the author to just logging the message for auditing. + */ +public final class ScamBlocker extends MessageReceiverAdapter implements UserInteractor { + private static final Logger logger = LoggerFactory.getLogger(ScamBlocker.class); + private static final Color AMBIENT_COLOR = Color.decode("#CFBFF5"); + private static final Set MODES_WITH_IMMEDIATE_DELETION = + EnumSet.of(ScamBlockerConfig.Mode.AUTO_DELETE_BUT_APPROVE_QUARANTINE, + ScamBlockerConfig.Mode.AUTO_DELETE_AND_QUARANTINE); + + private final ScamBlockerConfig.Mode mode; + private final String reportChannelPattern; + private final Predicate isReportChannel; + private final ScamDetector scamDetector; + private final Config config; + private final ModerationActionsStore actionsStore; + private final ScamHistoryStore scamHistoryStore; + private final Predicate hasRequiredRole; + + private ComponentIdGenerator componentIdGenerator; + + /** + * Creates a new listener to receive all message sent in any channel. + * + * @param actionsStore to store quarantine actions in + * @param scamHistoryStore to store and retrieve scam history from + * @param config the config to use for this + */ + public ScamBlocker(@NotNull ModerationActionsStore actionsStore, + @NotNull ScamHistoryStore scamHistoryStore, @NotNull Config config) { + super(Pattern.compile(".*")); + + this.actionsStore = actionsStore; + this.scamHistoryStore = scamHistoryStore; + this.config = config; + mode = config.getScamBlocker().getMode(); + scamDetector = new ScamDetector(config); + + reportChannelPattern = config.getScamBlocker().getReportChannelPattern(); + Predicate isReportChannelName = + Pattern.compile(reportChannelPattern).asMatchPredicate(); + isReportChannel = channel -> isReportChannelName.test(channel.getName()); + hasRequiredRole = Pattern.compile(config.getSoftModerationRolePattern()).asMatchPredicate(); + } + + @Override + public @NotNull String getName() { + return "scam-blocker"; + } + + @Override + public void onSelectionMenu(@NotNull SelectionMenuEvent event, @NotNull List args) { + throw new UnsupportedOperationException("Not used"); + } + + @Override + public void acceptComponentIdGenerator(@NotNull ComponentIdGenerator generator) { + componentIdGenerator = generator; + } + + @Override + public void onMessageReceived(@NotNull GuildMessageReceivedEvent event) { + if (event.getAuthor().isBot() || event.isWebhookMessage()) { + return; + } + + if (mode == ScamBlockerConfig.Mode.OFF) { + return; + } + + Message message = event.getMessage(); + String content = message.getContentDisplay(); + if (!scamDetector.isScam(content)) { + return; + } + + if (scamHistoryStore.hasRecentScamDuplicate(message)) { + takeActionWasAlreadyReported(event); + return; + } + + takeAction(event); + } + + private void takeActionWasAlreadyReported(@NotNull GuildMessageReceivedEvent event) { + // The user recently send the same scam already, and that was already reported and handled + addScamToHistory(event); + + boolean shouldDeleteMessage = MODES_WITH_IMMEDIATE_DELETION.contains(mode); + if (shouldDeleteMessage) { + deleteMessage(event); + } + } + + private void takeAction(@NotNull GuildMessageReceivedEvent event) { + switch (mode) { + case OFF -> throw new AssertionError( + "The OFF-mode should be detected earlier already to prevent expensive computation"); + case ONLY_LOG -> takeActionLogOnly(event); + case APPROVE_FIRST -> takeActionApproveFirst(event); + case AUTO_DELETE_BUT_APPROVE_QUARANTINE -> takeActionAutoDeleteButApproveQuarantine( + event); + case AUTO_DELETE_AND_QUARANTINE -> takeActionAutoDeleteAndQuarantine(event); + default -> throw new IllegalArgumentException("Mode not supported: " + mode); + } + } + + private void takeActionLogOnly(@NotNull GuildMessageReceivedEvent event) { + addScamToHistory(event); + logScamMessage(event); + } + + private void takeActionApproveFirst(@NotNull GuildMessageReceivedEvent event) { + addScamToHistory(event); + logScamMessage(event); + reportScamMessage(event, "Is this scam?", createConfirmDialog(event)); + } + + private void takeActionAutoDeleteButApproveQuarantine( + @NotNull GuildMessageReceivedEvent event) { + addScamToHistory(event); + logScamMessage(event); + deleteMessage(event); + reportScamMessage(event, "Is this scam? (already deleted)", createConfirmDialog(event)); + } + + private void takeActionAutoDeleteAndQuarantine(@NotNull GuildMessageReceivedEvent event) { + addScamToHistory(event); + logScamMessage(event); + deleteMessage(event); + quarantineAuthor(event); + dmUser(event); + reportScamMessage(event, "Detected and handled scam", List.of()); + } + + private void addScamToHistory(@NotNull GuildMessageReceivedEvent event) { + scamHistoryStore.addScam(event.getMessage(), MODES_WITH_IMMEDIATE_DELETION.contains(mode)); + } + + private void logScamMessage(@NotNull GuildMessageReceivedEvent event) { + logger.warn("Detected a scam message ('{}') from user '{}' in channel '{}' of guild '{}'.", + event.getMessageId(), event.getAuthor().getId(), event.getChannel().getId(), + event.getGuild().getId()); + } + + private void deleteMessage(@NotNull GuildMessageReceivedEvent event) { + event.getMessage().delete().queue(); + } + + private void quarantineAuthor(@NotNull GuildMessageReceivedEvent event) { + quarantineAuthor(event.getGuild(), event.getMember(), event.getJDA().getSelfUser()); + } + + private void quarantineAuthor(@NotNull Guild guild, @NotNull Member author, + @NotNull SelfUser bot) { + String reason = "User posted scam that was automatically detected"; + + actionsStore.addAction(guild.getIdLong(), bot.getIdLong(), author.getIdLong(), + ModerationAction.QUARANTINE, null, reason); + + guild + .addRoleToMember(author, + ModerationUtils.getQuarantinedRole(guild, config).orElseThrow()) + .reason(reason) + .queue(); + } + + private void reportScamMessage(@NotNull GuildMessageReceivedEvent event, + @NotNull String reportTitle, @NotNull Collection actionRows) { + Guild guild = event.getGuild(); + Optional reportChannel = getReportChannel(guild); + if (reportChannel.isEmpty()) { + logger.warn( + "Unable to report a scam message, did not find a report channel matching the configured pattern '{}' for guild '{}'", + reportChannelPattern, guild.getName()); + return; + } + + User author = event.getAuthor(); + MessageEmbed embed = + new EmbedBuilder().setDescription(event.getMessage().getContentStripped()) + .setTitle(reportTitle) + .setAuthor(author.getAsTag(), null, author.getAvatarUrl()) + .setTimestamp(event.getMessage().getTimeCreated()) + .setColor(AMBIENT_COLOR) + .setFooter(author.getId()) + .build(); + Message message = new MessageBuilder().setEmbeds(embed).setActionRows(actionRows).build(); + + reportChannel.orElseThrow().sendMessage(message).queue(); + } + + private void dmUser(@NotNull GuildMessageReceivedEvent event) { + dmUser(event.getGuild(), event.getAuthor().getIdLong(), event.getJDA()); + } + + private void dmUser(@NotNull Guild guild, long userId, @NotNull JDA jda) { + String dmMessage = + """ + Hey there, we detected that you did send scam in the server %s and therefore put you under quarantine. + This means you can no longer interact with anyone in the server until you have been unquarantined again. + + If you think this was a mistake (for example, your account was hacked, but you got back control over it), + please contact a moderator or admin of the server. + """ + .formatted(guild.getName()); + + jda.openPrivateChannelById(userId) + .flatMap(channel -> channel.sendMessage(dmMessage)) + .queue(); + } + + private Optional getReportChannel(@NotNull Guild guild) { + // Check cache first, then get full list + return guild.getTextChannelCache() + .stream() + .filter(isReportChannel) + .findAny() + .or(() -> guild.getTextChannels().stream().filter(isReportChannel).findAny()); + } + + private List createConfirmDialog(@NotNull GuildMessageReceivedEvent event) { + ComponentIdArguments args = new ComponentIdArguments(mode, event.getGuild().getIdLong(), + event.getChannel().getIdLong(), event.getMessageIdLong(), + event.getAuthor().getIdLong(), + ScamHistoryStore.hashMessageContent(event.getMessage())); + + return List + .of(ActionRow.of(Button.of(ButtonStyle.SUCCESS, generateComponentId(args), "Yes"), + Button.of(ButtonStyle.DANGER, generateComponentId(args), "No"))); + } + + private @NotNull String generateComponentId(@NotNull ComponentIdArguments args) { + return Objects.requireNonNull(componentIdGenerator) + .generate(new ComponentId(getName(), args.toList()), Lifespan.REGULAR); + } + + @Override + public void onButtonClick(@NotNull ButtonClickEvent event, @NotNull List argsRaw) { + ComponentIdArguments args = ComponentIdArguments.fromList(argsRaw); + if (event.getMember().getRoles().stream().map(Role::getName).noneMatch(hasRequiredRole)) { + event.reply( + "You can not handle scam in this guild, since you do not have the required role.") + .setEphemeral(true) + .queue(); + return; + } + + MessageUtils.disableButtons(event.getMessage()); + event.deferEdit().queue(); + if (event.getButton().getStyle() == ButtonStyle.DANGER) { + logger.info( + "Identified a false-positive scam (id '{}', hash '{}') in guild '{}' sent by author '{}'", + args.messageId, args.contentHash, args.guildId, args.authorId); + return; + } + Guild guild = event.getJDA().getGuildById(args.guildId); + if (guild == null) { + logger.debug( + "Attempted to handle scam, but the bot is not connected to the guild '{}' anymore, skipping scam handling.", + args.guildId); + return; + } + + Consumer onRetrieveAuthorSuccess = author -> { + quarantineAuthor(guild, author, event.getJDA().getSelfUser()); + dmUser(guild, args.authorId, event.getJDA()); + + // Delete all messages like this + Collection scamMessages = scamHistoryStore + .markScamDuplicatesDeleted(args.guildId, args.authorId, args.contentHash); + + scamMessages.forEach(scamMessage -> { + TextChannel channel = guild.getTextChannelById(scamMessage.channelId()); + if (channel == null) { + logger.debug( + "Attempted to delete scam messages, bot the channel '{}' does not exist anymore, skipping deleting messages for this channel.", + scamMessage.channelId()); + return; + } + + channel.deleteMessageById(scamMessage.messageId()).mapToResult().queue(); + }); + }; + + Consumer onRetrieveAuthorFailure = failure -> { + if (failure instanceof ErrorResponseException errorResponseException) { + if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { + logger.debug("Attempted to handle scam, but user '{}' does not exist anymore.", + args.authorId); + return; + } + + if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MEMBER) { + logger.debug( + "Attempted to handle scam, but user '{}' is not a member of the guild anymore.", + args.authorId); + } + } + }; + + guild.retrieveMemberById(args.authorId) + .queue(onRetrieveAuthorSuccess, onRetrieveAuthorFailure); + } + + + private record ComponentIdArguments(@NotNull ScamBlockerConfig.Mode mode, long guildId, + long channelId, long messageId, long authorId, String contentHash) { + static ComponentIdArguments fromList(@NotNull List args) { + ScamBlockerConfig.Mode mode = ScamBlockerConfig.Mode.valueOf(args.get(0)); + long guildId = Long.parseLong(args.get(1)); + long channelId = Long.parseLong(args.get(2)); + long messageId = Long.parseLong(args.get(3)); + long authorId = Long.parseLong(args.get(4)); + String contentHash = args.get(5); + return new ComponentIdArguments(mode, guildId, channelId, messageId, authorId, + contentHash); + } + + List toList() { + return List.of(mode.name(), Long.toString(guildId), Long.toString(channelId), + Long.toString(messageId), Long.toString(authorId), contentHash); + } + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetector.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetector.java new file mode 100644 index 0000000000..1acb1485cc --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetector.java @@ -0,0 +1,105 @@ +package org.togetherjava.tjbot.commands.moderation.scam; + +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.utils.StringDistances; +import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.config.ScamBlockerConfig; + +import java.net.URI; +import java.util.regex.Pattern; + +public class ScamDetector { + private static final Pattern TOKENIZER = Pattern.compile("[\\s,]"); + private final ScamBlockerConfig config; + + public ScamDetector(@NotNull Config config) { + this.config = config.getScamBlocker(); + } + + public boolean isScam(@NotNull CharSequence message) { + AnalyseResults results = new AnalyseResults(); + TOKENIZER.splitAsStream(message).forEach(token -> analyzeToken(token, results)); + return isScam(results); + } + + private boolean isScam(@NotNull AnalyseResults results) { + if (results.pingsEveryone && results.containsNitroKeyword && results.hasUrl) { + return true; + } + return results.containsNitroKeyword && results.hasSuspiciousUrl; + } + + private void analyzeToken(@NotNull String token, @NotNull AnalyseResults results) { + if ("@everyone".equalsIgnoreCase(token)) { + results.pingsEveryone = true; + } + if ("nitro".equalsIgnoreCase(token)) { + results.containsNitroKeyword = true; + } + + if (token.startsWith("http")) { + analyzeUrl(token, results); + } + } + + private void analyzeUrl(@NotNull String url, @NotNull AnalyseResults results) { + String host; + try { + host = URI.create(url).getHost(); + } catch (IllegalArgumentException e) { + // Invalid urls are not scam + return; + } + if (host == null) { + return; + } + + results.hasUrl = true; + + if (config.getHostWhitelist().contains(host)) { + return; + } + + if (config.getHostBlacklist().contains(host)) { + results.hasSuspiciousUrl = true; + return; + } + + for (String keyword : config.getSuspiciousHostKeywords()) { + if (isHostSimilarToKeyword(host, keyword)) { + results.hasSuspiciousUrl = true; + break; + } + } + } + + private boolean isHostSimilarToKeyword(@NotNull String host, @NotNull String keyword) { + // NOTE This algorithm is far from optimal. + // It is good enough for our purpose though and not that complex. + + // Rolling window of keyword-size over host. + // If any window has a small distance, it is similar + int windowStart = 0; + int windowEnd = keyword.length(); + while (windowEnd <= host.length()) { + String window = host.substring(windowStart, windowEnd); + int distance = StringDistances.editDistance(keyword, window); + + if (distance <= config.getIsHostSimilarToKeywordDistanceThreshold()) { + return true; + } + + windowStart++; + windowEnd++; + } + + return false; + } + + private static class AnalyseResults { + private boolean pingsEveryone; + private boolean containsNitroKeyword; + private boolean hasUrl; + private boolean hasSuspiciousUrl; + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamHistoryPurgeRoutine.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamHistoryPurgeRoutine.java new file mode 100644 index 0000000000..649f793b88 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamHistoryPurgeRoutine.java @@ -0,0 +1,36 @@ +package org.togetherjava.tjbot.commands.moderation.scam; + +import net.dv8tion.jda.api.JDA; +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.Routine; + +import java.time.Instant; +import java.time.Period; +import java.util.concurrent.TimeUnit; + +/** + * Cleanup routine to get rid of old scam history entries in the {@link ScamHistoryStore}. + */ +public final class ScamHistoryPurgeRoutine implements Routine { + private final ScamHistoryStore scamHistoryStore; + private static final Period DELETE_SCAM_RECORDS_AFTER = Period.ofWeeks(2); + + /** + * Creates a new instance. + * + * @param scamHistoryStore containing the scam history to purge + */ + public ScamHistoryPurgeRoutine(@NotNull ScamHistoryStore scamHistoryStore) { + this.scamHistoryStore = scamHistoryStore; + } + + @Override + public @NotNull Schedule createSchedule() { + return new Schedule(ScheduleMode.FIXED_RATE, 0, 1, TimeUnit.DAYS); + } + + @Override + public void runRoutine(@NotNull JDA jda) { + scamHistoryStore.deleteHistoryOlderThan(Instant.now().minus(DELETE_SCAM_RECORDS_AFTER)); + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamHistoryStore.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamHistoryStore.java new file mode 100644 index 0000000000..3154820dc5 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamHistoryStore.java @@ -0,0 +1,164 @@ +package org.togetherjava.tjbot.commands.moderation.scam; + +import net.dv8tion.jda.api.entities.Message; +import org.jetbrains.annotations.NotNull; +import org.jooq.Result; +import org.togetherjava.tjbot.commands.utils.Hashing; +import org.togetherjava.tjbot.db.Database; +import org.togetherjava.tjbot.db.generated.tables.records.ScamHistoryRecord; + +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.time.Instant; +import java.util.Collection; +import java.util.Objects; + +import static org.togetherjava.tjbot.db.generated.tables.ScamHistory.SCAM_HISTORY; + +/** + * Store for history of detected scam messages. Can be used to retrieve information about past + * events and further processing and handling of scam. For example, to delete a group of duplicate + * scam messages after a moderator confirmed that it actually is scam and decided for an action. + *

+ * Scam has to be added to the store using {@link #addScam(Message, boolean)} and can then be used + * to determine {@link #hasRecentScamDuplicate(Message)} or for further processing, such as + * {@link #markScamDuplicatesDeleted(Message)}. + *

+ * Entries are only kept for a certain amount of time and will be purged regularly by + * {@link ScamHistoryPurgeRoutine}. + *

+ * The store persists the actions and is thread safe. + */ +public final class ScamHistoryStore { + private final Database database; + private static final Duration RECENT_SCAM_DURATION = Duration.ofMinutes(15); + private static final String HASH_METHOD = "SHA"; + + /** + * Creates a new instance. + * + * @param database containing the scam history to work with + */ + public ScamHistoryStore(@NotNull Database database) { + this.database = database; + } + + /** + * Adds the given scam message to the store. + * + * @param scam the message to add + * @param isDeleted whether the message is already, or about to get, deleted + */ + public void addScam(@NotNull Message scam, boolean isDeleted) { + Objects.requireNonNull(scam); + + database.write(context -> context.newRecord(SCAM_HISTORY) + .setSentAt(scam.getTimeCreated().toInstant()) + .setGuildId(scam.getGuild().getIdLong()) + .setChannelId(scam.getChannel().getIdLong()) + .setMessageId(scam.getIdLong()) + .setAuthorId(scam.getAuthor().getIdLong()) + .setContentHash(hashMessageContent(scam)) + .setIsDeleted(isDeleted) + .insert()); + } + + /** + * Marks all duplicates to the given scam message (i.e. same guild, author, content, ...) as + * deleted. + * + * @param scam the scam message to mark duplicates for + * @return identifications of all scam messages that have just been marked deleted, which + * previously have not been marked accordingly yet + */ + public @NotNull Collection markScamDuplicatesDeleted( + @NotNull Message scam) { + return markScamDuplicatesDeleted(scam.getGuild().getIdLong(), scam.getAuthor().getIdLong(), + hashMessageContent(scam)); + } + + /** + * Marks all duplicates to the given scam message as deleted. + * + * @param guildId the id of the guild to mark duplicates for + * @param authorId the id of the author to mark duplicates for + * @param contentHash a hash identifying the content of the message to mark duplicates for, as + * determined by {@link #hashMessageContent(Message)} + * @return identifications of all scam messages that have just been marked deleted, which + * previously have not been marked accordingly yet + */ + public @NotNull Collection markScamDuplicatesDeleted(long guildId, + long authorId, @NotNull String contentHash) { + return database.writeAndProvide(context -> { + Result undeletedDuplicates = context.selectFrom(SCAM_HISTORY) + .where(SCAM_HISTORY.GUILD_ID.eq(guildId) + .and(SCAM_HISTORY.AUTHOR_ID.eq(authorId)) + .and(SCAM_HISTORY.CONTENT_HASH.eq(contentHash)) + .and(SCAM_HISTORY.IS_DELETED.isFalse())) + .fetch(); + + undeletedDuplicates + .forEach(undeletedDuplicate -> undeletedDuplicate.setIsDeleted(true).update()); + + return undeletedDuplicates.stream().map(ScamIdentification::ofDatabaseRecord).toList(); + }); + } + + /** + * Whether there are recent (a few minutes) duplicates to the given scam message (i.e. same + * guild, author, content, ...). + * + * @param scam the scam message to look for duplicates + * @return whether there are recent duplicates + */ + public boolean hasRecentScamDuplicate(@NotNull Message scam) { + Instant recentScamThreshold = Instant.now().minus(RECENT_SCAM_DURATION); + + return database.read(context -> context.fetchCount(SCAM_HISTORY, + SCAM_HISTORY.SENT_AT.greaterOrEqual(recentScamThreshold) + .and(SCAM_HISTORY.GUILD_ID.eq(scam.getGuild().getIdLong())) + .and(SCAM_HISTORY.AUTHOR_ID.eq(scam.getAuthor().getIdLong())) + .and(SCAM_HISTORY.CONTENT_HASH.eq(hashMessageContent(scam))))) != 0; + } + + /** + * Deletes all scam records from the history, which have been sent earlier than the given time. + * + * @param olderThan all records older than this will be deleted + */ + public void deleteHistoryOlderThan(Instant olderThan) { + database.write(context -> context.deleteFrom(SCAM_HISTORY) + .where(SCAM_HISTORY.SENT_AT.lessOrEqual(olderThan)) + .execute()); + } + + /** + * Hashes the content of the given message to uniquely identify it. + * + * @param message the message to hash + * @return a text representation of the hash + */ + public static @NotNull String hashMessageContent(@NotNull Message message) { + return Hashing.bytesToHex(Hashing.hash(HASH_METHOD, + message.getContentRaw().getBytes(StandardCharsets.UTF_8))); + } + + /** + * Identification of a scam message, consisting mostly of IDs that uniquely identify it. + * + * @param guildId the id of the guild the message was sent in + * @param channelId the id of the channel the message was sent in + * @param messageId the id of the message itself + * @param authorId the id of the author who sent the message + * @param contentHash the unique hash of the message content + */ + public record ScamIdentification(long guildId, long channelId, long messageId, long authorId, + String contentHash) { + private static ScamIdentification ofDatabaseRecord( + @NotNull ScamHistoryRecord scamHistoryRecord) { + return new ScamIdentification(scamHistoryRecord.getGuildId(), + scamHistoryRecord.getChannelId(), scamHistoryRecord.getMessageId(), + scamHistoryRecord.getAuthorId(), scamHistoryRecord.getContentHash()); + } + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/package-info.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/package-info.java new file mode 100644 index 0000000000..40b4605eff --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/package-info.java @@ -0,0 +1,5 @@ +/** + * This package offers classes dealing with detecting scam messages and taking appropriate action, + * see {@link org.togetherjava.tjbot.commands.moderation.scam.ScamBlocker} as main entry point. + */ +package org.togetherjava.tjbot.commands.moderation.scam; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/utils/Hashing.java b/application/src/main/java/org/togetherjava/tjbot/commands/utils/Hashing.java new file mode 100644 index 0000000000..c82ed92bfe --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/utils/Hashing.java @@ -0,0 +1,62 @@ +package org.togetherjava.tjbot.commands.utils; + +import org.jetbrains.annotations.NotNull; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Objects; + +/** + * Utility for hashing data. + */ +public enum Hashing { + ; + + /** + * All characters available in the hexadecimal-system, as UTF-8 encoded array. + */ + private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.UTF_8); + + /** + * Creates a hexadecimal representation of the given binary data. + * + * @param bytes the binary data to convert + * @return a hexadecimal representation + */ + @SuppressWarnings("MagicNumber") + @NotNull + public static String bytesToHex(byte @NotNull [] bytes) { + Objects.requireNonNull(bytes); + // See https://stackoverflow.com/a/9855338/2411243 + // noinspection MultiplyOrDivideByPowerOfTwo + final byte[] hexChars = new byte[bytes.length * 2]; + // noinspection ArrayLengthInLoopCondition + for (int j = 0; j < bytes.length; j++) { + final int v = bytes[j] & 0xFF; + // noinspection MultiplyOrDivideByPowerOfTwo + hexChars[j * 2] = HEX_ARRAY[v >>> 4]; + // noinspection MultiplyOrDivideByPowerOfTwo + hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; + } + return new String(hexChars, StandardCharsets.UTF_8); + } + + /** + * Hashes the given data using the given method. + * + * @param method the method to use for hashing, must be supported by {@link MessageDigest}, e.g. + * {@code "SHA"} + * @param data the data to hash + * @return the computed hash + */ + public static byte @NotNull [] hash(@NotNull String method, byte @NotNull [] data) { + Objects.requireNonNull(method); + Objects.requireNonNull(data); + try { + return MessageDigest.getInstance(method).digest(data); + } catch (final NoSuchAlgorithmException e) { + throw new IllegalStateException("Hash method must be supported", e); + } + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/config/Config.java b/application/src/main/java/org/togetherjava/tjbot/config/Config.java index 6c93a42a6c..0581e329cf 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/Config.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/Config.java @@ -28,6 +28,7 @@ public final class Config { private final String helpChannelPattern; private final SuggestionsConfig suggestions; private final String quarantinedRolePattern; + private final ScamBlockerConfig scamBlocker; @SuppressWarnings("ConstructorWithTooManyParameters") @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) @@ -43,7 +44,8 @@ private Config(@JsonProperty("token") String token, @JsonProperty("freeCommand") List freeCommand, @JsonProperty("helpChannelPattern") String helpChannelPattern, @JsonProperty("suggestions") SuggestionsConfig suggestions, - @JsonProperty("quarantinedRolePattern") String quarantinedRolePattern) { + @JsonProperty("quarantinedRolePattern") String quarantinedRolePattern, + @JsonProperty("scamBlocker") ScamBlockerConfig scamBlocker) { this.token = token; this.databasePath = databasePath; this.projectWebsite = projectWebsite; @@ -57,6 +59,7 @@ private Config(@JsonProperty("token") String token, this.helpChannelPattern = helpChannelPattern; this.suggestions = suggestions; this.quarantinedRolePattern = quarantinedRolePattern; + this.scamBlocker = scamBlocker; } /** @@ -172,7 +175,7 @@ public String getTagManageRolePattern() { * * @return the channel name pattern */ - public String getHelpChannelPattern() { + public @NotNull String getHelpChannelPattern() { return helpChannelPattern; } @@ -181,7 +184,7 @@ public String getHelpChannelPattern() { * * @return the suggestion system config */ - public SuggestionsConfig getSuggestions() { + public @NotNull SuggestionsConfig getSuggestions() { return suggestions; } @@ -193,4 +196,13 @@ public SuggestionsConfig getSuggestions() { public String getQuarantinedRolePattern() { return quarantinedRolePattern; } + + /** + * Gets the config for the scam blocker system. + * + * @return the scam blocker system config + */ + public @NotNull ScamBlockerConfig getScamBlocker() { + return scamBlocker; + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/config/ScamBlockerConfig.java b/application/src/main/java/org/togetherjava/tjbot/config/ScamBlockerConfig.java new file mode 100644 index 0000000000..1bb8c918bf --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/config/ScamBlockerConfig.java @@ -0,0 +1,126 @@ +package org.togetherjava.tjbot.config; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonRootName; +import org.jetbrains.annotations.NotNull; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +/** + * Configuration for the scam blocker system, see + * {@link org.togetherjava.tjbot.commands.moderation.scam.ScamBlocker}. + */ +@SuppressWarnings("ClassCanBeRecord") +@JsonRootName("scamBlocker") +public final class ScamBlockerConfig { + private final Mode mode; + private final String reportChannelPattern; + private final Set hostWhitelist; + private final Set hostBlacklist; + private final Set suspiciousHostKeywords; + private final int isHostSimilarToKeywordDistanceThreshold; + + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) + private ScamBlockerConfig(@JsonProperty("mode") Mode mode, + @JsonProperty("reportChannelPattern") String reportChannelPattern, + @JsonProperty("hostWhitelist") Set hostWhitelist, + @JsonProperty("hostBlacklist") Set hostBlacklist, + @JsonProperty("suspiciousHostKeywords") Set suspiciousHostKeywords, + @JsonProperty("isHostSimilarToKeywordDistanceThreshold") int isHostSimilarToKeywordDistanceThreshold) { + this.mode = mode; + this.reportChannelPattern = reportChannelPattern; + this.hostWhitelist = new HashSet<>(hostWhitelist); + this.hostBlacklist = new HashSet<>(hostBlacklist); + this.suspiciousHostKeywords = new HashSet<>(suspiciousHostKeywords); + this.isHostSimilarToKeywordDistanceThreshold = isHostSimilarToKeywordDistanceThreshold; + } + + /** + * Gets the mode of the scam blocker. Controls which actions it takes when detecting scam. + * + * @return the scam blockers mode + */ + public @NotNull Mode getMode() { + return mode; + } + + /** + * Gets the REGEX pattern used to identify the channel that is used to report identified scam + * to. + * + * @return the channel name pattern + */ + public String getReportChannelPattern() { + return reportChannelPattern; + } + + /** + * Gets the set of trusted hosts. Urls using those hosts are not considered scam. + * + * @return the whitelist of hosts + */ + public @NotNull Set getHostWhitelist() { + return Collections.unmodifiableSet(hostWhitelist); + } + + /** + * Gets the set of known scam hosts. Urls using those hosts are considered scam. + * + * @return the blacklist of hosts + */ + public @NotNull Set getHostBlacklist() { + return Collections.unmodifiableSet(hostBlacklist); + } + + /** + * Gets the set of keywords that are considered suspicious if they appear in host names. Urls + * using hosts that have those, or similar, keywords in their name, are considered suspicious. + * + * @return the set of suspicious host keywords + */ + public @NotNull Set getSuspiciousHostKeywords() { + return Collections.unmodifiableSet(suspiciousHostKeywords); + } + + /** + * Gets the threshold used to determine whether a host is similar to a given keyword. If the + * host contains an infix with an edit distance that is below this threshold, they are + * considered similar. + * + * @return the threshold to determine similarity + */ + public int getIsHostSimilarToKeywordDistanceThreshold() { + return isHostSimilarToKeywordDistanceThreshold; + } + + /** + * Mode of a scam blocker. Controls which actions it takes when detecting scam. + */ + public enum Mode { + /** + * The blocker is turned off and will not scan any messages for scam. + */ + OFF, + /** + * The blocker will log any detected scam but will not take action on them. + */ + ONLY_LOG, + /** + * Detected scam will be sent to moderators for review. Any action has to be approved + * explicitly first. + */ + APPROVE_FIRST, + /** + * Detected scam will automatically be deleted. A moderator will be informed for review. + * They can then decide whether the user should be put into quarantine. + */ + AUTO_DELETE_BUT_APPROVE_QUARANTINE, + /** + * The blocker will automatically delete any detected scam and put the user into quarantine. + */ + AUTO_DELETE_AND_QUARANTINE + } +} diff --git a/application/src/main/resources/db/V9__Add_Scam_History.sql b/application/src/main/resources/db/V9__Add_Scam_History.sql new file mode 100644 index 0000000000..d6bff1bdeb --- /dev/null +++ b/application/src/main/resources/db/V9__Add_Scam_History.sql @@ -0,0 +1,11 @@ +CREATE TABLE scam_history +( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + sent_at TIMESTAMP NOT NULL, + guild_id BIGINT NOT NULL, + channel_id BIGINT NOT NULL, + message_id BIGINT NOT NULL, + author_id BIGINT NOT NULL, + content_hash TEXT NOT NULL, + is_deleted BOOLEAN NOT NULL +) \ No newline at end of file From 259f3a66d3e6a5ddb53c281022f733fcd9752cbb Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 11 Mar 2022 12:37:48 +0100 Subject: [PATCH 03/11] false-positive shouldnt be tracked by TODO-tracker --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index cae6b31fe6..8f593d152d 100644 --- a/build.gradle +++ b/build.gradle @@ -41,7 +41,7 @@ subprojects { // sonarlint configuration, not to be confused with sonarqube/sonarcloud. sonarlint { excludes { - // Disables "Track uses of "TODO" tags" rule. + // Disables "Track uses of "TO-DO" tags" rule. message 'java:S1135' // Disables "Regular expressions should not overflow the stack" rule. From d1bd0220f4ae0f7caa27ca634051271a4561356b Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Tue, 15 Mar 2022 08:52:57 +0100 Subject: [PATCH 04/11] CR Tais * collection instead of list * newlines between some lines for readability * removed nameToSlashCommand as extra map --- .../commands/moderation/scam/ScamBlocker.java | 4 ++- .../moderation/scam/ScamDetector.java | 2 ++ .../tjbot/commands/system/BotCore.java | 28 +++++++++---------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java index 507010488c..d112617fde 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java @@ -258,7 +258,7 @@ private Optional getReportChannel(@NotNull Guild guild) { .or(() -> guild.getTextChannels().stream().filter(isReportChannel).findAny()); } - private List createConfirmDialog(@NotNull GuildMessageReceivedEvent event) { + private Collection createConfirmDialog(@NotNull GuildMessageReceivedEvent event) { ComponentIdArguments args = new ComponentIdArguments(mode, event.getGuild().getIdLong(), event.getChannel().getIdLong(), event.getMessageIdLong(), event.getAuthor().getIdLong(), @@ -293,6 +293,7 @@ public void onButtonClick(@NotNull ButtonClickEvent event, @NotNull List args.messageId, args.contentHash, args.guildId, args.authorId); return; } + Guild guild = event.getJDA().getGuildById(args.guildId); if (guild == null) { logger.debug( @@ -345,6 +346,7 @@ public void onButtonClick(@NotNull ButtonClickEvent event, @NotNull List private record ComponentIdArguments(@NotNull ScamBlockerConfig.Mode mode, long guildId, long channelId, long messageId, long authorId, String contentHash) { + static ComponentIdArguments fromList(@NotNull List args) { ScamBlockerConfig.Mode mode = ScamBlockerConfig.Mode.valueOf(args.get(0)); long guildId = Long.parseLong(args.get(1)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetector.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetector.java index 1acb1485cc..bff85980e5 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetector.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetector.java @@ -33,6 +33,7 @@ private void analyzeToken(@NotNull String token, @NotNull AnalyseResults results if ("@everyone".equalsIgnoreCase(token)) { results.pingsEveryone = true; } + if ("nitro".equalsIgnoreCase(token)) { results.containsNitroKeyword = true; } @@ -50,6 +51,7 @@ private void analyzeUrl(@NotNull String url, @NotNull AnalyseResults results) { // Invalid urls are not scam return; } + if (host == null) { return; } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index 1473d2b5fb..11705448d8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -56,7 +56,6 @@ public final class BotCore extends ListenerAdapter implements SlashCommandProvid Executors.newScheduledThreadPool(5); private final Config config; private final Map nameToInteractor; - private final Map nameToSlashCommand; private final ComponentIdParser componentIdParser; private final ComponentIdStore componentIdStore; private final Map channelNameToMessageReceiver = new HashMap<>(); @@ -105,26 +104,18 @@ public BotCore(@NotNull JDA jda, @NotNull Database database, @NotNull Config con } }); - // User Interactors + // User Interactors (e.g. slash commands) nameToInteractor = features.stream() .filter(UserInteractor.class::isInstance) .map(UserInteractor.class::cast) .collect(Collectors.toMap(UserInteractor::getName, Function.identity())); - // Slash Commands - nameToSlashCommand = features.stream() - .filter(SlashCommand.class::isInstance) - .map(SlashCommand.class::cast) - .collect(Collectors.toMap(SlashCommand::getName, Function.identity())); - // Reload Command if (nameToInteractor.containsKey(RELOAD_COMMAND)) { throw new IllegalStateException( "The 'reload' command is a special reserved command that must not be used by other user interactors"); } - SlashCommand reloadCommand = new ReloadCommand(this); - nameToInteractor.put(RELOAD_COMMAND, reloadCommand); - nameToSlashCommand.put(RELOAD_COMMAND, reloadCommand); + nameToInteractor.put(RELOAD_COMMAND, new ReloadCommand(this)); // Component Id Store componentIdStore = new ComponentIdStore(database); @@ -139,18 +130,24 @@ public BotCore(@NotNull JDA jda, @NotNull Database database, @NotNull Config con }))); if (logger.isInfoEnabled()) { - logger.info("Available commands: {}", nameToSlashCommand.keySet()); + logger.info("Available user interactors: {}", nameToInteractor.keySet()); } } @Override public @NotNull Collection getSlashCommands() { - return Collections.unmodifiableCollection(nameToSlashCommand.values()); + return nameToInteractor.values() + .stream() + .filter(SlashCommand.class::isInstance) + .map(SlashCommand.class::cast) + .toList(); } @Override public @NotNull Optional getSlashCommand(@NotNull String name) { - return Optional.ofNullable(nameToSlashCommand.get(name)); + return Optional.ofNullable(nameToInteractor.get(name)) + .filter(SlashCommand.class::isInstance) + .map(SlashCommand.class::cast); } @Override @@ -287,7 +284,8 @@ private void forwardComponentCommand(@NotNull T * @throws NullPointerException if the command with the given name was not registered */ private @NotNull SlashCommand requireSlashCommand(@NotNull String name) { - return Objects.requireNonNull(nameToSlashCommand.get(name)); + return getSlashCommand(name).orElseThrow( + () -> new NullPointerException("There is no slash command with name " + name)); } /** From 44fb7778606969169c6e28172bc28f27476124eb Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Tue, 15 Mar 2022 09:39:49 +0100 Subject: [PATCH 05/11] unit tests for ScamDetector * also added junit parametrized test dependency * removed a useless gradle dep that was duplicated --- application/build.gradle | 2 +- .../moderation/scam/ScamDetectorTest.java | 144 ++++++++++++++++++ 2 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 application/src/test/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetectorTest.java diff --git a/application/build.gradle b/application/build.gradle index 28e4136531..b9b1f3e3aa 100644 --- a/application/build.gradle +++ b/application/build.gradle @@ -60,8 +60,8 @@ dependencies { implementation 'com.github.ben-manes.caffeine:caffeine:3.0.4' testImplementation 'org.mockito:mockito-core:4.0.0' - testRuntimeOnly 'org.mockito:mockito-core:4.0.0' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' + testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' } diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetectorTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetectorTest.java new file mode 100644 index 0000000000..a5ab9830a5 --- /dev/null +++ b/application/src/test/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetectorTest.java @@ -0,0 +1,144 @@ +package org.togetherjava.tjbot.commands.moderation.scam; + +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.config.ScamBlockerConfig; + +import java.util.List; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +final class ScamDetectorTest { + private ScamDetector scamDetector; + + @BeforeEach + void setUp() { + Config config = mock(Config.class); + ScamBlockerConfig scamConfig = mock(ScamBlockerConfig.class); + when(config.getScamBlocker()).thenReturn(scamConfig); + + when(scamConfig.getHostWhitelist()).thenReturn(Set.of("discord.com", "discord.gg", + "discord.media", "discordapp.com", "discordapp.net", "discordstatus.com")); + when(scamConfig.getHostBlacklist()).thenReturn(Set.of("bit.ly")); + when(scamConfig.getSuspiciousHostKeywords()) + .thenReturn(Set.of("discord", "nitro", "premium")); + when(scamConfig.getIsHostSimilarToKeywordDistanceThreshold()).thenReturn(2); + + scamDetector = new ScamDetector(config); + } + + @ParameterizedTest + @MethodSource("provideRealScamMessages") + @DisplayName("Can detect real scam messages") + void detectsRealScam(@NotNull String scamMessage) { + // GIVEN a real scam message + // WHEN analyzing it + boolean isScamResult = scamDetector.isScam(scamMessage); + + // THEN flags it as scam + assertTrue(isScamResult); + } + + @Test + @DisplayName("Can detect messages that contain blacklisted websites as scam") + void detectsBlacklistedWebsite() { + // GIVEN a message with a link to a blacklisted website + String scamMessage = "Checkout https://bit.ly/3IhcLiO to get your free nitro !"; + + // WHEN analyzing it + boolean isScamResult = scamDetector.isScam(scamMessage); + + // THEN flags it as scam + assertTrue(isScamResult); + } + + @Test + @DisplayName("Can detect messages that contain whitelisted websites and does not flag them as scam") + void detectsWhitelistedWebsite() { + // GIVEN a message with a link to a whitelisted website + String harmlessMessage = + "Checkout https://discord.com/nitro to get your nitro - but not for free."; + + // WHEN analyzing it + boolean isScamResult = scamDetector.isScam(harmlessMessage); + + // THEN flags it as harmless + assertFalse(isScamResult); + } + + @Test + @DisplayName("Can detect messages that contain links to suspicious websites and flags them as scam") + void detectsSuspiciousWebsites() { + // GIVEN a message with a link to a suspicious website + String scamMessage = "Checkout https://disc0rdS.com/n1tro to get your nitro for free."; + + // WHEN analyzing it + boolean isScamResult = scamDetector.isScam(scamMessage); + + // THEN flags it as scam + assertTrue(isScamResult); + } + + @Test + @DisplayName("Messages that contain links to websites that are not similar enough to suspicious keywords are not flagged as scam") + void websitesWithTooManyDifferencesAreNotSuspicious() { + // GIVEN a message with a link to a website that is not similar enough to a suspicious + // keyword + String notSimilarEnoughMessage = + "Checkout https://dI5c0ndS.com/n1rt0 to get your nitro for free."; + + // WHEN analyzing it + boolean isScamResult = scamDetector.isScam(notSimilarEnoughMessage); + + // THEN flags it as harmless + assertFalse(isScamResult); + } + + private static @NotNull List provideRealScamMessages() { + return List.of(""" + 🤩bro steam gived nitro - https://nitro-ds.online/LfgUfMzqYyx12""", + """ + @everyone, Free subscription for 3 months DISCORD NITRO - https://e-giftpremium.com/x12""", + """ + @everyone + Discord Nitro distribution from STEAM. + Get 3 month of Discord Nitro. Offer ends January 28, 2022 at 11am EDT. Customize your profile, share your screen in HD, update your emoji and more! + https://dlscrod-game.ru/promotionx12""", + """ + @everyone + Gifts for the new year, nitro for 3 months: https://discofdapp.com/newyearsx12""", + """ + @everyone yo , I got some nitro left over here https://steelsseriesnitros.com/billing/promotions/vh98rpaEJZnha5x37agpmOz3x12""", + """ + @everyone + :video_game: • Get Discord Nitro for Free from Steam Store + Free 3 months Discord Nitro + :clock630: • Personalize your profile, screen share in HD, upgrade your emojis, and more. + :gem: • Click to get Nitro: https://discoord-nittro.com/welcomex12 + :Works only with prime go or rust or pubg""", + """ + @everyone, Check this lol, there nitro is handed out for free, take it until everything is sorted out https://dicsord-present.ru/airdropx12""", + """ + @everyone + • Get Discord Nitro for Free from Steam Store + Free 3 months Discord Nitro + • The offer is valid until at 6:00PM on November 30, 2021. Personalize your profile, screen share in HD, upgrade your emojis, and more. + • Click to get Nitro: https://dliscord.shop/welcomex12""", + """ + airdrop discord nitro by steam, take it https://bit.ly/30RzoKx""", + """ + Steam is giving away free discord nitro, have time to pick up at my link https://bit.ly/3nlzmUa before the action is over.""", + """ + @everyone, take nitro faster, it's already running out + https://discordu.gift/u1CHEX2sjpDuR3T5"""); + } +} From 3445e33bc53b151ad46ac63af2d1b1c4aa3fcd4d Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Tue, 15 Mar 2022 09:42:50 +0100 Subject: [PATCH 06/11] Removed obsolete channel lookup (CR Tais) --- .../tjbot/commands/moderation/scam/ScamBlocker.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java index d112617fde..f83021c0ba 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java @@ -250,12 +250,7 @@ If you think this was a mistake (for example, your account was hacked, but you g } private Optional getReportChannel(@NotNull Guild guild) { - // Check cache first, then get full list - return guild.getTextChannelCache() - .stream() - .filter(isReportChannel) - .findAny() - .or(() -> guild.getTextChannels().stream().filter(isReportChannel).findAny()); + return guild.getTextChannelCache().stream().filter(isReportChannel).findAny(); } private Collection createConfirmDialog(@NotNull GuildMessageReceivedEvent event) { From 8f77e1282b66c9e2b45f996ce6c04b9aca799a58 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Tue, 15 Mar 2022 09:46:37 +0100 Subject: [PATCH 07/11] Using single nullable ActionRow instead of list (CR Tais) --- .../commands/moderation/scam/ScamBlocker.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java index f83021c0ba..893aa98c4a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java @@ -13,6 +13,7 @@ import net.dv8tion.jda.api.interactions.components.ButtonStyle; import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.MessageReceiverAdapter; @@ -169,7 +170,7 @@ private void takeActionAutoDeleteAndQuarantine(@NotNull GuildMessageReceivedEven deleteMessage(event); quarantineAuthor(event); dmUser(event); - reportScamMessage(event, "Detected and handled scam", List.of()); + reportScamMessage(event, "Detected and handled scam", null); } private void addScamToHistory(@NotNull GuildMessageReceivedEvent event) { @@ -205,7 +206,7 @@ private void quarantineAuthor(@NotNull Guild guild, @NotNull Member author, } private void reportScamMessage(@NotNull GuildMessageReceivedEvent event, - @NotNull String reportTitle, @NotNull Collection actionRows) { + @NotNull String reportTitle, @Nullable ActionRow confirmDialog) { Guild guild = event.getGuild(); Optional reportChannel = getReportChannel(guild); if (reportChannel.isEmpty()) { @@ -224,7 +225,8 @@ private void reportScamMessage(@NotNull GuildMessageReceivedEvent event, .setColor(AMBIENT_COLOR) .setFooter(author.getId()) .build(); - Message message = new MessageBuilder().setEmbeds(embed).setActionRows(actionRows).build(); + Message message = + new MessageBuilder().setEmbeds(embed).setActionRows(confirmDialog).build(); reportChannel.orElseThrow().sendMessage(message).queue(); } @@ -249,19 +251,18 @@ If you think this was a mistake (for example, your account was hacked, but you g .queue(); } - private Optional getReportChannel(@NotNull Guild guild) { + private @NotNull Optional getReportChannel(@NotNull Guild guild) { return guild.getTextChannelCache().stream().filter(isReportChannel).findAny(); } - private Collection createConfirmDialog(@NotNull GuildMessageReceivedEvent event) { + private @NotNull ActionRow createConfirmDialog(@NotNull GuildMessageReceivedEvent event) { ComponentIdArguments args = new ComponentIdArguments(mode, event.getGuild().getIdLong(), event.getChannel().getIdLong(), event.getMessageIdLong(), event.getAuthor().getIdLong(), ScamHistoryStore.hashMessageContent(event.getMessage())); - return List - .of(ActionRow.of(Button.of(ButtonStyle.SUCCESS, generateComponentId(args), "Yes"), - Button.of(ButtonStyle.DANGER, generateComponentId(args), "No"))); + return ActionRow.of(Button.of(ButtonStyle.SUCCESS, generateComponentId(args), "Yes"), + Button.of(ButtonStyle.DANGER, generateComponentId(args), "No")); } private @NotNull String generateComponentId(@NotNull ComponentIdArguments args) { @@ -340,9 +341,9 @@ public void onButtonClick(@NotNull ButtonClickEvent event, @NotNull List private record ComponentIdArguments(@NotNull ScamBlockerConfig.Mode mode, long guildId, - long channelId, long messageId, long authorId, String contentHash) { + long channelId, long messageId, long authorId, @NotNull String contentHash) { - static ComponentIdArguments fromList(@NotNull List args) { + static @NotNull ComponentIdArguments fromList(@NotNull List args) { ScamBlockerConfig.Mode mode = ScamBlockerConfig.Mode.valueOf(args.get(0)); long guildId = Long.parseLong(args.get(1)); long channelId = Long.parseLong(args.get(2)); @@ -353,6 +354,7 @@ static ComponentIdArguments fromList(@NotNull List args) { contentHash); } + @NotNull List toList() { return List.of(mode.name(), Long.toString(guildId), Long.toString(channelId), Long.toString(messageId), Long.toString(authorId), contentHash); From 0cd69057eaa4a4b9f44b8573b803ce203b4000cd Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Tue, 29 Mar 2022 08:31:48 +0200 Subject: [PATCH 08/11] Using JDAs Error Handler (CR Tais) --- .../commands/moderation/scam/ScamBlocker.java | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java index 893aa98c4a..9f93697e1c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java @@ -7,7 +7,7 @@ import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; -import net.dv8tion.jda.api.exceptions.ErrorResponseException; +import net.dv8tion.jda.api.exceptions.ErrorHandler; import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.Button; import net.dv8tion.jda.api.interactions.components.ButtonStyle; @@ -319,21 +319,14 @@ public void onButtonClick(@NotNull ButtonClickEvent event, @NotNull List }); }; - Consumer onRetrieveAuthorFailure = failure -> { - if (failure instanceof ErrorResponseException errorResponseException) { - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { - logger.debug("Attempted to handle scam, but user '{}' does not exist anymore.", - args.authorId); - return; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MEMBER) { - logger.debug( - "Attempted to handle scam, but user '{}' is not a member of the guild anymore.", - args.authorId); - } - } - }; + Consumer onRetrieveAuthorFailure = new ErrorHandler() + .handle(ErrorResponse.UNKNOWN_USER, + failure -> logger.debug( + "Attempted to handle scam, but user '{}' does not exist anymore.", + args.authorId)) + .handle(ErrorResponse.UNKNOWN_MEMBER, failure -> logger.debug( + "Attempted to handle scam, but user '{}' is not a member of the guild anymore.", + args.authorId)); guild.retrieveMemberById(args.authorId) .queue(onRetrieveAuthorSuccess, onRetrieveAuthorFailure); From 2d9bac706f622ac202697f9d0f90dc1bf03b16c2 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Tue, 29 Mar 2022 08:56:43 +0200 Subject: [PATCH 09/11] missing Javadoc (CR Tais) --- .../moderation/scam/ScamDetector.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetector.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetector.java index bff85980e5..15d32d15a9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetector.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamDetector.java @@ -8,14 +8,31 @@ import java.net.URI; import java.util.regex.Pattern; -public class ScamDetector { +/** + * Detects whether a text message classifies as scam or not, using certain heuristics. + * + * Highly configurable, using {@link ScamBlockerConfig}. Main method to use is + * {@link #isScam(CharSequence)}. + */ +public final class ScamDetector { private static final Pattern TOKENIZER = Pattern.compile("[\\s,]"); private final ScamBlockerConfig config; + /** + * Creates a new instance with the given configuration + * + * @param config the scam blocker config to use + */ public ScamDetector(@NotNull Config config) { this.config = config.getScamBlocker(); } + /** + * Detects whether the given message classifies as scam or not, using certain heuristics. + * + * @param message the message to analyze + * @return Whether the message classifies as scam + */ public boolean isScam(@NotNull CharSequence message) { AnalyseResults results = new AnalyseResults(); TOKENIZER.splitAsStream(message).forEach(token -> analyzeToken(token, results)); From db95417a220fe0353fbe90802d13a0b11ee451de Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Tue, 29 Mar 2022 10:39:52 +0200 Subject: [PATCH 10/11] Using proper button factories (CR Tais) --- .../tjbot/commands/mathcommands/TeXCommand.java | 8 ++++---- .../tjbot/commands/moderation/scam/ScamBlocker.java | 4 ++-- .../togetherjava/tjbot/commands/system/ReloadCommand.java | 4 ++-- .../togetherjava/tjbot/jda/ButtonClickEventBuilder.java | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java index 6ca3a797b8..3c936fb4de 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java @@ -3,8 +3,7 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.interactions.commands.OptionType; -import net.dv8tion.jda.api.interactions.components.buttons.Button; -import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; +import net.dv8tion.jda.api.interactions.components.Button; import org.jetbrains.annotations.NotNull; import org.scilab.forge.jlatexmath.ParseException; import org.scilab.forge.jlatexmath.TeXConstants; @@ -15,7 +14,8 @@ import org.togetherjava.tjbot.commands.SlashCommandVisibility; import javax.imageio.ImageIO; -import java.awt.*; +import java.awt.Color; +import java.awt.Image; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -88,7 +88,7 @@ public void onSlashCommand(@NotNull final SlashCommandInteractionEvent event) { } event.getHook() .editOriginal(renderedTextImageStream.toByteArray(), "tex.png") - .setActionRow(Button.of(ButtonStyle.DANGER, generateComponentId(userID), "Delete")) + .setActionRow(Button.danger(generateComponentId(userID), "Delete")) .queue(); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java index 9f93697e1c..1aa3705db0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java @@ -261,8 +261,8 @@ If you think this was a mistake (for example, your account was hacked, but you g event.getAuthor().getIdLong(), ScamHistoryStore.hashMessageContent(event.getMessage())); - return ActionRow.of(Button.of(ButtonStyle.SUCCESS, generateComponentId(args), "Yes"), - Button.of(ButtonStyle.DANGER, generateComponentId(args), "No")); + return ActionRow.of(Button.success(generateComponentId(args), "Yes"), + Button.danger(generateComponentId(args), "No")); } private @NotNull String generateComponentId(@NotNull ComponentIdArguments args) { diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java index cd2c83a223..2f60fb192e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java @@ -70,8 +70,8 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { event.reply( "Are you sure? You can only reload commands a few times each day, so do not overdo this.") .addActionRow( - Button.of(ButtonStyle.SUCCESS, generateComponentId(member.getId()), "Yes"), - Button.of(ButtonStyle.DANGER, generateComponentId(member.getId()), "No")) + Button.success(generateComponentId(member.getId()), "Yes"), + Button.danger(generateComponentId(member.getId()), "No")) .queue(); } diff --git a/application/src/test/java/org/togetherjava/tjbot/jda/ButtonClickEventBuilder.java b/application/src/test/java/org/togetherjava/tjbot/jda/ButtonClickEventBuilder.java index 6b9192566b..f68d13fe08 100644 --- a/application/src/test/java/org/togetherjava/tjbot/jda/ButtonClickEventBuilder.java +++ b/application/src/test/java/org/togetherjava/tjbot/jda/ButtonClickEventBuilder.java @@ -46,11 +46,11 @@ * {@code * // Default message with a delete button * jdaTester.createButtonClickEvent() - * .setActionRows(ActionRow.of(Button.of(ButtonStyle.DANGER, "1", "Delete")) + * .setActionRows(ActionRow.of(Button.danger("1", "Delete")) * .buildWithSingleButton(); * * // More complex message with a user who clicked the button that is not the message author and multiple buttons - * Button clickedButton = Button.of(ButtonStyle.PRIMARY, "1", "Next"); + * Button clickedButton = Button.primary("1", "Next"); * jdaTester.createButtonClickEvent() * .setMessage(new MessageBuilder() * .setContent("See the following entry") @@ -61,7 +61,7 @@ * .build()) * .setUserWhoClicked(jdaTester.createMemberSpy(5)) * .setActionRows( - * ActionRow.of(Button.of(ButtonStyle.PRIMARY, "1", "Previous"), + * ActionRow.of(Button.primary("1", "Previous"), * clickedButton) * .build(clickedButton); * } From 4134e416ecd8974e3050a741f47891382fa8d66f Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Tue, 29 Mar 2022 12:13:26 +0200 Subject: [PATCH 11/11] Fixed issues after rebase --- .../tjbot/commands/SlashCommand.java | 30 ++++++------ .../tjbot/commands/UserInteractor.java | 25 +++++----- .../commands/mathcommands/TeXCommand.java | 2 +- .../commands/moderation/scam/ScamBlocker.java | 47 ++++++++++--------- .../tjbot/commands/system/ReloadCommand.java | 3 +- 5 files changed, 55 insertions(+), 52 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java index 1e85a892a6..20f5c936b7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java @@ -1,12 +1,14 @@ package org.togetherjava.tjbot.commands; import net.dv8tion.jda.api.entities.Emoji; -import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; -import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; +import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent; import net.dv8tion.jda.api.interactions.commands.build.CommandData; -import net.dv8tion.jda.api.interactions.components.ButtonStyle; +import net.dv8tion.jda.api.interactions.commands.build.Commands; +import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; +import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.componentids.ComponentId; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; @@ -28,9 +30,9 @@ * is then to be returned by {@link #getData()} where the system will then pick it up from. *

* After registration, the system will notify a command whenever one of its corresponding slash - * commands ({@link #onSlashCommand(SlashCommandEvent)}), buttons - * ({@link #onButtonClick(ButtonClickEvent, List)}) or menus - * ({@link #onSelectionMenu(SelectionMenuEvent, List)}) have been triggered. + * commands ({@link #onSlashCommand(SlashCommandInteractionEvent)}), buttons + * ({@link #onButtonClick(ButtonInteractionEvent, List)}) or menus + * ({@link #onSelectionMenu(SelectMenuInteractionEvent, List)}) have been triggered. *

*

* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}. @@ -40,7 +42,7 @@ public interface SlashCommand extends UserInteractor { /** * Gets the description of the command. *

- * Requirements for this are documented in {@link CommandData#CommandData(String, String)}. + * Requirements for this are documented in {@link Commands#slash(String, String)}. *

*

* After registration of the command, the description must not change anymore. @@ -76,7 +78,7 @@ public interface SlashCommand extends UserInteractor { * @return the command data of this command */ @NotNull - CommandData getData(); + SlashCommandData getData(); /** * Triggered by the core system when a slash command corresponding to this implementation (based @@ -91,8 +93,8 @@ public interface SlashCommand extends UserInteractor { *

* Buttons or menus have to be created with a component ID (see * {@link ComponentInteraction#getComponentId()}, - * {@link net.dv8tion.jda.api.interactions.components.Button#of(ButtonStyle, String, Emoji)}) in - * a very specific format, otherwise the core system will fail to identify the command that + * {@link net.dv8tion.jda.api.interactions.components.buttons.Button#of(ButtonStyle, String, Emoji)}) + * in a very specific format, otherwise the core system will fail to identify the command that * corresponded to the button or menu click event and is unable to route it back. *

* The component ID has to be a UUID-string (see {@link java.util.UUID}), which is associated to @@ -102,8 +104,8 @@ public interface SlashCommand extends UserInteractor { * given to {@link #acceptComponentIdGenerator(ComponentIdGenerator)} during system setup. The * required {@link ComponentId} instance accepts optional extra arguments, which, if provided, * can be picked up during the corresponding event (see - * {@link #onButtonClick(ButtonClickEvent, List)}, - * {@link #onSelectionMenu(SelectionMenuEvent, List)}). + * {@link #onButtonClick(ButtonInteractionEvent, List)}, + * {@link #onSelectionMenu(SelectMenuInteractionEvent, List)}). *

* Alternatively, if {@link SlashCommandAdapter} has been extended, it also offers a handy * {@link SlashCommandAdapter#generateComponentId(String...)} method to ease the flow. @@ -116,5 +118,5 @@ public interface SlashCommand extends UserInteractor { * * @param event the event that triggered this */ - void onSlashCommand(@NotNull SlashCommandEvent event); + void onSlashCommand(@NotNull SlashCommandInteractionEvent event); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java index 908fd3ac1a..4ad0ed0da2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java @@ -1,9 +1,8 @@ package org.togetherjava.tjbot.commands; -import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; -import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.interactions.commands.build.CommandData; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; +import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent; import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator; @@ -20,7 +19,8 @@ public interface UserInteractor extends Feature { /** * Gets the name of the interactor. *

- * Requirements for this are documented in {@link CommandData#CommandData(String, String)}. + * Requirements for this are documented in + * {@link net.dv8tion.jda.api.interactions.commands.build.Commands#slash(String, String)}. *

*

* After registration of the interactor, the name must not change anymore. @@ -46,10 +46,10 @@ public interface UserInteractor extends Feature { * * @param event the event that triggered this * @param args the arguments transported with the button, see - * {@link SlashCommand#onSlashCommand(SlashCommandEvent)} for details on how these are - * created + * {@link SlashCommand#onSlashCommand(SlashCommandInteractionEvent)} for details on how + * these are created */ - void onButtonClick(@NotNull ButtonClickEvent event, @NotNull List args); + void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List args); /** * Triggered by the core system when a selection menu corresponding to this implementation @@ -67,16 +67,17 @@ public interface UserInteractor extends Feature { * * @param event the event that triggered this * @param args the arguments transported with the selection menu, see - * {@link SlashCommand#onSlashCommand(SlashCommandEvent)} for details on how these are - * created + * {@link SlashCommand#onSlashCommand(SlashCommandInteractionEvent)} for details on how + * these are created */ - void onSelectionMenu(@NotNull SelectionMenuEvent event, @NotNull List args); + void onSelectionMenu(@NotNull SelectMenuInteractionEvent event, @NotNull List args); /** * Triggered by the core system during its setup phase. It will provide the interactor a * component id generator through this method, which can be used to generate component ids, as * used for button or selection menus. See - * {@link SlashCommand#onSlashCommand(SlashCommandEvent)} for details on how to use this. + * {@link SlashCommand#onSlashCommand(SlashCommandInteractionEvent)} for details on how to use + * this. * * @param generator the provided component id generator */ diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java index 3c936fb4de..1f5be8d3ea 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/mathcommands/TeXCommand.java @@ -3,7 +3,7 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.interactions.commands.OptionType; -import net.dv8tion.jda.api.interactions.components.Button; +import net.dv8tion.jda.api.interactions.components.buttons.Button; import org.jetbrains.annotations.NotNull; import org.scilab.forge.jlatexmath.ParseException; import org.scilab.forge.jlatexmath.TeXConstants; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java index 1aa3705db0..e4240cedb2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/scam/ScamBlocker.java @@ -4,13 +4,13 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.MessageBuilder; import net.dv8tion.jda.api.entities.*; -import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; -import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; -import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; +import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.exceptions.ErrorHandler; import net.dv8tion.jda.api.interactions.components.ActionRow; -import net.dv8tion.jda.api.interactions.components.Button; -import net.dv8tion.jda.api.interactions.components.ButtonStyle; +import net.dv8tion.jda.api.interactions.components.buttons.Button; +import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -89,7 +89,8 @@ public ScamBlocker(@NotNull ModerationActionsStore actionsStore, } @Override - public void onSelectionMenu(@NotNull SelectionMenuEvent event, @NotNull List args) { + public void onSelectionMenu(@NotNull SelectMenuInteractionEvent event, + @NotNull List args) { throw new UnsupportedOperationException("Not used"); } @@ -99,7 +100,7 @@ public void acceptComponentIdGenerator(@NotNull ComponentIdGenerator generator) } @Override - public void onMessageReceived(@NotNull GuildMessageReceivedEvent event) { + public void onMessageReceived(@NotNull MessageReceivedEvent event) { if (event.getAuthor().isBot() || event.isWebhookMessage()) { return; } @@ -122,7 +123,7 @@ public void onMessageReceived(@NotNull GuildMessageReceivedEvent event) { takeAction(event); } - private void takeActionWasAlreadyReported(@NotNull GuildMessageReceivedEvent event) { + private void takeActionWasAlreadyReported(@NotNull MessageReceivedEvent event) { // The user recently send the same scam already, and that was already reported and handled addScamToHistory(event); @@ -132,7 +133,7 @@ private void takeActionWasAlreadyReported(@NotNull GuildMessageReceivedEvent eve } } - private void takeAction(@NotNull GuildMessageReceivedEvent event) { + private void takeAction(@NotNull MessageReceivedEvent event) { switch (mode) { case OFF -> throw new AssertionError( "The OFF-mode should be detected earlier already to prevent expensive computation"); @@ -145,26 +146,25 @@ private void takeAction(@NotNull GuildMessageReceivedEvent event) { } } - private void takeActionLogOnly(@NotNull GuildMessageReceivedEvent event) { + private void takeActionLogOnly(@NotNull MessageReceivedEvent event) { addScamToHistory(event); logScamMessage(event); } - private void takeActionApproveFirst(@NotNull GuildMessageReceivedEvent event) { + private void takeActionApproveFirst(@NotNull MessageReceivedEvent event) { addScamToHistory(event); logScamMessage(event); reportScamMessage(event, "Is this scam?", createConfirmDialog(event)); } - private void takeActionAutoDeleteButApproveQuarantine( - @NotNull GuildMessageReceivedEvent event) { + private void takeActionAutoDeleteButApproveQuarantine(@NotNull MessageReceivedEvent event) { addScamToHistory(event); logScamMessage(event); deleteMessage(event); reportScamMessage(event, "Is this scam? (already deleted)", createConfirmDialog(event)); } - private void takeActionAutoDeleteAndQuarantine(@NotNull GuildMessageReceivedEvent event) { + private void takeActionAutoDeleteAndQuarantine(@NotNull MessageReceivedEvent event) { addScamToHistory(event); logScamMessage(event); deleteMessage(event); @@ -173,21 +173,21 @@ private void takeActionAutoDeleteAndQuarantine(@NotNull GuildMessageReceivedEven reportScamMessage(event, "Detected and handled scam", null); } - private void addScamToHistory(@NotNull GuildMessageReceivedEvent event) { + private void addScamToHistory(@NotNull MessageReceivedEvent event) { scamHistoryStore.addScam(event.getMessage(), MODES_WITH_IMMEDIATE_DELETION.contains(mode)); } - private void logScamMessage(@NotNull GuildMessageReceivedEvent event) { + private void logScamMessage(@NotNull MessageReceivedEvent event) { logger.warn("Detected a scam message ('{}') from user '{}' in channel '{}' of guild '{}'.", event.getMessageId(), event.getAuthor().getId(), event.getChannel().getId(), event.getGuild().getId()); } - private void deleteMessage(@NotNull GuildMessageReceivedEvent event) { + private void deleteMessage(@NotNull MessageReceivedEvent event) { event.getMessage().delete().queue(); } - private void quarantineAuthor(@NotNull GuildMessageReceivedEvent event) { + private void quarantineAuthor(@NotNull MessageReceivedEvent event) { quarantineAuthor(event.getGuild(), event.getMember(), event.getJDA().getSelfUser()); } @@ -205,8 +205,8 @@ private void quarantineAuthor(@NotNull Guild guild, @NotNull Member author, .queue(); } - private void reportScamMessage(@NotNull GuildMessageReceivedEvent event, - @NotNull String reportTitle, @Nullable ActionRow confirmDialog) { + private void reportScamMessage(@NotNull MessageReceivedEvent event, @NotNull String reportTitle, + @Nullable ActionRow confirmDialog) { Guild guild = event.getGuild(); Optional reportChannel = getReportChannel(guild); if (reportChannel.isEmpty()) { @@ -231,7 +231,7 @@ private void reportScamMessage(@NotNull GuildMessageReceivedEvent event, reportChannel.orElseThrow().sendMessage(message).queue(); } - private void dmUser(@NotNull GuildMessageReceivedEvent event) { + private void dmUser(@NotNull MessageReceivedEvent event) { dmUser(event.getGuild(), event.getAuthor().getIdLong(), event.getJDA()); } @@ -255,7 +255,7 @@ If you think this was a mistake (for example, your account was hacked, but you g return guild.getTextChannelCache().stream().filter(isReportChannel).findAny(); } - private @NotNull ActionRow createConfirmDialog(@NotNull GuildMessageReceivedEvent event) { + private @NotNull ActionRow createConfirmDialog(@NotNull MessageReceivedEvent event) { ComponentIdArguments args = new ComponentIdArguments(mode, event.getGuild().getIdLong(), event.getChannel().getIdLong(), event.getMessageIdLong(), event.getAuthor().getIdLong(), @@ -271,7 +271,8 @@ If you think this was a mistake (for example, your account was hacked, but you g } @Override - public void onButtonClick(@NotNull ButtonClickEvent event, @NotNull List argsRaw) { + public void onButtonClick(@NotNull ButtonInteractionEvent event, + @NotNull List argsRaw) { ComponentIdArguments args = ComponentIdArguments.fromList(argsRaw); if (event.getMember().getRoles().stream().map(Role::getName).noneMatch(hasRequiredRole)) { event.reply( diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java index 2f60fb192e..256faa7d5e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/ReloadCommand.java @@ -69,8 +69,7 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { event.reply( "Are you sure? You can only reload commands a few times each day, so do not overdo this.") - .addActionRow( - Button.success(generateComponentId(member.getId()), "Yes"), + .addActionRow(Button.success(generateComponentId(member.getId()), "Yes"), Button.danger(generateComponentId(member.getId()), "No")) .queue(); }