-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.go
More file actions
149 lines (122 loc) · 3.72 KB
/
utilities.go
File metadata and controls
149 lines (122 loc) · 3.72 KB
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
package main
import (
"context"
"math/rand"
"strings"
"time"
"github.com/bwmarrin/lit"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/snowflake/v2"
)
// findUserVoiceState finds user current voice channel
func findUserVoiceState(s *bot.Client, guildID, userID snowflake.ID) *discord.VoiceState {
v, found := s.Caches.VoiceState(guildID, userID)
if !found {
return nil
}
return &v
}
// advancedReplace returns src string with every instance of toReplace with a random item from a
func advancedReplace(src string, toReplace string, a []string) string {
var dst = src
for i := 0; i < strings.Count(src, toReplace); i++ {
dst = strings.Replace(dst, toReplace, a[rand.Intn(len(a))], 1)
}
return dst
}
// Returns a random value from a map of string
func getRand(a map[string]string) string {
// produce a pseudo-random number between 0 and len(a)-1
i := int(float32(len(a)) * rand.Float32())
for _, v := range a {
if i == 0 {
return v
}
i--
}
panic("impossible")
}
// Initialize server for a given guildID if its nil
func initializeServer(guildID snowflake.ID) {
if server[guildID] == nil {
server[guildID] = NewServer(guildID)
}
}
// Sends embed as response to an interaction
func sendEmbedInteraction(embed discord.Embed, e *events.ApplicationCommandInteractionCreate, c chan<- struct{}) {
err := e.CreateMessage(discord.NewMessageCreate().AddEmbeds(embed))
if err != nil {
lit.Error("InteractionRespond failed: %s", err)
return
}
if c != nil {
c <- struct{}{}
}
}
// Sends and delete after three second an embed in a given channel
func sendAndDeleteEmbedInteraction(embed discord.Embed, e *events.ApplicationCommandInteractionCreate, wait time.Duration) {
sendEmbedInteraction(embed, e, nil)
time.Sleep(wait)
err := e.Client().Rest.DeleteInteractionResponse(e.ApplicationID(), e.Token())
if err != nil {
lit.Error("InteractionResponseDelete failed: %s", err)
return
}
}
func sendEmbed(c *bot.Client, embed discord.Embed, txtChannel snowflake.ID) *discord.Message {
m, err := c.Rest.CreateMessage(txtChannel, discord.NewMessageCreate().AddEmbeds(embed))
if err != nil {
lit.Error("sendEmbed failed: %s", err)
return nil
}
return m
}
// joinVC joins the voice channel if not already joined, returns true if joined successfully
func joinVC(e *events.ApplicationCommandInteractionCreate, channelID, guildID snowflake.ID) bool {
if server[guildID].vc == nil {
// Create the voice connection
server[guildID].vc = e.Client().VoiceManager.CreateConn(guildID)
}
if server[guildID].voiceChannel == nil {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
errCh := make(chan error, 1)
go func() {
// Join the voice channel
errCh <- server[guildID].vc.Open(ctx, channelID, false, true)
}()
var err error
select {
case err = <-errCh:
case <-ctx.Done():
err = ctx.Err()
}
if err != nil {
sendAndDeleteEmbedInteraction(discord.NewEmbed().WithTitle(BotName).AddField(errorTitle, cantJoinVC, false).
WithColor(0x7289DA), e, time.Second*5)
return false
}
server[guildID].voiceChannel = &channelID
}
return true
}
// Disconnects the bot from the voice channel
func quitVC(guildID snowflake.ID) {
if server[guildID].queue.IsEmpty() && server[guildID].voiceChannel != nil {
server[guildID].vc.Close(context.TODO())
server[guildID].voiceChannel = nil
server[guildID].vc = nil
}
}
func deleteInteraction(e *events.ApplicationCommandInteractionCreate, c <-chan struct{}) {
if c != nil {
<-c
}
err := e.Client().Rest.DeleteInteractionResponse(e.ApplicationID(), e.Token())
if err != nil {
lit.Error("DeleteInteractionResponse failed: %s", err)
return
}
}