Added a basic ping command

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-25 19:02:50 +01:00
parent c9e25131a5
commit 3b33554a86
3 changed files with 44 additions and 1 deletions

View File

@ -4,15 +4,27 @@ import discord4j.core.DiscordClientBuilder;
import discord4j.core.GatewayDiscordClient; import discord4j.core.GatewayDiscordClient;
import discord4j.core.event.domain.lifecycle.ReadyEvent; import discord4j.core.event.domain.lifecycle.ReadyEvent;
import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.User; import discord4j.core.object.entity.User;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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 class MovieQuoteBot {
public static final String DISCORD_TOKEN_ENVIRONMENT = "DISCORD_TOKEN"; 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 Logger logger = LoggerFactory.getLogger(MovieQuoteBot.class.getCanonicalName());
private static final Map<String, Command> commands = new HashMap<>();
static {
commands.put("ping", new Ping());
}
public static void main(String[] args) { public static void main(String[] args) {
if (System.getenv(DISCORD_TOKEN_ENVIRONMENT) == null && args.length == 0) { 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()); 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(); client.onDisconnect().block();
} }

View File

@ -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<Void> execute(MessageCreateEvent event);
}

View File

@ -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<Void> execute(MessageCreateEvent event) {
return event.getMessage().getChannel()
.flatMap(channel -> channel.createMessage("Pong!"))
.then();
}
}