/
client-handler.go
168 lines (144 loc) · 3.87 KB
/
client-handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package tempest
import (
"crypto/ed25519"
"encoding/json"
"io"
"net/http"
)
func (client *Client) HandleDiscordRequest(w http.ResponseWriter, r *http.Request) {
// Deprecated since Go v1.22 - Please specify http method when registering handler.
//
// if r.Method != http.MethodPost {
// http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
// return
// }
verified := verifyRequest(r, ed25519.PublicKey(client.PublicKey))
if !verified {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
buf, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
defer r.Body.Close()
var extractor InteractionTypeExtractor
err = json.Unmarshal(buf, &extractor)
if err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
switch extractor.Type {
case PING_INTERACTION_TYPE:
w.Header().Add("Content-Type", ContentTypeJSON)
w.Write(bodyPingResponse)
return
case APPLICATION_COMMAND_INTERACTION_TYPE:
var interaction CommandInteraction
err := json.Unmarshal(buf, &interaction)
if err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
itx, command, available := client.seekCommand(interaction)
if !available {
w.Header().Add("Content-Type", ContentTypeJSON)
w.Write(bodyUnknownCommandResponse)
return
}
w.WriteHeader(http.StatusNoContent)
if !command.AvailableInDM && interaction.GuildID == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
if client.preCommandHandler != nil && !client.preCommandHandler(command, &itx) {
return
}
command.SlashCommandHandler(&itx)
if client.postCommandHandler != nil {
client.postCommandHandler(command, &itx)
}
return
case MESSAGE_COMPONENT_INTERACTION_TYPE:
var itx ComponentInteraction
err := json.Unmarshal(buf, &itx)
if err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
itx.Client = client
fn, available := client.components[itx.Data.CustomID]
if available && fn != nil {
itx.w = w
fn(itx)
return
}
client.qMu.RLock()
signalChannel, available := client.queuedComponents[itx.Data.CustomID]
client.qMu.RUnlock()
if available && signalChannel != nil {
w.Header().Add("Content-Type", ContentTypeJSON)
w.Write(bodyAcknowledgeResponse)
signalChannel <- &itx
return
}
if client.componentHandler != nil {
itx.w = w
client.componentHandler(&itx)
}
return
case APPLICATION_COMMAND_AUTO_COMPLETE_INTERACTION_TYPE:
var interaction CommandInteraction
err := json.Unmarshal(buf, &interaction)
if err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
itx, command, available := client.seekCommand(interaction)
if !available || command.AutoCompleteHandler == nil || len(command.Options) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
choices := command.AutoCompleteHandler(itx)
body, err := json.Marshal(ResponseAutoComplete{
Type: AUTOCOMPLETE_RESPONSE_TYPE,
Data: &ResponseAutoCompleteData{
Choices: choices,
},
})
if err != nil {
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", ContentTypeJSON)
w.Write(body)
return
case MODAL_SUBMIT_INTERACTION_TYPE:
var itx ModalInteraction
err := json.Unmarshal(buf, &itx)
if err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
fn, available := client.modals[itx.Data.CustomID]
if available && fn != nil {
itx.w = w
fn(itx)
return
}
client.qMu.RLock()
signalChannel, available := client.queuedModals[itx.Data.CustomID]
client.qMu.RUnlock()
if available && signalChannel != nil {
w.Header().Add("Content-Type", ContentTypeJSON)
w.Write(bodyAcknowledgeResponse)
signalChannel <- &itx
}
if client.modalHandler != nil {
itx.w = w
client.modalHandler(&itx)
}
return
}
}