Skip to content

Commit

Permalink
Update commands for core examples
Browse files Browse the repository at this point in the history
  • Loading branch information
quanticc committed Dec 7, 2020
1 parent aaec973 commit b3c7951
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void main(String[] args) {
.build()
.withGateway(client -> client.on(CommandListener.createWithPrefix("!!")
.on("echo", Commands::echo)
.on("exit", (req, res) -> res.getClient().logout())
.on("exit", (req, res) -> req.getClient().logout())
.on("status", Commands::status)));
}
}
2 changes: 1 addition & 1 deletion core/src/test/java/discord4j/core/ExampleLogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void main(String[] args) {

client.on(CommandListener.createWithPrefix("!!")
.on("echo", Commands::echo)
.on("exit", (req, res) -> res.getClient().logout())
.on("exit", (req, res) -> req.getClient().logout())
.on("status", Commands::status))
.blockLast();
}
Expand Down
8 changes: 6 additions & 2 deletions core/src/test/java/discord4j/core/ExampleVoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@

import discord4j.common.util.Snowflake;
import discord4j.core.command.CommandListener;
import discord4j.core.support.AddRandomReaction;
import discord4j.core.support.Commands;
import discord4j.core.support.VoiceSupport;
import discord4j.core.support.AddRandomReaction;
import discord4j.discordjson.json.ApplicationInfoData;
import reactor.core.publisher.Mono;
import reactor.util.Logger;
import reactor.util.Loggers;

import static discord4j.core.support.Commands.isAuthor;

