@@ -9,14 +9,15 @@ import Foundation
99import DiscordKitCore
1010
1111/// Provides methods to get parameters of and respond to application command interactions
12- public struct CommandData {
12+ public class CommandData {
1313 internal init (
1414 optionValues: [ OptionData ] ,
15- rest: DiscordREST , token : String , interactionID: Snowflake
15+ rest: DiscordREST , applicationID : String , interactionID: Snowflake , token : String
1616 ) {
1717 self . rest = rest
1818 self . token = token
1919 self . interactionID = interactionID
20+ self . applicationID = applicationID
2021
2122 self . optionValues = Self . unwrapOptionDatas ( optionValues)
2223 }
@@ -27,9 +28,15 @@ public struct CommandData {
2728 /// instance gets deallocated.
2829 private weak var rest : DiscordREST ?
2930
31+ /// The ID of this bot application
32+ private let applicationID : String
33+
3034 /// Values of options in this command
3135 private let optionValues : [ String : OptionData ]
3236
37+ /// If this reply has already been deferred
38+ fileprivate var replyDeferred = false
39+
3340 // MARK: Parameters for executing callbacks
3441 /// The token to use when carrying out actions with this interaction
3542 let token : String
@@ -82,14 +89,40 @@ public extension CommandData {
8289
8390// MARK: - Callback APIs
8491public extension CommandData {
92+ /// Wrapper function to send an interaction response with the current interaction's ID and token
93+ private func sendResponse( _ response: InteractionResponse ) async throws {
94+ try await rest? . sendInteractionResponse ( response, interactionID: interactionID, token: token)
95+ }
96+
8597 /// Reply to this interaction with a plain text response
98+ ///
99+ /// If a prior call to ``deferReply()`` was made, this function automatically
100+ /// calls ``followUp(_:)`` instead.
86101 func reply( _ content: String ) async throws {
87- try await rest? . sendInteractionResponse (
102+ if replyDeferred {
103+ _ = try await followUp ( content)
104+ return
105+ }
106+ try await sendResponse (
88107 . init(
89108 type: . interactionReply,
90109 data: . message( . init( content: content) )
91- ) ,
92- interactionID: interactionID, token: token
110+ )
93111 )
94112 }
113+
114+ /// Send a follow up response to this interaction
115+ ///
116+ /// By default, this creates a second reply to this interaction, appearing as a
117+ /// reply in clients. However, if a call to ``deferReply()`` was made, this
118+ /// edits the loading message with the content provided.
119+ func followUp( _ content: String ) async throws -> Message {
120+ try await rest!. sendInteractionFollowUp ( . init( content: content) , applicationID: applicationID, token: token)
121+ }
122+
123+ /// Defer the reply to this interaction - the user sees a loading state
124+ func deferReply( ) async throws {
125+ try await sendResponse ( . init( type: . deferredInteractionReply, data: nil ) )
126+ replyDeferred = true
127+ }
95128}
0 commit comments