@@ -5,6 +5,7 @@ import com.mrkirby153.botcore.builder.MessageBuilder
5
5
import com.mrkirby153.botcore.command.slashcommand.dsl.SlashContext
6
6
import com.mrkirby153.botcore.coroutine.await
7
7
import com.mrkirby153.botcore.utils.SLF4J
8
+ import kotlinx.coroutines.CancellableContinuation
8
9
import kotlinx.coroutines.suspendCancellableCoroutine
9
10
import net.dv8tion.jda.api.entities.User
10
11
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent
@@ -31,11 +32,11 @@ object ConfirmationHandler : ListenerAdapter() {
31
32
try {
32
33
if (id == interaction.yes) {
33
34
log.debug(" $interactionId confirmed" )
34
- event.deferReply().queue()
35
+ event.deferReply(interaction.ephemeral ).queue()
35
36
interaction.callback.onSuccess(event.hook)
36
37
} else if (id == interaction.no) {
37
38
log.debug(" $interactionId rejected" )
38
- event.deferReply().queue()
39
+ event.deferReply(interaction.ephemeral ).queue()
39
40
interaction.callback.onFail(event.hook)
40
41
}
41
42
} finally {
@@ -48,11 +49,13 @@ object ConfirmationHandler : ListenerAdapter() {
48
49
callback : Callback ,
49
50
yes : String ,
50
51
no : String ,
51
- allowedUsers : List <User >
52
+ allowedUsers : List <User >,
53
+ ephemeral : Boolean
52
54
): Long {
53
55
val id = nextId.getAndIncrement()
54
56
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)
56
59
componentsToInteractions[yes] = id
57
60
componentsToInteractions[no] = id
58
61
return id
@@ -68,6 +71,19 @@ object ConfirmationHandler : ListenerAdapter() {
68
71
}
69
72
}
70
73
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
+
71
87
/* *
72
88
* Confirms an interaction with two buttons, a "yes" button and a "no" button. Specify [yesButton] or
73
89
* [noButton] to determine what the respective buttons look like. Specify [allowedUsers] to dictate
@@ -90,16 +106,13 @@ suspend fun SlashContext.confirm(
90
106
}
91
107
reply(message.create()).setEphemeral(ephemeral).await()
92
108
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
+ )
103
116
continuation.invokeOnCancellation { ConfirmationHandler .dequeue(id) }
104
117
}
105
118
@@ -112,10 +125,11 @@ suspend fun SlashContext.confirm(
112
125
*/
113
126
suspend fun InteractionHook.confirm (
114
127
allowedUser : User ,
128
+ ephemeral : Boolean = false,
115
129
yesButton : (ButtonBuilder .() -> Unit )? = null,
116
130
noButton : (ButtonBuilder .() -> Unit )? = null,
117
131
builder : MessageBuilder .() -> Unit
118
- ) = confirm(listOf (allowedUser), yesButton, noButton, builder)
132
+ ) = confirm(listOf (allowedUser), ephemeral, yesButton, noButton, builder)
119
133
120
134
/* *
121
135
* Confirms an interaction with two buttons, a "yes" button and a "no" button. Specify [yesButton] or
@@ -124,6 +138,7 @@ suspend fun InteractionHook.confirm(
124
138
*/
125
139
suspend fun InteractionHook.confirm (
126
140
allowedUsers : List <User >,
141
+ ephemeral : Boolean = false,
127
142
yesButton : (ButtonBuilder .() -> Unit )? = null,
128
143
noButton : (ButtonBuilder .() -> Unit )? = null,
129
144
builder : MessageBuilder .() -> Unit
@@ -135,16 +150,13 @@ suspend fun InteractionHook.confirm(
135
150
}
136
151
editOriginal(message.edit()).await()
137
152
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
+ )
148
160
continuation.invokeOnCancellation { ConfirmationHandler .dequeue(id) }
149
161
}
150
162
}
@@ -188,5 +200,6 @@ private data class QueuedConfirmation(
188
200
val callback : Callback ,
189
201
val yes : String ,
190
202
val no : String ,
191
- val allowedUsers : List <Long >
203
+ val allowedUsers : List <Long >,
204
+ val ephemeral : Boolean
192
205
)
0 commit comments