Added a basic ping command
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
c9e25131a5
commit
3b33554a86
@ -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<String, Command> 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();
|
||||
}
|
||||
|
||||
|
9
src/main/java/xyz/vallat/louis/commands/Command.java
Normal file
9
src/main/java/xyz/vallat/louis/commands/Command.java
Normal 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);
|
||||
}
|
14
src/main/java/xyz/vallat/louis/commands/Ping.java
Normal file
14
src/main/java/xyz/vallat/louis/commands/Ping.java
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user