-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4097373
commit 99ef608
Showing
14 changed files
with
655 additions
and
815 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/main/java/com/denizenscript/ddiscordbot/DiscordCommandUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.denizenscript.ddiscordbot; | ||
|
||
import com.denizenscript.ddiscordbot.objects.*; | ||
import com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException; | ||
import com.denizenscript.denizencore.scripts.ScriptEntry; | ||
import com.denizenscript.denizencore.utilities.debugging.Debug; | ||
import net.dv8tion.jda.api.requests.RestAction; | ||
|
||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
public class DiscordCommandUtils { | ||
|
||
public static DiscordBotTag inferBot(Object... options) { | ||
for (Object obj : options) { | ||
DiscordBotTag result = inferBotInternal(obj); | ||
if (result != null) { | ||
return result; | ||
} | ||
} | ||
throw new InvalidArgumentsRuntimeException("Unknown bot to use! Must specify 'id:' argument!"); | ||
} | ||
|
||
public static DiscordBotTag inferBotInternal(Object obj) { | ||
if (obj instanceof DiscordBotTag botTag) { | ||
return botTag; | ||
} | ||
else if (obj instanceof DiscordUserTag user && user.bot != null) { | ||
return new DiscordBotTag(user.bot); | ||
} | ||
else if (obj instanceof DiscordRoleTag role && role.bot != null) { | ||
return new DiscordBotTag(role.bot); | ||
} | ||
else if (obj instanceof DiscordGroupTag group && group.bot != null) { | ||
return new DiscordBotTag(group.bot); | ||
} | ||
else if (obj instanceof DiscordChannelTag channel && channel.bot != null) { | ||
return new DiscordBotTag(channel.bot); | ||
} | ||
else if (obj instanceof List list) { | ||
for (Object subObj : list) { | ||
DiscordBotTag val = inferBotInternal(subObj); | ||
if (val != null) { | ||
return val; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static void cleanWait(ScriptEntry scriptEntry, ActionOrValue<?> action) { | ||
action.onErrorFlatMap(t -> { | ||
Debug.echoError(scriptEntry, t); | ||
scriptEntry.setFinished(true); | ||
return null; | ||
}).onSuccess((t) -> scriptEntry.setFinished(true)).queue(); | ||
} | ||
|
||
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.