Skip to content

Commit

Permalink
Add more context send methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Jan 24, 2023
1 parent 2a8d380 commit b692e60
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'me.devoxin'
version '3.0.1'
version '3.0.2'

repositories {
maven {
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/me/devoxin/flight/api/context/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
55 changes: 48 additions & 7 deletions src/main/kotlin/me/devoxin/flight/api/context/MessageContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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.
*
Expand All @@ -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<Message> {
Expand Down

0 comments on commit b692e60

Please sign in to comment.