/
voice_update.go
361 lines (305 loc) · 10.1 KB
/
voice_update.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package channels
import (
"errors"
"fmt"
"sort"
"strconv"
"github.com/Haibread/godisco/database"
"github.com/Haibread/godisco/logging"
"github.com/Haibread/godisco/models"
"github.com/bwmarrin/discordgo"
"go.uber.org/zap"
"gorm.io/gorm"
)
var log *zap.SugaredLogger
func init() {
log = logging.InitLogger()
}
func userJoined(s *discordgo.Session, i *discordgo.VoiceStateUpdate) {
channel, err := s.State.Channel(i.ChannelID)
if err != nil {
log.Error(err)
return
}
if isChannelPrimary(s, i.VoiceState.ChannelID) {
_, err := createSecondaryChannelandMove(s, i, channel, i.VoiceState.UserID)
//_, err := createChildChannel(channel)
if err != nil {
log.Error(err)
}
}
}
func createSecondaryChannelandMove(s *discordgo.Session, i *discordgo.VoiceStateUpdate, parentChannel *discordgo.Channel, UserID string) (*discordgo.Channel, error) {
channel, err := s.State.Channel(parentChannel.ID)
if err != nil {
return nil, err
}
createdChannel, err := createSecondaryChannel(s, i, channel)
if err != nil {
return nil, err
}
err = s.GuildMemberMove(parentChannel.GuildID, UserID, &createdChannel.ID)
if err != nil {
return nil, err
}
return createdChannel, nil
}
func createSecondaryChannel(s *discordgo.Session, i *discordgo.VoiceStateUpdate, parentChannel *discordgo.Channel) (*discordgo.Channel, error) {
channelName, err := getChannelName(s, parentChannel, nil, i.UserID)
if err != nil {
return nil, err
}
channelToCreate := &discordgo.GuildChannelCreateData{
Name: channelName,
Type: discordgo.ChannelTypeGuildVoice,
Bitrate: parentChannel.Bitrate,
Position: parentChannel.Position - 1,
ParentID: parentChannel.ParentID,
PermissionOverwrites: parentChannel.PermissionOverwrites,
}
// Create the new channel
chanCreated, err := s.GuildChannelCreateComplex(parentChannel.GuildID, *channelToCreate)
if err != nil {
return nil, err
}
// Add channel in database.database.DB
database.DB.Create(&models.SecondaryChannel{Name: chanCreated.Name, ChannelID: chanCreated.ID, GuildID: chanCreated.GuildID, ParentChannelID: parentChannel.ID, CreatorID: i.UserID})
return chanCreated, nil
}
func userMoved(s *discordgo.Session, i *discordgo.VoiceStateUpdate) {
// Check if the channel is managed
if isChannelPrimary(s, i.BeforeUpdate.ChannelID) {
log.Debugf("Last channel %v was a primary channel, no action required", i.BeforeUpdate.ChannelID)
} else if isChannelPrimary(s, i.ChannelID) {
log.Debugf("Current channel %v is a primary channel, we need to create a new channel", i.ChannelID)
channel, err := s.Channel(i.ChannelID)
if err != nil {
log.Error(err)
return
}
_, err = createSecondaryChannelandMove(s, i, channel, i.UserID)
if err != nil {
log.Error(err)
}
}
// Check if the channel is in managed channel created
if isChannelSecondary(s, i.BeforeUpdate.ChannelID) {
log.Debugf("Last channel %v was a secondary channel, checking if empty", i.BeforeUpdate.ChannelID)
if isChannelEmpty(s, i.GuildID, i.BeforeUpdate.ChannelID) {
log.Debugf("Secondary channel %v is empty on guild %v, deleting it", i.BeforeUpdate.ChannelID, i.GuildID)
_, err := s.ChannelDelete(i.BeforeUpdate.ChannelID)
if err != nil {
log.Error(err)
}
log.Debugf("Removing secondary channel %v record from database.DB", i.BeforeUpdate.ChannelID)
database.DB.Unscoped().Where("channel_id = ?", i.BeforeUpdate.ChannelID).Delete(&models.SecondaryChannel{})
} else {
log.Debugf("Secondary channel %v is not empty, no actions required", i.BeforeUpdate.ChannelID)
}
} else if isChannelSecondary(s, i.ChannelID) {
log.Debugf("Current channel %v is a secondary channel, no actions required", i.ChannelID)
}
}
func isChannelEmpty(s *discordgo.Session, GuildID string, ChannelID string) bool {
count := 0
guild, err := s.State.Guild(GuildID)
if err != nil {
log.Error(err)
}
for _, channel := range guild.VoiceStates {
if channel.ChannelID == ChannelID {
count += 1
}
}
if count == 0 {
return true
} else {
return false
}
}
func isChannelPrimary(s *discordgo.Session, ChannelID string) bool {
var channel models.PrimaryChannel
managed_channel := database.DB.Select("channel_id").Where("channel_id = ?", ChannelID).First(&channel)
if managed_channel.Error != nil {
if errors.Is(managed_channel.Error, gorm.ErrRecordNotFound) {
//log.Debugf("database.DB Record for Channel ID \"%v\" has not been found", ChannelID)
} else {
log.Error(managed_channel.Error)
}
return false
}
if channel.ChannelID != "" {
return true
}
return false
}
func isChannelSecondary(s *discordgo.Session, ChannelID string) bool {
var channel models.SecondaryChannel
managed_channel := database.DB.Select("channel_id").Where("channel_id = ?", ChannelID).First(&channel)
if managed_channel.Error != nil {
if errors.Is(managed_channel.Error, gorm.ErrRecordNotFound) {
//log.Debugf("database.DB Record for Channel ID \"%v\" has not been found", ChannelID)
} else {
log.Error(managed_channel.Error)
}
return false
}
if channel.ChannelID != "" {
return true
}
return false
}
func getPrimaryChannelTemplate(s *discordgo.Session, ChannelID string) (string, error) {
var channel models.PrimaryChannel
managed_channel := database.DB.Select("name_template").Where("channel_id = ?", ChannelID).First(&channel)
if managed_channel.Error != nil {
if errors.Is(managed_channel.Error, gorm.ErrRecordNotFound) {
//log.Debugf("database.DB Record for Channel ID \"%v\" has not been found", ChannelID)
return "", nil
} else {
return "", fmt.Errorf("error while getting channel template: %v", managed_channel.Error)
}
}
if channel.NameTemplate != "" {
return channel.NameTemplate, nil
} else {
return "", fmt.Errorf("no template found for channel %v", ChannelID)
}
}
func getPrimaryChannelDefaultName(s *discordgo.Session, ChannelID string) (string, error) {
var channel models.PrimaryChannel
query := database.DB.Select("name_default").Where("channel_id = ?", ChannelID).First(&channel)
if query.Error != nil {
if errors.Is(query.Error, gorm.ErrRecordNotFound) {
//log.Debugf("database.DB Record for Channel ID \"%v\" has not been found", ChannelID)
return "", nil
} else {
return "", fmt.Errorf("error while getting channel default name: %v", query.Error)
}
}
if channel.NameDefault != "" {
return channel.NameDefault, nil
} else {
return "", fmt.Errorf("no default name found for channel %v", ChannelID)
}
}
func getSecondaryChannelRank(s *discordgo.Session, ParentChannelID string, ChannelID string) (int, error) {
// Get all secondary channels for the parent
var channels []models.SecondaryChannel
secondary_channels := database.DB.Select("channel_id").Where("parent_channel_id = ?", ParentChannelID).Find(&channels)
if secondary_channels.Error != nil {
if errors.Is(secondary_channels.Error, gorm.ErrRecordNotFound) {
return 1, nil
} else {
return 0, fmt.Errorf("error while getting secondary channel count : %v", secondary_channels.Error)
}
}
// Get all the channel_ids
var secondary_channel_ids []int
for _, channel := range channels {
int_channel_id, err := strconv.Atoi(channel.ChannelID)
if err != nil {
return 0, fmt.Errorf("error while converting channel ID to int: %v", err)
}
secondary_channel_ids = append(secondary_channel_ids, int_channel_id)
}
// Sort the channel id
sort.Ints(secondary_channel_ids)
// Count and compare
count := 0
found := false
for _, channel := range secondary_channel_ids {
var int_channel_id int
var err error
if ChannelID != "" {
int_channel_id, err = strconv.Atoi(ChannelID)
if err != nil {
return 0, fmt.Errorf("error while converting channel ID to int: %v", err)
}
} else {
int_channel_id = 0
}
// If found current channel_id, return value+1
if channel == int_channel_id {
found = true
return count + 1, nil
} else {
count += 1
}
}
if !found {
return count + 1, nil
}
return count, nil
}
func getChannelName(s *discordgo.Session, parentChannel *discordgo.Channel, secondaryChannel *discordgo.Channel, CreatorID string) (string, error) {
// Get channel rank
var channelrank int
var err error
if secondaryChannel == nil {
channelrank, err = getSecondaryChannelRank(s, parentChannel.ID, "")
} else {
channelrank, err = getSecondaryChannelRank(s, parentChannel.ID, secondaryChannel.ID)
}
if err != nil {
return "nil", err
}
// Get Template
channelTemplateName, err := getPrimaryChannelTemplate(s, parentChannel.ID)
if err != nil {
channelTemplateName = ""
}
var channel_tpl = &ChanneltoRename{}
if secondaryChannel == nil {
channel_tpl = &ChanneltoRename{
PrimaryChannel: parentChannel,
Creator: CreatorID,
Template: channelTemplateName,
Session: s,
Rank: channelrank,
}
} else if secondaryChannel != nil {
channel_tpl = &ChanneltoRename{
SecondaryChannel: secondaryChannel,
Creator: CreatorID,
Template: channelTemplateName,
Session: s,
Rank: channelrank,
}
} else {
return "nil", fmt.Errorf("error while getting channel type: %v", err)
}
var channelName string
// Get Name from template
if channel_tpl.Template != "" {
channelName, err = channel_tpl.getNamefromTemplate()
if err != nil {
return "nil", err
}
}
if channelName == "" {
channelDefaultName, err := getPrimaryChannelDefaultName(s, parentChannel.ID)
if err != nil {
return "nil", err
}
channelName = fmt.Sprintf("#%d %s", (channelrank)+1, channelDefaultName)
}
return channelName, nil
}
func CreatePrimaryChannel(s *discordgo.Session, GuildID string, NameTemplate string, NameDefault string) (*discordgo.Channel, error) {
channelToCreate := &discordgo.GuildChannelCreateData{
Name: "➕ New Channel",
Type: discordgo.ChannelTypeGuildVoice,
}
// Create the new channel
chanCreated, err := s.GuildChannelCreateComplex(GuildID, *channelToCreate)
if err != nil {
return nil, err
}
// Add channel in database.database.DB
query := database.DB.Create(&models.PrimaryChannel{NameTemplate: NameTemplate, NameDefault: NameDefault, ChannelID: chanCreated.ID, GuildID: GuildID})
if query.Error != nil {
return nil, query.Error
}
return chanCreated, nil
}