Skip to content

Commit

Permalink
further simplify Message command
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 19, 2023
1 parent 99ef608 commit cab9759
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import net.dv8tion.jda.api.requests.RestAction;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.concurrent.CompletableFuture;

public class DiscordCommandUtils {

Expand Down Expand Up @@ -49,59 +48,27 @@ else if (obj instanceof List list) {
return null;
}

public static void cleanWait(ScriptEntry scriptEntry, ActionOrValue<?> action) {
action.onErrorFlatMap(t -> {
public static <T> RestAction<T> mapError(ScriptEntry scriptEntry, RestAction<T> action) {
return action.onErrorMap(t -> {
Debug.echoError(scriptEntry, t);
scriptEntry.setFinished(true);
return null;
}).onSuccess((t) -> scriptEntry.setFinished(true)).queue();
});
}

public static void cleanWait(ScriptEntry scriptEntry, CompletableFuture<?> action) {
action.exceptionally(t -> {
Debug.echoError(scriptEntry, t);
scriptEntry.setFinished(true);
return null;
}).thenAccept((t) -> scriptEntry.setFinished(true));
}

public static void cleanWait(ScriptEntry scriptEntry, RestAction<?> action) {
if (action == null) {
scriptEntry.setFinished(true);
return;
}
cleanWait(scriptEntry, new ActionOrValue<>(action));
}

public static class ActionOrValue<T> {
public T raw;
public RestAction<T> action;
public ActionOrValue(T raw) {
this.raw = raw;
}
public ActionOrValue(RestAction<T> action) {
this.action = action;
}
public ActionOrValue<T> onErrorFlatMap(Function<Throwable, RestAction<T>> function) {
if (action != null) {
return new ActionOrValue<T>(action.onErrorFlatMap(function));
}
if (raw == null) {
return new ActionOrValue<T>(function.apply(null));
}
return this;
}
public <O> ActionOrValue<O> flatMap(Function<T, RestAction<O>> function) {
if (raw != null) {
return new ActionOrValue<>(function.apply(raw));
}
else {
return new ActionOrValue<>(action.flatMap(function));
}
}
public ActionOrValue<T> onSuccess(Consumer<T> success) {
if (action != null) {
return new ActionOrValue<>(action.onSuccess(success));
}
success.accept(raw);
return this;
}
public void queue() {
if (action != null) {
action.queue();
}
}
cleanWait(scriptEntry, mapError(scriptEntry, action).submit());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.ItemComponent;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.FileUpload;
import net.dv8tion.jda.api.utils.messages.*;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class DiscordMessageCommand extends AbstractCommand implements Holdable {

Expand Down Expand Up @@ -131,11 +131,11 @@ else if (component.canBeType(DiscordSelectionTag.class)) {
return actionRows;
}

private static DiscordCommandUtils.ActionOrValue requireChannel(Channel channel) {
if (!(channel instanceof MessageChannel)) {
private static CompletableFuture<MessageChannel> requireChannel(Channel channel) {
if (!(channel instanceof MessageChannel messageChannel)) {
throw new InvalidArgumentsRuntimeException("Invalid message channel ID given.");
}
return new DiscordCommandUtils.ActionOrValue<>((MessageChannel) channel);
return CompletableFuture.completedFuture(messageChannel);
}

public static void autoExecute(ScriptEntry scriptEntry,
Expand All @@ -161,7 +161,7 @@ public static void autoExecute(ScriptEntry scriptEntry,
}
DiscordConnection connection = bot.getConnection();
JDA client = connection.client;
DiscordCommandUtils.ActionOrValue<MessageChannel> toChannel = null;
CompletableFuture<? extends MessageChannel> toChannel = null;
if (reply != null && reply.channel_id != 0) {
toChannel = requireChannel(connection.getChannel(reply.channel_id));
}
Expand All @@ -176,7 +176,7 @@ else if (user != null) {
if (userObj == null) {
throw new InvalidArgumentsRuntimeException("Invalid or unrecognized user (given user ID not valid? Have you enabled the 'members' intent?).");
}
toChannel = new DiscordCommandUtils.ActionOrValue<>((RestAction) userObj.openPrivateChannel());
toChannel = userObj.openPrivateChannel().submit();
}
else {
throw new InvalidArgumentsRuntimeException("Missing channel!");
Expand Down Expand Up @@ -205,7 +205,7 @@ else if (user != null) {
}
final AbstractMessageBuilder<?, ?> finalBuilder = builder;
final DiscordBotTag finalBot = bot;
DiscordCommandUtils.cleanWait(scriptEntry, toChannel.flatMap(c -> {
DiscordCommandUtils.cleanWait(scriptEntry, toChannel.thenApply(c -> {
if (reply != null) {
return c.retrieveMessageById(reply.message_id).flatMap(m -> m.reply((MessageCreateData) finalBuilder.build()));
}
Expand All @@ -215,6 +215,6 @@ else if (edit != null) {
else {
return c.sendMessage((MessageCreateData) finalBuilder.build());
}
}).onSuccess(m -> scriptEntry.addObject("message", new DiscordMessageTag(finalBot.bot, m))));
}).thenAccept(r -> DiscordCommandUtils.mapError(scriptEntry, r).onSuccess((m -> scriptEntry.addObject("message", new DiscordMessageTag(finalBot.bot, m))))));
}
}

0 comments on commit cab9759

Please sign in to comment.