Skip to content

Commit d7b7f93

Browse files
committed
Add listener for #talk-to-james channel
1 parent 83c4dc0 commit d7b7f93

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/main/java/org/javacord/bot/Constants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public final class Constants {
2727
*/
2828
public static final long DAPI_JAVACORD_CHANNEL_ID = 381889796785831936L;
2929

30+
/**
31+
* The ID of the #talk-to-james channel on the Javacord server.
32+
*/
33+
public static final long TALK_TO_JAMES_CHANNEL_ID = 712366253603422209L;
34+
3035
/**
3136
* The API URL where to obtain the latest release version for Javacord.
3237
*/

src/main/java/org/javacord/bot/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.javacord.bot.commands.SetupCommand;
1818
import org.javacord.bot.commands.WikiCommand;
1919
import org.javacord.bot.listeners.CommandCleanupListener;
20+
import org.javacord.bot.listeners.TalkToJamesListener;
2021
import org.javacord.bot.util.LatestVersionFinder;
2122

2223
import java.io.BufferedReader;
@@ -80,6 +81,7 @@ public static void main(String[] args) throws IOException {
8081
handler.registerCommand(new HelpCommand(handler));
8182

8283
api.addMessageDeleteListener(new CommandCleanupListener());
84+
api.addMessageCreateListener(new TalkToJamesListener(handler));
8385
}
8486

8587
private static void setupLogging() throws IOException {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.javacord.bot.listeners;
2+
3+
import de.btobastian.sdcf4j.CommandHandler;
4+
import org.javacord.api.entity.message.embed.EmbedBuilder;
5+
import org.javacord.api.event.message.MessageCreateEvent;
6+
import org.javacord.api.listener.message.MessageCreateListener;
7+
import org.javacord.api.util.logging.ExceptionLogger;
8+
import org.javacord.bot.Constants;
9+
10+
import java.util.Arrays;
11+
12+
public class TalkToJamesListener implements MessageCreateListener {
13+
14+
private final CommandHandler commandHandler;
15+
16+
/**
17+
* Initializes the listener.
18+
*
19+
* @param commandHandler The command handler used to extract commands.
20+
*/
21+
public TalkToJamesListener(CommandHandler commandHandler) {
22+
this.commandHandler = commandHandler;
23+
}
24+
25+
@Override
26+
public void onMessageCreate(MessageCreateEvent event) {
27+
if (event.getChannel().getId() != Constants.TALK_TO_JAMES_CHANNEL_ID) {
28+
return;
29+
}
30+
if (event.getMessageAuthor().isYourself()) {
31+
return;
32+
}
33+
34+
boolean messageIsCommand = commandHandler.getCommands().stream()
35+
.flatMap(command -> Arrays.stream(command.getCommandAnnotation().aliases()))
36+
.anyMatch(alias -> event.getMessageContent().startsWith(alias));
37+
38+
if (!messageIsCommand) {
39+
EmbedBuilder embed = new EmbedBuilder()
40+
.setTitle("Information")
41+
.setDescription(
42+
"This channel is solely used to talk to me.\nYou can see all my commands with `!help`.")
43+
.setColor(Constants.JAVACORD_ORANGE);
44+
CommandCleanupListener.insertResponseTracker(embed, event.getMessageId());
45+
event.getChannel().sendMessage(embed).exceptionally(ExceptionLogger.get());
46+
}
47+
}
48+
49+
}

0 commit comments

Comments
 (0)