-
Notifications
You must be signed in to change notification settings - Fork 1
/
ws.go
82 lines (73 loc) · 3.03 KB
/
ws.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
package guildedgo
import (
"bytes"
"encoding/json"
"log"
)
var interfaces = make(map[string]any)
func init() {
interfaces["BotServerMembershipCreated"] = &BotServerMembershipCreated{}
interfaces["BotServerMembershipDeleted"] = &BotServerMembershipDeleted{}
interfaces["ChatMessageCreated"] = &ChatMessageCreated{}
interfaces["ChatMessageUpdated"] = &ChatMessageUpdated{}
interfaces["ChatMessageDeleted"] = &ChatMessageDeleted{}
interfaces["ServerMemberJoined"] = &ServerMemberJoined{}
interfaces["ServerMemberRemoved"] = &ServerMemberRemoved{}
interfaces["ServerMemberBanned"] = &ServerMemberBanned{}
interfaces["ServerMemberUnbanned"] = &ServerMemberUnbanned{}
interfaces["ServerMemberUpdated"] = &ServerMemberUpdated{}
interfaces["ServerRolesUpdated"] = &ServerRolesUpdated{}
interfaces["ServerChannelCreated"] = &ServerChannelCreated{}
interfaces["ServerChannelUpdated"] = &ServerChannelUpdated{}
interfaces["ServerChannelDeleted"] = &ServerChannelDeleted{}
interfaces["ServerWebhookCreated"] = &ServerWebhookCreated{}
interfaces["ServerWebhookUpdated"] = &ServerWebhookUpdated{}
interfaces["ChannelArchived"] = &ChannelArchived{}
interfaces["ChannelRestored"] = &ChannelRestored{}
interfaces["DocCreated"] = &DocCreated{}
interfaces["DocUpdated"] = &DocUpdated{}
interfaces["DocDeleted"] = &DocDeleted{}
interfaces["CalendarEventCreated"] = &CalendarEventCreated{}
interfaces["CalendarEventUpdated"] = &CalendarEventUpdated{}
interfaces["CalendarEventDeleted"] = &CalendarEventDeleted{}
interfaces["ForumTopicCreated"] = &ForumTopicCreated{}
interfaces["ForumTopicUpdated"] = &ForumTopicUpdated{}
interfaces["ForumTopicDeleted"] = &ForumTopicDeleted{}
interfaces["ForumTopicPinned"] = &ForumTopicPinned{}
interfaces["ForumTopicUnpinned"] = &ForumTopicUnpinned{}
interfaces["ForumTopicReactionCreated"] = &ForumTopicReactionCreated{}
interfaces["ForumTopicReactionDeleted"] = &ForumTopicReactionDeleted{}
interfaces["ForumTopicLocked"] = &ForumTopic{}
interfaces["ForumTopicUnlocked"] = &ForumTopic{}
interfaces["ForumTopicCommentCreated"] = &ForumTopicComment{}
interfaces["ForumTopicCommentUpdated"] = &ForumTopicComment{}
interfaces["ForumTopicCommentDeleted"] = &ForumTopicComment{}
interfaces["CalendarEventRsvpUpdated"] = &CalendarEventRsvp{}
interfaces["CalendarEventRsvpManyUpdated"] = &[]CalendarEventRsvp{}
interfaces["CalendarEventRsvpDeleted"] = &CalendarEventRsvp{}
}
type RawEvent struct {
T string `json:"t"`
S string `json:"s"`
Data json.RawMessage `json:"d"`
}
func (c *Client) onEvent(msg []byte) {
var err error
reader := bytes.NewBuffer(msg)
var re *RawEvent
decoder := json.NewDecoder(reader)
err = decoder.Decode(&re)
if err != nil {
log.Println("Failed to decode raw event")
}
eventType := interfaces[re.T]
err = json.Unmarshal(re.Data, eventType)
if err != nil {
log.Printf("Failed to unmarshal event data for %q. Error: %s", re.T, err.Error())
}
// Is this smart? Probably not.
eventsCB := c.events[re.T]
for _, event := range eventsCB {
event.Callback(c, eventType)
}
}