This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelpers.go
244 lines (207 loc) · 5.65 KB
/
helpers.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package dstate
import (
"time"
"github.com/jonas747/discordgo"
)
func GuildSetFromGuild(guild *discordgo.Guild) *GuildSet {
channels := make([]ChannelState, 0, len(guild.Channels))
for _, v := range guild.Channels {
channels = append(channels, ChannelStateFromDgo(v))
}
roles := make([]discordgo.Role, len(guild.Roles))
for i := range guild.Roles {
roles[i] = *guild.Roles[i]
}
emojis := make([]discordgo.Emoji, len(guild.Emojis))
for i := range guild.Emojis {
emojis[i] = *guild.Emojis[i]
}
voiceStates := make([]discordgo.VoiceState, len(guild.Emojis))
for i := range guild.VoiceStates {
voiceStates[i] = *guild.VoiceStates[i]
}
return &GuildSet{
GuildState: *GuildStateFromDgo(guild),
Channels: channels,
Roles: roles,
Emojis: emojis,
VoiceStates: voiceStates,
}
}
func MessageStateFromDgo(m *discordgo.Message) *MessageState {
var embeds []discordgo.MessageEmbed
if len(m.Embeds) > 0 {
embeds = make([]discordgo.MessageEmbed, len(m.Embeds))
for i, v := range m.Embeds {
embeds[i] = *v
}
}
var mentions []discordgo.User
if len(m.Mentions) > 0 {
mentions = make([]discordgo.User, len(m.Mentions))
for i, v := range m.Mentions {
mentions[i] = *v
}
}
var attachments []discordgo.MessageAttachment
if len(m.Attachments) > 0 {
attachments = make([]discordgo.MessageAttachment, len(m.Attachments))
for i, v := range m.Attachments {
attachments[i] = *v
}
}
var author discordgo.User
if m.Author != nil {
author = *m.Author
}
parsedC, _ := m.Timestamp.Parse()
var parsedE time.Time
if m.EditedTimestamp != "" {
parsedE, _ = m.EditedTimestamp.Parse()
}
return &MessageState{
ID: m.ID,
GuildID: m.GuildID,
ChannelID: m.ChannelID,
Author: author,
Member: m.Member,
Content: m.Content,
Embeds: embeds,
Mentions: mentions,
Attachments: attachments,
MentionRoles: m.MentionRoles,
ParsedCreatedAt: parsedC,
ParsedEditedAt: parsedE,
}
}
func MemberStateFromPresence(p *discordgo.PresenceUpdate) *MemberState {
var user discordgo.User
if p.User != nil {
user = *p.User
}
// get the main activity
// it either gets the first one, or the one with typ 1 (streaming)
var mainActivity *discordgo.Game
for i, v := range p.Activities {
if i == 0 || v.Type == 1 {
mainActivity = v
}
}
var lg *LightGame
if mainActivity != nil {
lg = &LightGame{
Name: mainActivity.Name,
Details: mainActivity.Details,
URL: mainActivity.URL,
State: mainActivity.State,
Type: mainActivity.Type,
}
}
// update the rest
var status PresenceStatus
switch p.Status {
case discordgo.StatusOnline:
status = StatusOnline
case discordgo.StatusIdle:
status = StatusIdle
case discordgo.StatusDoNotDisturb:
status = StatusDoNotDisturb
case discordgo.StatusInvisible:
status = StatusInvisible
case discordgo.StatusOffline:
status = StatusOffline
}
return &MemberState{
User: user,
GuildID: p.GuildID,
Member: nil,
Presence: &PresenceFields{
Game: lg,
Status: status,
},
}
}
func ChannelStateFromDgo(c *discordgo.Channel) ChannelState {
pos := make([]discordgo.PermissionOverwrite, len(c.PermissionOverwrites))
for i, v := range c.PermissionOverwrites {
pos[i] = *v
}
return ChannelState{
ID: c.ID,
GuildID: c.GuildID,
PermissionOverwrites: pos,
ParentID: c.ParentID,
Name: c.Name,
Topic: c.Topic,
Type: c.Type,
NSFW: c.NSFW,
Position: c.Position,
Bitrate: c.Bitrate,
}
}
func GuildStateFromDgo(guild *discordgo.Guild) *GuildState {
if guild.Unavailable {
return &GuildState{
ID: guild.ID,
Available: false,
}
}
return &GuildState{
ID: guild.ID,
Available: true,
Region: guild.Region,
MemberCount: int64(guild.MemberCount),
OwnerID: guild.OwnerID,
Name: guild.Name,
Icon: guild.Icon,
Description: guild.Description,
PreferredLocale: guild.PreferredLocale,
AfkChannelID: guild.AfkChannelID,
EmbedChannelID: guild.EmbedChannelID,
Splash: guild.Splash,
AfkTimeout: guild.AfkTimeout,
VerificationLevel: guild.VerificationLevel,
EmbedEnabled: guild.EmbedEnabled,
Large: guild.Large,
DefaultMessageNotifications: guild.DefaultMessageNotifications,
MaxPresences: guild.MaxPresences,
MaxMembers: guild.MaxMembers,
Unavailable: guild.Unavailable,
ExplicitContentFilter: guild.ExplicitContentFilter,
Features: guild.Features,
MfaLevel: guild.MfaLevel,
WidgetEnabled: guild.WidgetEnabled,
WidgetChannelID: guild.WidgetChannelID,
SystemChannelID: guild.SystemChannelID,
}
}
func IsRoleAbove(a, b *discordgo.Role) bool {
if a.Position != b.Position {
return a.Position > b.Position
}
if a.ID == b.ID {
return false
}
return a.ID < b.ID
}
// Channels are a collection of Channels
type Channels []ChannelState
func (r Channels) Len() int {
return len(r)
}
func (r Channels) Less(i, j int) bool {
return r[i].Position < r[j].Position
}
func (r Channels) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
type Roles []discordgo.Role
func (r Roles) Len() int {
return len(r)
}
func (r Roles) Less(i, j int) bool {
return IsRoleAbove(&r[i], &r[j])
}
func (r Roles) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}