11package net.modfest.botfest.extensions
22
3+ import dev.kord.common.annotation.KordUnsafe
4+ import dev.kord.core.behavior.interaction.ActionInteractionBehavior
5+ import dev.kord.core.behavior.interaction.respondEphemeral
6+ import dev.kord.core.behavior.interaction.response.EphemeralMessageInteractionResponseBehavior
37import dev.kord.core.behavior.interaction.response.edit
8+ import dev.kord.core.entity.User
9+ import dev.kord.core.event.interaction.ButtonInteractionCreateEvent
10+ import dev.kord.core.event.interaction.GuildButtonInteractionCreateEvent
11+ import dev.kord.core.event.interaction.InteractionCreateEvent
12+ import dev.kord.rest.builder.message.create.InteractionResponseCreateBuilder
13+ import dev.kordex.core.commands.Arguments
14+ import dev.kordex.core.commands.application.slash.EphemeralSlashCommand
415import dev.kordex.core.commands.application.slash.ephemeralSubCommand
516import dev.kordex.core.commands.application.slash.group
617import dev.kordex.core.components.forms.ModalForm
18+ import dev.kordex.core.events.EventContext
719import dev.kordex.core.extensions.Extension
820import dev.kordex.core.extensions.ephemeralSlashCommand
21+ import dev.kordex.core.extensions.event
922import dev.kordex.core.i18n.toKey
1023import dev.kordex.core.i18n.types.Key
1124import dev.kordex.core.i18n.withContext
1225import dev.kordex.core.koin.KordExKoinComponent
26+ import dev.kordex.core.types.TranslatableContext
1327import dev.kordex.modules.dev.unsafe.annotations.UnsafeAPI
1428import dev.kordex.modules.dev.unsafe.commands.slash.InitialSlashCommandResponse
29+ import dev.kordex.modules.dev.unsafe.commands.slash.UnsafeSlashCommand
30+ import dev.kordex.modules.dev.unsafe.components.forms.UnsafeModalForm
1531import dev.kordex.modules.dev.unsafe.extensions.unsafeSlashCommand
32+ import net.modfest.botfest.CommandReferences
1633import net.modfest.botfest.MAIN_GUILD_ID
1734import net.modfest.botfest.Platform
1835import net.modfest.botfest.i18n.Translations
@@ -25,14 +42,30 @@ import java.util.regex.Pattern
2542/* *
2643 * Provides various debugging commands
2744 */
45+ @OptIn(UnsafeAPI ::class )
2846class EventCommands : Extension (), KordExKoinComponent {
47+ val cmds: CommandReferences by inject()
2948 override val name = " event"
3049 val platform: Platform by inject()
3150
32- @OptIn(UnsafeAPI ::class )
51+ @OptIn(KordUnsafe ::class )
3352 override suspend fun setup () {
3453 // Register command
35- unsafeSlashCommand {
54+ event<GuildButtonInteractionCreateEvent > {
55+ check {
56+ failIfNot(event.interaction.componentId == " modfest-registration-button" )
57+ }
58+ action {
59+ doRegister(
60+ { event.interaction.respondEphemeral(it) },
61+ { event.interaction.deferEphemeralResponseUnsafe() },
62+ { it.sendAndDeferEphemeral(this @action) },
63+ event.interaction.user
64+ )
65+ }
66+ }
67+
68+ cmds.registerCommand = unsafeSlashCommand {
3669 name = Translations .Commands .Register .name
3770 description = Translations .Commands .Register .description
3871 guild(MAIN_GUILD_ID )
@@ -44,70 +77,12 @@ class EventCommands : Extension(), KordExKoinComponent {
4477 initialResponse = InitialSlashCommandResponse .None
4578
4679 action {
47- val curEvent = platform.getCurrentEvent().event;
48- if (curEvent == null ) {
49- ackEphemeral {
50- content = Translations .Commands .Register .Response .noevent
51- .withContext(this @action)
52- .translateNamed()
53- }
54- return @action // Do *not* try to send any modals, we've already replied
55- }
56-
57- var platformUser = platform.getUser(this .user)
58-
59- var message = if (platformUser == null ) {
60- val modal = RegisterModal ()
61-
62- modal.displayName.initialValue = this .member?.asMember()?.effectiveName?.toKey(Locale .UK )
63- modal.displayName.translateInitialValue = false
64-
65- val result = modal.sendAndDeferEphemeral(this )
66-
67- // Result will be null if the user didn't enter anything
68- // or if the modal timed out
69- if (result == null ) {
70- return @action
71- }
72-
73- platformUser = platform.authenticatedAsBotFest().createUser(UserCreateData (
74- modal.displayName.value,
75- modal.pronouns.value,
76- modal.modrinthSlug.value,
77- this .user.id.value.toString()
78- ))
79-
80- result
81- } else {
82- // User already has their data registered in platform
83- // Ack immediately: we only have 5 seconds for any ack
84- ackEphemeral()
85- }
86-
87- // Okay, we now have a user that's registered. And we have a
88- // message we can edit to respond to the user
89- // let's perform the actual registration
90- val event = platform.getEvent(curEvent)
91-
92- if (platformUser.registered.contains(curEvent)) {
93- message.edit {
94- content = Translations .Commands .Register .Response .already
95- .withContext(this @action)
96- .translateNamed(
97- " event" to event.name
98- )
99- }
100- return @action
101- }
102-
103- platform.withAuth(user).registerMe(event)
104- message.edit {
105- content = Translations .Commands .Register .Response .success
106- .withContext(this @action)
107- .translateNamed(
108- " event" to event.name
109- )
110- }
80+ doRegister(
81+ { ackEphemeral(it) },
82+ { ackEphemeral() },
83+ { it.sendAndDeferEphemeral(this @action) },
84+ this .user.asUser()
85+ )
11186 }
11287 }
11388
@@ -119,7 +94,7 @@ class EventCommands : Extension(), KordExKoinComponent {
11994 guild(MAIN_GUILD_ID )
12095
12196 // unregister command
122- ephemeralSubCommand {
97+ cmds.unregisterCommand = ephemeralSubCommand {
12398 name = Translations .Commands .Event .Unregister .name
12499 description = Translations .Commands .Event .Unregister .description
125100
@@ -161,6 +136,74 @@ class EventCommands : Extension(), KordExKoinComponent {
161136 }
162137 }
163138
139+ suspend inline fun TranslatableContext.doRegister (respond : (InteractionResponseCreateBuilder .() -> Unit ) -> EphemeralMessageInteractionResponseBehavior , ackBlank : () -> EphemeralMessageInteractionResponseBehavior , sendModal : (ModalForm ) -> EphemeralMessageInteractionResponseBehavior ? , user : User ) {
140+ val curEvent = platform.getCurrentEvent().event;
141+ if (curEvent == null ) {
142+ val c = Translations .Commands .Register .Response .noevent
143+ .withContext(this @doRegister)
144+ .translateNamed()
145+ respond {
146+ content = c
147+ }
148+ return // Do *not* try to send any modals, we've already replied
149+ }
150+
151+ var platformUser = platform.getUser(user)
152+
153+ var message = if (platformUser == null ) {
154+ val modal = RegisterModal ()
155+
156+ modal.displayName.initialValue = user.asMember(MAIN_GUILD_ID ).effectiveName.toKey(Locale .UK )
157+ modal.displayName.translateInitialValue = false
158+
159+ val result = sendModal(modal)
160+
161+ // Result will be null if the user didn't enter anything
162+ // or if the modal timed out
163+ if (result == null ) {
164+ return
165+ }
166+
167+ platformUser = platform.authenticatedAsBotFest().createUser(UserCreateData (
168+ modal.displayName.value,
169+ modal.pronouns.value,
170+ modal.modrinthSlug.value,
171+ user.id.value.toString()
172+ ))
173+
174+ result
175+ } else {
176+ // User already has their data registered in platform
177+ // Ack immediately: we only have 5 seconds for any ack
178+ ackBlank()
179+ }
180+
181+ // Okay, we now have a user that's registered. And we have a
182+ // message we can edit to respond to the user
183+ // let's perform the actual registration
184+ val event = platform.getEvent(curEvent)
185+
186+ if (platformUser.registered.contains(curEvent)) {
187+ message.edit {
188+ content = Translations .Commands .Register .Response .already
189+ .withContext(this @doRegister)
190+ .translateNamed(
191+ " event" to event.name
192+ )
193+ }
194+ return
195+ }
196+
197+ platform.withAuth(user).registerMe(event)
198+ message.edit {
199+ content = Translations .Commands .Register .Response .success
200+ .withContext(this @doRegister)
201+ .translateNamed(
202+ " event" to event.name
203+ )
204+ }
205+ }
206+
164207 class RegisterModal : ModalForm () {
165208 override var title: Key = Translations .Modal .Register .title
166209
0 commit comments