public class ExampleVoice {

private static final Logger log = Loggers.getLogger(ExampleVoice.class);

public static void main(String[] args) {
GatewayDiscordClient client = DiscordClient.create(System.getenv("token"))
.login()
Expand All @@ -42,7 +46,7 @@ public static void main(String[] args) {
CommandListener listener = CommandListener.createWithPrefix("!!")
.filter(req -> isAuthor(ownerId, req))
.on("echo", Commands::echo)
.on("exit", (req, res) -> res.getClient().logout())
.on("exit", (req, res) -> req.getClient().logout())
.on("status", Commands::status)
.on("requestMembers", Commands::requestMembers)
.on("getMembers", Commands::getMembers)
Expand Down
3 changes: 3 additions & 0 deletions core/src/test/java/discord4j/core/command/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package discord4j.core.command;

import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;

import java.util.Objects;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -45,4 +46,6 @@ public Publisher<Void> apply(CommandRequest request, CommandResponse response) {
public boolean test(CommandRequest request) {
return condition.test(request);
}

static CommandHandler NOOP_HANDLER = new CommandHandler(req -> false, (req, res) -> Mono.empty());
}
34 changes: 16 additions & 18 deletions core/src/test/java/discord4j/core/command/CommandListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuples;

import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;

Expand All @@ -36,17 +36,15 @@ public class CommandListener extends ReactiveEventAdapter {

private final Function<MessageCreateEvent, Publisher<String>> prefixFunction;
private final Function<? super CommandRequest, Mono<Boolean>> filter;
private final CopyOnWriteArrayList<CommandHandler> handlers = new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<CommandHandler> handlers;
private final CopyOnWriteArrayList<BiFunction<? super CommandRequest, Throwable, Publisher<?>>> errorHandlers;

CommandListener(Function<MessageCreateEvent, Publisher<String>> prefixFunction,
Function<? super CommandRequest, Mono<Boolean>> filter) {
this.prefixFunction = prefixFunction;
this.filter = filter;
}

CommandListener(CommandListener source) {
this.prefixFunction = source.prefixFunction;
this.filter = source.filter;
this.handlers = new CopyOnWriteArrayList<>();
this.errorHandlers = new CopyOnWriteArrayList<>();
}

public static CommandListener create() {
Expand Down Expand Up @@ -93,18 +91,18 @@ public Publisher<?> onMessageCreate(MessageCreateEvent event) {
int endIndex = (trimmed.indexOf(' ') < 0) ? trimmed.length() : trimmed.indexOf(' ');
String commandName = trimmed.substring(0, endIndex);
String remaining = trimmed.substring(commandName.length()).trim();
return Tuples.of(
new DefaultCommandRequest(event, commandName, remaining),
new DefaultCommandResponse(event)
);
return new CommandOperations(event, commandName, remaining);
})
.filterWhen(exchange -> filter.apply(exchange.getT1()))
.flatMap(exchange -> handlers.stream()
.filter(handler -> handler.test(exchange.getT1()))
.map(handler -> handler.apply(exchange.getT1(), exchange.getT2()))
.findFirst()
.orElse(Mono.empty()));
// TODO: use CommandResponse to manage event lifecycle
.filterWhen(filter)
.flatMap(ops -> applyCommand(ops, ops));
}
}

private Mono<Void> applyCommand(CommandRequest req, CommandResponse res) {
return Mono.from(handlers.stream()
.filter(handler -> handler.test(req))
.findFirst()
.orElse(CommandHandler.NOOP_HANDLER)
.apply(req, res));
}
}
114 changes: 114 additions & 0 deletions core/src/test/java/discord4j/core/command/CommandOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* This file is part of Discord4J.
*
* Discord4J is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Discord4J is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Discord4J. If not, see <http://www.gnu.org/licenses/>.
*/

package discord4j.core.command;

import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.entity.User;
import discord4j.core.object.entity.channel.MessageChannel;
import discord4j.core.object.entity.channel.PrivateChannel;
import discord4j.core.spec.EmbedCreateSpec;
import discord4j.core.spec.MessageCreateSpec;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;

import java.util.function.Consumer;
import java.util.function.Supplier;

class CommandOperations implements CommandRequest, CommandResponse {

private final MessageCreateEvent event;
private final String command;
private final String parameters;
private final Supplier<Mono<MessageChannel>> replyChannel;
private final Scheduler replyScheduler;

public CommandOperations(MessageCreateEvent event, String command, String parameters) {
this.event = event;
this.command = command;
this.parameters = parameters;
this.replyChannel = this::getReplyChannel;
this.replyScheduler = Schedulers.immediate();
}

CommandOperations(MessageCreateEvent event, String command, String parameters,
Supplier<Mono<MessageChannel>> replyChannel, Scheduler replyScheduler) {
this.event = event;
this.command = command;
this.parameters = parameters;
this.replyChannel = replyChannel;
this.replyScheduler = replyScheduler;
}

@Override
public MessageCreateEvent event() {
return event;
}

@Override
public String command() {
return command;
}

@Override
public String parameters() {
return parameters;
}

@Override
public Mono<MessageChannel> getReplyChannel() {
return event.getMessage().getChannel();
}

@Override
public Mono<PrivateChannel> getPrivateChannel() {
return Mono.justOrEmpty(event.getMessage().getAuthor()).flatMap(User::getPrivateChannel);
}

@Override
public CommandResponse withDirectMessage() {
return new CommandOperations(event, command, parameters, () -> getPrivateChannel().cast(MessageChannel.class),
replyScheduler);
}

@Override
public CommandResponse withReplyChannel(Mono<MessageChannel> channelSource) {
return new CommandOperations(event, command, parameters, () -> channelSource, replyScheduler);
}

@Override
public CommandResponse withScheduler(Scheduler scheduler) {
return new CommandOperations(event, command, parameters, replyChannel, scheduler);
}

@Override
public Mono<Void> sendMessage(Consumer<? super MessageCreateSpec> spec) {
return replyChannel.get()
.publishOn(replyScheduler)
.flatMap(channel -> channel.createMessage(spec))
.then();
}

@Override
public Mono<Void> sendEmbed(Consumer<? super EmbedCreateSpec> spec) {
return replyChannel.get()
.publishOn(replyScheduler)
.flatMap(channel -> channel.createEmbed(spec))
.then();
}
}
6 changes: 6 additions & 0 deletions core/src/test/java/discord4j/core/command/CommandRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.User;
import discord4j.core.object.entity.channel.GuildChannel;
import discord4j.core.object.entity.channel.MessageChannel;
import discord4j.core.object.entity.channel.PrivateChannel;
import discord4j.rest.util.PermissionSet;
import reactor.core.publisher.Mono;

Expand All @@ -35,6 +37,10 @@ public interface CommandRequest {

String parameters();

Mono<MessageChannel> getReplyChannel();

Mono<PrivateChannel> getPrivateChannel();

default GatewayDiscordClient getClient() {
return event().getClient();
}
Expand Down
17 changes: 11 additions & 6 deletions core/src/test/java/discord4j/core/command/CommandResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@

package discord4j.core.command;

import discord4j.core.GatewayDiscordClient;
import discord4j.core.object.entity.channel.MessageChannel;
import discord4j.core.object.entity.channel.PrivateChannel;
import discord4j.core.spec.EmbedCreateSpec;
import discord4j.core.spec.MessageCreateSpec;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;

import java.util.function.Consumer;

public interface CommandResponse {

GatewayDiscordClient getClient();
CommandResponse withDirectMessage();

CommandResponse withReplyChannel(Mono<MessageChannel> channelSource);

Mono<MessageChannel> getReplyChannel();
CommandResponse withScheduler(Scheduler scheduler);

Mono<PrivateChannel> getPrivateChannel();
Mono<Void> sendMessage(Consumer<? super MessageCreateSpec> spec);

// TODO: add error(...) methods for common error handling
Mono<Void> sendEmbed(Consumer<? super EmbedCreateSpec> spec);
}

This file was deleted.

This file was deleted.

0 comments on commit b3c7951

Please sign in to comment.