-
-
Notifications
You must be signed in to change notification settings - Fork 104
Add Message and Event receivers #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0df3163
+ Feature, EventReceiver and MessageReceiver
Zabuzard a520488
Integration of features
Zabuzard a6a249f
Removed onReady
Zabuzard a384cf8
Fixed some typos in user-facing strings (missing spaces)
Zabuzard 1a88fab
Suppressed false-positive SonarLint warning
Zabuzard 0608574
Added some missing curlies that made SonarLint angry
Zabuzard 9f4f772
onMessageSent -> onMessageReceived, minor improvement on adapter
Zabuzard 8d1c998
MessageReceivers now subscribe to channels by name patterns
Zabuzard 546b5d1
Minor changes from CR
Zabuzard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 0 additions & 78 deletions
78
application/src/main/java/org/togetherjava/tjbot/commands/Commands.java
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
application/src/main/java/org/togetherjava/tjbot/commands/EventReceiver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package org.togetherjava.tjbot.commands; | ||
|
|
||
| import net.dv8tion.jda.api.hooks.EventListener; | ||
|
|
||
| /** | ||
| * Receives all incoming Discord events, unfiltered. A list of all available event types can be | ||
| * found in {@link net.dv8tion.jda.api.hooks.ListenerAdapter}. | ||
| * | ||
| * If possible, prefer one of the more concrete features instead, such as {@link SlashCommand} or | ||
| * {@link MessageReceiver}. Take care to not accidentally implement both, this and one of the other | ||
| * {@link Feature}s, as this might result in events being received multiple times. | ||
| * <p> | ||
| * All event receivers have to implement this interface. A new receiver can then be registered by | ||
| * adding it to {@link Features}. | ||
| * <p> | ||
| * <p> | ||
| * After registration, the system will notify a receiver for any incoming Discord event. | ||
| */ | ||
| @FunctionalInterface | ||
| public interface EventReceiver extends EventListener, Feature { | ||
| // Basically a renaming of JDAs EventListener, plus our Feature marker interface | ||
| } |
11 changes: 11 additions & 0 deletions
11
application/src/main/java/org/togetherjava/tjbot/commands/Feature.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package org.togetherjava.tjbot.commands; | ||
|
|
||
| /** | ||
| * Interface for features supported by the bots core system. | ||
| * <p> | ||
| * New features are added in {@link org.togetherjava.tjbot.commands.Features} and from there picked | ||
| * up by {@link org.togetherjava.tjbot.commands.system.BotCore}. | ||
| */ | ||
| public interface Feature { | ||
| // Marker interface | ||
| } |
84 changes: 84 additions & 0 deletions
84
application/src/main/java/org/togetherjava/tjbot/commands/Features.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package org.togetherjava.tjbot.commands; | ||
|
|
||
| import net.dv8tion.jda.api.JDA; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.togetherjava.tjbot.commands.basic.PingCommand; | ||
| 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.temp.TemporaryModerationRoutine; | ||
| import org.togetherjava.tjbot.commands.system.BotCore; | ||
| import org.togetherjava.tjbot.commands.tags.TagCommand; | ||
| import org.togetherjava.tjbot.commands.tags.TagManageCommand; | ||
| import org.togetherjava.tjbot.commands.tags.TagSystem; | ||
| import org.togetherjava.tjbot.commands.tags.TagsCommand; | ||
| import org.togetherjava.tjbot.db.Database; | ||
| import org.togetherjava.tjbot.routines.ModAuditLogRoutine; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
|
|
||
| /** | ||
| * Utility class that offers all features that should be registered by the system, such as commands. | ||
| * New features have to be added here, where {@link BotCore} will then pick it up from and register | ||
| * it with the system. | ||
| * <p> | ||
| * To add a new slash command, extend the commands returned by | ||
| * {@link #createFeatures(JDA, Database)}. | ||
| */ | ||
| public enum Features { | ||
| ; | ||
|
|
||
| /** | ||
| * Creates all features that should be registered with this application. | ||
| * <p> | ||
| * Calling this method multiple times will result in multiple features being created, which | ||
| * generally should be avoided. | ||
| * | ||
| * @param jda the JDA instance commands will be registered at | ||
| * @param database the database of the application, which features can use to persist data | ||
| * @return a collection of all features | ||
| */ | ||
| public static @NotNull Collection<Feature> createFeatures(@NotNull JDA jda, | ||
| @NotNull Database database) { | ||
| TagSystem tagSystem = new TagSystem(database); | ||
| ModerationActionsStore actionsStore = new ModerationActionsStore(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 | ||
| // available. | ||
| Collection<Feature> features = new ArrayList<>(); | ||
|
|
||
| // Routines | ||
| // TODO This should be moved into some proper command system instead (see GH issue #235 | ||
| // which adds support for routines) | ||
| new ModAuditLogRoutine(jda, database).start(); | ||
| new TemporaryModerationRoutine(jda, actionsStore).start(); | ||
|
|
||
| // Message receivers | ||
|
|
||
| // Event receivers | ||
| features.add(new RejoinMuteListener(actionsStore)); | ||
|
|
||
| // Slash commands | ||
| features.add(new PingCommand()); | ||
| features.add(new TeXCommand()); | ||
| features.add(new TagCommand(tagSystem)); | ||
| features.add(new TagManageCommand(tagSystem)); | ||
| features.add(new TagsCommand(tagSystem)); | ||
| features.add(new VcActivityCommand()); | ||
| features.add(new WarnCommand(actionsStore)); | ||
| features.add(new KickCommand(actionsStore)); | ||
| features.add(new BanCommand(actionsStore)); | ||
| features.add(new UnbanCommand(actionsStore)); | ||
| features.add(new AuditCommand(actionsStore)); | ||
| features.add(new MuteCommand(actionsStore)); | ||
| features.add(new UnmuteCommand(actionsStore)); | ||
|
|
||
| // Mixtures | ||
| features.add(new FreeCommand()); | ||
|
|
||
| return features; | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
application/src/main/java/org/togetherjava/tjbot/commands/MessageReceiver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package org.togetherjava.tjbot.commands; | ||
|
|
||
| import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; | ||
| import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Receives incoming Discord guild messages from channels matching a given pattern. | ||
| * <p> | ||
| * All message receivers have to implement this interface. For convenience, there is a | ||
| * {@link MessageReceiverAdapter} available that implemented most methods already. A new receiver | ||
| * can then be registered by adding it to {@link Features}. | ||
| * <p> | ||
| * <p> | ||
| * After registration, the system will notify a receiver whenever a new message was sent or an | ||
| * existing message was updated in any channel matching the {@link #getChannelNamePattern()} the bot | ||
| * is added to. | ||
| */ | ||
| public interface MessageReceiver extends Feature { | ||
| /** | ||
| * Retrieves the pattern matching the names of channels of which this receiver is interested in | ||
| * receiving sent messages from. Called by the core system once during the startup in order to | ||
| * register the receiver accordingly. | ||
| * <p> | ||
| * Changes on the pattern returned by this method afterwards will not be picked up. | ||
| * | ||
| * @return the pattern matching the names of relevant channels | ||
| */ | ||
| @NotNull | ||
| Pattern getChannelNamePattern(); | ||
Zabuzard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Triggered by the core system whenever a new message was sent and received in a text channel | ||
| * of a guild the bot has been added to. | ||
| * | ||
| * @param event the event that triggered this, containing information about the corresponding | ||
| * message that was sent and received | ||
| */ | ||
| void onMessageReceived(@NotNull GuildMessageReceivedEvent event); | ||
|
|
||
| /** | ||
| * Triggered by the core system whenever an existing message was edited in a text channel of a | ||
| * guild the bot has been added to. | ||
| * | ||
| * @param event the event that triggered this, containing information about the corresponding | ||
| * message that was edited | ||
| */ | ||
| void onMessageUpdated(@NotNull GuildMessageUpdateEvent event); | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
application/src/main/java/org/togetherjava/tjbot/commands/MessageReceiverAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package org.togetherjava.tjbot.commands; | ||
|
|
||
| import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; | ||
| import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Adapter implementation of a {@link MessageReceiver}. A new receiver can then be registered by | ||
| * adding it to {@link Features}. | ||
| * <p> | ||
| * {@link #onMessageReceived(GuildMessageReceivedEvent)} and | ||
| * {@link #onMessageUpdated(GuildMessageUpdateEvent)} can be overridden if desired. The default | ||
| * implementation is empty, the adapter will not react to such events. | ||
| */ | ||
| public abstract class MessageReceiverAdapter implements MessageReceiver { | ||
|
|
||
| private final Pattern channelNamePattern; | ||
|
|
||
| /** | ||
| * Creates an instance of a message receiver with the given pattern. | ||
| * | ||
| * @param channelNamePattern the pattern matching names of channels interested in, only messages | ||
| * from matching channels will be received | ||
| */ | ||
| protected MessageReceiverAdapter(@NotNull Pattern channelNamePattern) { | ||
| this.channelNamePattern = channelNamePattern; | ||
| } | ||
|
|
||
| @Override | ||
| public final @NotNull Pattern getChannelNamePattern() { | ||
| return channelNamePattern; | ||
| } | ||
Zabuzard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @SuppressWarnings("NoopMethodInAbstractClass") | ||
| @Override | ||
| public void onMessageReceived(@NotNull GuildMessageReceivedEvent event) { | ||
| // Adapter does not react by default, subclasses may change this behavior | ||
| } | ||
|
|
||
| @SuppressWarnings("NoopMethodInAbstractClass") | ||
| @Override | ||
| public void onMessageUpdated(@NotNull GuildMessageUpdateEvent event) { | ||
| // Adapter does not react by default, subclasses may change this behavior | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.