diff --git a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java index 6c74513..d2d8a5c 100644 --- a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java @@ -4,15 +4,27 @@ import discord4j.core.DiscordClientBuilder; import discord4j.core.GatewayDiscordClient; import discord4j.core.event.domain.lifecycle.ReadyEvent; import discord4j.core.event.domain.message.MessageCreateEvent; -import discord4j.core.object.entity.Message; import discord4j.core.object.entity.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import xyz.vallat.louis.commands.Command; +import xyz.vallat.louis.commands.Ping; + +import java.util.HashMap; +import java.util.Map; public class MovieQuoteBot { public static final String DISCORD_TOKEN_ENVIRONMENT = "DISCORD_TOKEN"; + public static final String PREFIX = "!"; private static final Logger logger = LoggerFactory.getLogger(MovieQuoteBot.class.getCanonicalName()); + private static final Map commands = new HashMap<>(); + + static { + commands.put("ping", new Ping()); + } public static void main(String[] args) { if (System.getenv(DISCORD_TOKEN_ENVIRONMENT) == null && args.length == 0) { @@ -31,6 +43,14 @@ public class MovieQuoteBot { logger.info("Logged in as {}#{}.", self.getUsername(), self.getDiscriminator()); }); + client.getEventDispatcher().on(MessageCreateEvent.class) + .flatMap(event -> Mono.just(event.getMessage().getContent()) + .flatMap(content -> Flux.fromIterable(commands.entrySet()) + .filter(entry -> content.startsWith(PREFIX + entry.getKey())) + .flatMap(entry -> entry.getValue().execute(event)) + .next())) + .subscribe(); + client.onDisconnect().block(); } diff --git a/src/main/java/xyz/vallat/louis/commands/Command.java b/src/main/java/xyz/vallat/louis/commands/Command.java new file mode 100644 index 0000000..1bba91f --- /dev/null +++ b/src/main/java/xyz/vallat/louis/commands/Command.java @@ -0,0 +1,9 @@ +package xyz.vallat.louis.commands; + +import discord4j.core.event.domain.message.MessageCreateEvent; +import reactor.core.publisher.Mono; + +public interface Command { + + Mono execute(MessageCreateEvent event); +} diff --git a/src/main/java/xyz/vallat/louis/commands/Ping.java b/src/main/java/xyz/vallat/louis/commands/Ping.java new file mode 100644 index 0000000..ad38690 --- /dev/null +++ b/src/main/java/xyz/vallat/louis/commands/Ping.java @@ -0,0 +1,14 @@ +package xyz.vallat.louis.commands; + +import discord4j.core.event.domain.message.MessageCreateEvent; +import reactor.core.publisher.Mono; + +public class Ping implements Command { + + @Override + public Mono execute(MessageCreateEvent event) { + return event.getMessage().getChannel() + .flatMap(channel -> channel.createMessage("Pong!")) + .then(); + } +}