|
| 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