From b692e6033da32afec441569bafc0b5c879dd324c Mon Sep 17 00:00:00 2001 From: Devoxin Date: Tue, 24 Jan 2023 22:19:22 +0000 Subject: [PATCH] Add more context send methods. --- build.gradle | 2 +- .../me/devoxin/flight/api/context/Context.kt | 5 ++ .../flight/api/context/MessageContext.kt | 55 ++++++++++++++++--- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index e64d0ba..8083aae 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group 'me.devoxin' -version '3.0.1' +version '3.0.2' repositories { maven { diff --git a/src/main/kotlin/me/devoxin/flight/api/context/Context.kt b/src/main/kotlin/me/devoxin/flight/api/context/Context.kt index 9a39ffc..5459b94 100644 --- a/src/main/kotlin/me/devoxin/flight/api/context/Context.kt +++ b/src/main/kotlin/me/devoxin/flight/api/context/Context.kt @@ -42,6 +42,11 @@ interface Context { ?: messageChannel.sendMessage(content).submit() } + fun respond(message: MessageCreateData): CompletableFuture<*> { + return asSlashContext?.respond0(message) + ?: messageChannel.sendMessage(message).submit() + } + fun send(content: String) { messageChannel.sendMessage(content).submit() } diff --git a/src/main/kotlin/me/devoxin/flight/api/context/MessageContext.kt b/src/main/kotlin/me/devoxin/flight/api/context/MessageContext.kt index 11a3388..ec57802 100644 --- a/src/main/kotlin/me/devoxin/flight/api/context/MessageContext.kt +++ b/src/main/kotlin/me/devoxin/flight/api/context/MessageContext.kt @@ -14,6 +14,7 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent import net.dv8tion.jda.api.requests.RestAction import net.dv8tion.jda.api.utils.FileUpload import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder +import net.dv8tion.jda.api.utils.messages.MessageCreateData import java.util.regex.Pattern class MessageContext( @@ -66,13 +67,25 @@ class MessageContext( send0({ setEmbeds(EmbedBuilder().apply(embed).build()) }).submit() } + /** + * Sends a message to the channel the Context was created from. + * This is intended as a lower level function (compared to the other send methods) + * to offer more functionality when needed. + * + * @param message + * The message to send. + */ + fun send(message: MessageCreateData) { + messageChannel.sendMessage(message).submit() + } + /** * Sends a message embed to the channel the Context was created from. * * @param content * The content of the message. * - * @return The created message. + * @return The sent message. */ suspend fun sendAsync(content: String): Message { return send0({ setContent(content) }).submit().await() @@ -84,7 +97,7 @@ class MessageContext( * @param attachment * The attachment to send. * - * @return The created message. + * @return The sent message. */ suspend fun sendAsync(attachment: FileUpload): Message { return send0(null, attachment).submit().await() @@ -96,12 +109,26 @@ class MessageContext( * @param embed * Options to apply to the message embed. * - * @return The created message. + * @return The sent message. */ suspend fun sendAsync(embed: EmbedBuilder.() -> Unit): Message { return send0({ setEmbeds(EmbedBuilder().apply(embed).build()) }).submit().await() } + /** + * Sends a message to the channel the Context was created from. + * This is intended as a lower level function (compared to the other send methods) + * to offer more functionality when needed. + * + * @param message + * The message to send. + * + * @return The sent message. + */ + suspend fun sendAsync(message: MessageCreateData): Message { + return messageChannel.sendMessage(message).submit().await() + } + /** * Sends the message author a direct message. * @@ -110,10 +137,24 @@ class MessageContext( */ fun sendPrivate(content: String) { author.openPrivateChannel().submit() - .thenAccept { - it.sendMessage(content).submit() - .handle { _, _ -> it.delete().submit() } - } + .thenCompose { it.sendMessage(content).submit() } + .thenCompose { it.channel.asPrivateChannel().delete().submit() } + } + + /** + * Sends the message author a direct message. + * This is intended as a lower level function (compared to the other send methods) + * to offer more functionality when needed. + * + * @param message + * The message to send. + * + * @return The sent message. + */ + fun sendPrivate(message: MessageCreateData) { + author.openPrivateChannel().submit() + .thenCompose { it.sendMessage(message).submit() } + .thenCompose { it.channel.asPrivateChannel().delete().submit() } } private fun send0(messageOpts: (MessageCreateBuilder.() -> Unit)? = null, vararg files: FileUpload): RestAction {