Skip to content

Commit 0e1d483

Browse files
committed
Support ephemeral confirmations
1 parent 217955e commit 0e1d483

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

src/main/kotlin/com/mrkirby153/botcore/ConfirmationHandler.kt

+39-26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.mrkirby153.botcore.builder.MessageBuilder
55
import com.mrkirby153.botcore.command.slashcommand.dsl.SlashContext
66
import com.mrkirby153.botcore.coroutine.await
77
import com.mrkirby153.botcore.utils.SLF4J
8+
import kotlinx.coroutines.CancellableContinuation
89
import kotlinx.coroutines.suspendCancellableCoroutine
910
import net.dv8tion.jda.api.entities.User
1011
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent
@@ -31,11 +32,11 @@ object ConfirmationHandler : ListenerAdapter() {
3132
try {
3233
if (id == interaction.yes) {
3334
log.debug("$interactionId confirmed")
34-
event.deferReply().queue()
35+
event.deferReply(interaction.ephemeral).queue()
3536
interaction.callback.onSuccess(event.hook)
3637
} else if (id == interaction.no) {
3738
log.debug("$interactionId rejected")
38-
event.deferReply().queue()
39+
event.deferReply(interaction.ephemeral).queue()
3940
interaction.callback.onFail(event.hook)
4041
}
4142
} finally {
@@ -48,11 +49,13 @@ object ConfirmationHandler : ListenerAdapter() {
4849
callback: Callback,
4950
yes: String,
5051
no: String,
51-
allowedUsers: List<User>
52+
allowedUsers: List<User>,
53+
ephemeral: Boolean
5254
): Long {
5355
val id = nextId.getAndIncrement()
5456
log.debug("Enqueueing a callback with Y:$yes, N:$no as ID $id")
55-
interactions[id] = QueuedConfirmation(callback, yes, no, allowedUsers.map { it.idLong })
57+
interactions[id] =
58+
QueuedConfirmation(callback, yes, no, allowedUsers.map { it.idLong }, ephemeral)
5659
componentsToInteractions[yes] = id
5760
componentsToInteractions[no] = id
5861
return id
@@ -68,6 +71,19 @@ object ConfirmationHandler : ListenerAdapter() {
6871
}
6972
}
7073

74+
private class ConfirmationCallback(
75+
private val continuation: CancellableContinuation<Pair<InteractionHook, Boolean>>
76+
) : Callback {
77+
override fun onSuccess(hook: InteractionHook) {
78+
continuation.resume(Pair(hook, true))
79+
}
80+
81+
override fun onFail(hook: InteractionHook) {
82+
continuation.resume(Pair(hook, false))
83+
}
84+
85+
}
86+
7187
/**
7288
* Confirms an interaction with two buttons, a "yes" button and a "no" button. Specify [yesButton] or
7389
* [noButton] to determine what the respective buttons look like. Specify [allowedUsers] to dictate
@@ -90,16 +106,13 @@ suspend fun SlashContext.confirm(
90106
}
91107
reply(message.create()).setEphemeral(ephemeral).await()
92108
return suspendCancellableCoroutine { continuation ->
93-
val callback = object : Callback {
94-
override fun onSuccess(hook: InteractionHook) {
95-
continuation.resume(Pair(hook, true))
96-
}
97-
98-
override fun onFail(hook: InteractionHook) {
99-
continuation.resume(Pair(hook, false))
100-
}
101-
}
102-
val id = ConfirmationHandler.enqueue(callback, buttons.first, buttons.second, allowedUsers)
109+
val id = ConfirmationHandler.enqueue(
110+
ConfirmationCallback(continuation),
111+
buttons.first,
112+
buttons.second,
113+
allowedUsers,
114+
ephemeral
115+
)
103116
continuation.invokeOnCancellation { ConfirmationHandler.dequeue(id) }
104117
}
105118

@@ -112,10 +125,11 @@ suspend fun SlashContext.confirm(
112125
*/
113126
suspend fun InteractionHook.confirm(
114127
allowedUser: User,
128+
ephemeral: Boolean = false,
115129
yesButton: (ButtonBuilder.() -> Unit)? = null,
116130
noButton: (ButtonBuilder.() -> Unit)? = null,
117131
builder: MessageBuilder.() -> Unit
118-
) = confirm(listOf(allowedUser), yesButton, noButton, builder)
132+
) = confirm(listOf(allowedUser), ephemeral, yesButton, noButton, builder)
119133

120134
/**
121135
* Confirms an interaction with two buttons, a "yes" button and a "no" button. Specify [yesButton] or
@@ -124,6 +138,7 @@ suspend fun InteractionHook.confirm(
124138
*/
125139
suspend fun InteractionHook.confirm(
126140
allowedUsers: List<User>,
141+
ephemeral: Boolean = false,
127142
yesButton: (ButtonBuilder.() -> Unit)? = null,
128143
noButton: (ButtonBuilder.() -> Unit)? = null,
129144
builder: MessageBuilder.() -> Unit
@@ -135,16 +150,13 @@ suspend fun InteractionHook.confirm(
135150
}
136151
editOriginal(message.edit()).await()
137152
return suspendCancellableCoroutine { continuation ->
138-
val callback = object : Callback {
139-
override fun onSuccess(hook: InteractionHook) {
140-
continuation.resume(Pair(hook, false))
141-
}
142-
143-
override fun onFail(hook: InteractionHook) {
144-
continuation.resume(Pair(hook, false))
145-
}
146-
}
147-
val id = ConfirmationHandler.enqueue(callback, buttons.first, buttons.second, allowedUsers)
153+
val id = ConfirmationHandler.enqueue(
154+
ConfirmationCallback(continuation),
155+
buttons.first,
156+
buttons.second,
157+
allowedUsers,
158+
ephemeral
159+
)
148160
continuation.invokeOnCancellation { ConfirmationHandler.dequeue(id) }
149161
}
150162
}
@@ -188,5 +200,6 @@ private data class QueuedConfirmation(
188200
val callback: Callback,
189201
val yes: String,
190202
val no: String,
191-
val allowedUsers: List<Long>
203+
val allowedUsers: List<Long>,
204+
val ephemeral: Boolean
192205
)

testbot/src/main/kotlin/com/mrkirby153/botcore/testbot/command/TestCommands.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class TestCommands(private val modalManager: ModalManager) : ProvidesSlashComman
111111
slashCommand("test-confirm") {
112112
subCommand("normal") {
113113
run {
114-
val (hook, confirmed) = confirm {
114+
val (hook, confirmed) = confirm(true) {
115115
text {
116116
appendLine("Are you sure you want to continue?")
117117
}

0 commit comments

Comments
 (0)