-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Educate about /foo when ?!.foo (#759)
* React to message commands (!close, .close, ?close) * matching * advice content follows next * Added actual advice * javadoc, got rid of the massive Pattern.compile(".*") duplication * unit tests * Improved regex * Test was unstable on CI/CD * dot after smiley looks odd
- Loading branch information
Showing
13 changed files
with
154 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
71 changes: 71 additions & 0 deletions
71
application/src/main/java/org/togetherjava/tjbot/features/basic/SlashCommandEducator.java
This file contains 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,71 @@ | ||
package org.togetherjava.tjbot.features.basic; | ||
|
||
import net.dv8tion.jda.api.EmbedBuilder; | ||
import net.dv8tion.jda.api.entities.Message; | ||
import net.dv8tion.jda.api.entities.MessageEmbed; | ||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; | ||
import net.dv8tion.jda.api.utils.FileUpload; | ||
|
||
import org.togetherjava.tjbot.features.MessageReceiverAdapter; | ||
import org.togetherjava.tjbot.features.help.HelpSystemHelper; | ||
|
||
import java.io.InputStream; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Listens to messages that are likely supposed to be message commands, such as {@code !foo} and | ||
* then educates the user about using slash commands, such as {@code /foo} instead. | ||
*/ | ||
public final class SlashCommandEducator extends MessageReceiverAdapter { | ||
private static final String SLASH_COMMAND_POPUP_ADVICE_PATH = "slashCommandPopupAdvice.png"; | ||
private static final Predicate<String> IS_MESSAGE_COMMAND = Pattern.compile(""" | ||
[.!?] #Start of message command | ||
[a-zA-Z]{2,15} #Name of message command, e.g. 'close' | ||
.* #Rest of the message | ||
""", Pattern.COMMENTS).asMatchPredicate(); | ||
|
||
@Override | ||
public void onMessageReceived(MessageReceivedEvent event) { | ||
if (event.getAuthor().isBot() || event.isWebhookMessage()) { | ||
return; | ||
} | ||
|
||
String content = event.getMessage().getContentRaw(); | ||
if (IS_MESSAGE_COMMAND.test(content)) { | ||
sendAdvice(event.getMessage()); | ||
} | ||
} | ||
|
||
private void sendAdvice(Message message) { | ||
String content = | ||
""" | ||
Looks like you attempted to use a command? Please note that we only use **slash-commands** on this server 🙂 | ||
Try starting your message with a forward-slash `/` and Discord should open a popup showing you all available commands. | ||
A command might then look like `/foo` 👍"""; | ||
|
||
createReply(message, content, SLASH_COMMAND_POPUP_ADVICE_PATH).queue(); | ||
} | ||
|
||
private static MessageCreateAction createReply(Message messageToReplyTo, String content, | ||
String imagePath) { | ||
boolean useImage = true; | ||
InputStream imageData = HelpSystemHelper.class.getResourceAsStream("/" + imagePath); | ||
if (imageData == null) { | ||
useImage = false; | ||
} | ||
|
||
MessageEmbed embed = new EmbedBuilder().setDescription(content) | ||
.setImage(useImage ? "attachment://" + imagePath : null) | ||
.build(); | ||
|
||
MessageCreateAction action = messageToReplyTo.replyEmbeds(embed); | ||
if (useImage) { | ||
action = action.addFiles(FileUpload.fromData(imageData, imagePath)); | ||
} | ||
|
||
return action; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions
70
...ication/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java
This file contains 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,70 @@ | ||
package org.togetherjava.tjbot.features.basic; | ||
|
||
import net.dv8tion.jda.api.entities.MessageEmbed; | ||
import net.dv8tion.jda.api.entities.channel.ChannelType; | ||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; | ||
import net.dv8tion.jda.api.utils.messages.MessageCreateData; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import org.togetherjava.tjbot.features.MessageReceiver; | ||
import org.togetherjava.tjbot.jda.JdaTester; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
final class SlashCommandEducatorTest { | ||
private JdaTester jdaTester; | ||
private MessageReceiver messageReceiver; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
jdaTester = new JdaTester(); | ||
messageReceiver = new SlashCommandEducator(); | ||
} | ||
|
||
private MessageReceivedEvent sendMessage(String content) { | ||
MessageCreateData message = new MessageCreateBuilder().setContent(content).build(); | ||
MessageReceivedEvent event = | ||
jdaTester.createMessageReceiveEvent(message, List.of(), ChannelType.TEXT); | ||
|
||
messageReceiver.onMessageReceived(event); | ||
|
||
return event; | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideMessageCommands") | ||
void sendsAdviceOnMessageCommand(String message) { | ||
// GIVEN a message containing a message command | ||
// WHEN the message is sent | ||
MessageReceivedEvent event = sendMessage(message); | ||
|
||
// THEN the system replies to it with an advice | ||
verify(event.getMessage(), times(1)).replyEmbeds(any(MessageEmbed.class)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideOtherMessages") | ||
void ignoresOtherMessages(String message) { | ||
// GIVEN a message that is not a message command | ||
// WHEN the message is sent | ||
MessageReceivedEvent event = sendMessage(message); | ||
|
||
// THEN the system ignores the message and does not reply to it | ||
verify(event.getMessage(), never()).replyEmbeds(any(MessageEmbed.class)); | ||
} | ||
|
||
private static Stream<String> provideMessageCommands() { | ||
return Stream.of("!foo", ".foo", "?foo", ".test", "!whatever", "!this is a test"); | ||
} | ||
|
||
private static Stream<String> provideOtherMessages() { | ||
return Stream.of(" a ", "foo", "#foo", "/foo", "!!!", "?!?!?", "?", ".,-", "!f", "! foo"); | ||
} | ||
} |
This file contains 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
This file contains 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