Skip to content

Commit

Permalink
feat: add from methods to NexusMessage and fix ephemeral not resp…
Browse files Browse the repository at this point in the history
…ected.
  • Loading branch information
ShindouMihou committed Aug 15, 2023
1 parent 2be24c2 commit 2553fc8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package pw.mihou.nexus.features.command.core

import org.javacord.api.event.interaction.SlashCommandCreateEvent
import org.javacord.api.interaction.callback.InteractionOriginalResponseUpdater
import org.javacord.api.util.logging.ExceptionLogger
import pw.mihou.nexus.Nexus
import pw.mihou.nexus.Nexus.globalAfterwares
import pw.mihou.nexus.Nexus.globalMiddlewares
import pw.mihou.nexus.Nexus.logger
import pw.mihou.nexus.core.threadpool.NexusThreadPool
import pw.mihou.nexus.features.command.facade.NexusCommandEvent
import pw.mihou.nexus.features.command.interceptors.core.NexusCommandInterceptorCore
import pw.mihou.nexus.features.command.interceptors.core.NexusMiddlewareGateCore
import pw.mihou.nexus.features.command.validation.OptionValidation
import pw.mihou.nexus.features.command.validation.result.ValidationResult
import pw.mihou.nexus.features.messages.NexusMessage
import java.time.Instant
import java.util.concurrent.CompletableFuture
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean

object NexusCommandDispatcher {
Expand Down Expand Up @@ -70,7 +66,11 @@ object NexusCommandDispatcher {
val updater = updaterFuture.join()
middlewareResponse.into(updater).update().exceptionally(ExceptionLogger.get())
} else {
middlewareResponse.into(nexusEvent.respondNow()).respond().exceptionally(ExceptionLogger.get())
var responder = nexusEvent.respondNow()
if (middlewareResponse.ephemeral) {
responder = nexusEvent.respondNowAsEphemeral()
}
middlewareResponse.into(responder).respond().exceptionally(ExceptionLogger.get())
}
}
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,11 @@ default void middlewares(List<String> middlewares, Consumer<Void> success) {

NexusMessage response = middlewareGate.response();
if (response != null) {
response.into(respondNow()).respond().exceptionally(ExceptionLogger.get());
InteractionImmediateResponseBuilder responder = respondNow();
if (response.getEphemeral()) {
responder = respondNowAsEphemeral();
}
response.into(responder).respond().exceptionally(ExceptionLogger.get());
}
}

Expand Down
28 changes: 25 additions & 3 deletions src/main/java/pw/mihou/nexus/features/messages/NexusMessage.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
package pw.mihou.nexus.features.messages

import org.javacord.api.entity.message.embed.EmbedBuilder
import org.javacord.api.interaction.callback.InteractionMessageBuilderBase

typealias NexusMessageBuilder = InteractionMessageBuilderBase<*>.() -> Unit

class NexusMessage internal constructor(
val ephemeral: Boolean = false,
val builder: InteractionMessageBuilderBase<*>.() -> Unit = { }
val builder: NexusMessageBuilder = { }
) {
companion object {
fun with(ephemeral: Boolean, builder: InteractionMessageBuilderBase<*>.() -> Unit): NexusMessage {
@JvmStatic
fun with(ephemeral: Boolean, builder: NexusMessageBuilder): NexusMessage {
return NexusMessage(ephemeral, builder)
}
fun with(builder: InteractionMessageBuilderBase<*>.() -> Unit): NexusMessage {

@JvmStatic
fun with(builder: NexusMessageBuilder): NexusMessage {
return with(false, builder)
}

@JvmStatic
@JvmOverloads
fun from(text: String, ephemeral: Boolean = false, builder: NexusMessageBuilder = {}): NexusMessage =
with(ephemeral) {
setContent(text)
builder(this)
}

@JvmStatic
@JvmOverloads
fun from(ephemeral: Boolean = false, builder: NexusMessageBuilder = {}, vararg embeds: EmbedBuilder): NexusMessage =
with(ephemeral) {
addEmbeds(*embeds)
builder(this)
}
}
fun <T: InteractionMessageBuilderBase<*>> into(instance: T): T {
builder(instance)
Expand Down

0 comments on commit 2553fc8

Please sign in to comment.