-
Notifications
You must be signed in to change notification settings - Fork 35
/
integration.go
197 lines (165 loc) · 5.18 KB
/
integration.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package discord
import (
"fmt"
"time"
"github.com/disgoorg/disgo/json"
"github.com/disgoorg/snowflake/v2"
)
// IntegrationType the type of Integration
type IntegrationType string
// All IntegrationType(s)
const (
IntegrationTypeTwitch IntegrationType = "twitch"
IntegrationTypeYouTube IntegrationType = "youtube"
IntegrationTypeBot IntegrationType = "discord"
)
// IntegrationAccount (https://discord.com/developers/docs/resources/guild#integration-account-object)
type IntegrationAccount struct {
ID string `json:"id"`
Name string `json:"name"`
}
// IntegrationApplication (https://discord.com/developers/docs/resources/guild#integration-application-object)
type IntegrationApplication struct {
ID snowflake.ID `json:"id"`
Name string `json:"name"`
Icon string `json:"icon"`
Description string `json:"description"`
Summary string `json:"summary"`
Client User `json:"bot"`
}
// Integration (https://discord.com/developers/docs/resources/guild#integration-object)
type Integration interface {
json.Marshaler
Type() IntegrationType
ID() snowflake.ID
CreatedAt() time.Time
}
type UnmarshalIntegration struct {
Integration
}
func (i *UnmarshalIntegration) UnmarshalJSON(data []byte) error {
var cType struct {
Type IntegrationType `json:"type"`
}
if err := json.Unmarshal(data, &cType); err != nil {
return err
}
var (
integration Integration
err error
)
switch cType.Type {
case IntegrationTypeTwitch:
var v TwitchIntegration
err = json.Unmarshal(data, &v)
integration = v
case IntegrationTypeYouTube:
var v YouTubeIntegration
err = json.Unmarshal(data, &v)
integration = v
case IntegrationTypeBot:
var v BotIntegration
err = json.Unmarshal(data, &v)
integration = v
default:
err = fmt.Errorf("unkown integration with type %s received", cType.Type)
}
if err != nil {
return err
}
i.Integration = integration
return nil
}
type TwitchIntegration struct {
IntegrationID snowflake.ID `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
Syncing bool `json:"syncing"`
RoleID snowflake.ID `json:"role_id"`
EnableEmoticons bool `json:"enable_emoticons"`
ExpireBehavior int `json:"expire_behavior"`
ExpireGracePeriod int `json:"expire_grace_period"`
User User `json:"user"`
Account IntegrationAccount `json:"account"`
SyncedAt string `json:"synced_at"`
SubscriberCount int `json:"subscriber_account"`
Revoked bool `json:"revoked"`
}
func (i TwitchIntegration) MarshalJSON() ([]byte, error) {
type twitchIntegration TwitchIntegration
return json.Marshal(struct {
Type IntegrationType `json:"type"`
twitchIntegration
}{
Type: i.Type(),
twitchIntegration: twitchIntegration(i),
})
}
func (TwitchIntegration) Type() IntegrationType {
return IntegrationTypeTwitch
}
func (i TwitchIntegration) ID() snowflake.ID {
return i.IntegrationID
}
func (i TwitchIntegration) CreatedAt() time.Time {
return i.IntegrationID.Time()
}
type YouTubeIntegration struct {
IntegrationID snowflake.ID `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
Syncing bool `json:"syncing"`
RoleID snowflake.ID `json:"role_id"`
ExpireBehavior int `json:"expire_behavior"`
ExpireGracePeriod int `json:"expire_grace_period"`
User User `json:"user"`
Account IntegrationAccount `json:"account"`
SyncedAt string `json:"synced_at"`
SubscriberCount int `json:"subscriber_account"`
Revoked bool `json:"revoked"`
}
func (i YouTubeIntegration) MarshalJSON() ([]byte, error) {
type youTubeIntegration YouTubeIntegration
return json.Marshal(struct {
Type IntegrationType `json:"type"`
youTubeIntegration
}{
Type: i.Type(),
youTubeIntegration: youTubeIntegration(i),
})
}
func (YouTubeIntegration) Type() IntegrationType {
return IntegrationTypeTwitch
}
func (i YouTubeIntegration) ID() snowflake.ID {
return i.IntegrationID
}
func (i YouTubeIntegration) CreatedAt() time.Time {
return i.IntegrationID.Time()
}
type BotIntegration struct {
IntegrationID snowflake.ID `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
Account IntegrationAccount `json:"account"`
Application IntegrationApplication `json:"application"`
}
func (i BotIntegration) MarshalJSON() ([]byte, error) {
type botIntegration BotIntegration
return json.Marshal(struct {
Type IntegrationType `json:"type"`
botIntegration
}{
Type: i.Type(),
botIntegration: botIntegration(i),
})
}
func (BotIntegration) Type() IntegrationType {
return IntegrationTypeBot
}
func (i BotIntegration) ID() snowflake.ID {
return i.IntegrationID
}
func (i BotIntegration) CreatedAt() time.Time {
return i.IntegrationID.Time()
}