This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
319 lines (245 loc) · 6.85 KB
/
utils.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
package main
import (
"fmt"
"log"
"regexp"
"strconv"
"time"
"github.com/bwmarrin/discordgo"
)
const (
EMOJI_SUCESS = "✅"
EMOJI_ERROR = "❌"
EMOJI_PLAYLIST = "💽"
EMOJI_SONG = "🎵"
)
const (
spotifyLogoURL = "https://www.freepnglogos.com/uploads/spotify-logo-png/spotify-icon-marilyn-scott-0.png"
youtubeLogoURL = "https://logodownload.org/wp-content/uploads/2014/10/youtube-logo-5-2-1536x1073.png"
)
const (
COLOR_ERROR int = 16538725
COLOR_WARNING = 16700208
COLOR_INFO = 4565746
COLOR_SUCCESS = 2547329
COLOR_SPOTIFY = 1947988
COLOR_YOUTUBE = 16711680
)
const (
ERROR_MSG string = "❌ | Something went wrong, try again."
WARN_INVALID_INPUT = "⚠️ | Please check your input."
WARN_NO_PERMISSION = "⚠️ | You don't have permission."
SUCCESS_MSG = "✅ | Command performed."
)
func CustomTip(content string) string {
return fmt.Sprintf("🔎 TIP | %s", content)
}
func CustomSuccess(content string) string {
return fmt.Sprintf("✅ | %s", content)
}
func CustomWarning(content string) string {
return fmt.Sprintf("✅ | %s", content)
}
func CustomInfo(content string) string {
return fmt.Sprintf("ℹ️ | %s", content)
}
func CustomError(content string) string {
return fmt.Sprintf("❌ | %s", content)
}
const ephemeralTime uint8 = 20
func (client *Client) CommandEmbedAnswer(message *discordgo.MessageCreate, title string, description string, color int) {
response := discordgo.MessageEmbed{
Type: discordgo.EmbedTypeRich,
Title: title,
Description: description,
Color: color,
}
msg, err := client.Session.ChannelMessageSendEmbed(message.ChannelID, &response)
if err != nil {
log.Println("Failed to send message!")
} else {
go func() {
time.Sleep(time.Duration(ephemeralTime) * time.Second)
client.Session.ChannelMessageDelete(message.ChannelID, message.ID)
client.Session.ChannelMessageDelete(msg.ChannelID, msg.ID)
}()
}
}
func (client *Client) MessageInteraction(message *discordgo.MessageCreate, title string, color int) {
response := discordgo.MessageEmbed{
Type: discordgo.EmbedTypeRich,
Title: title,
Color: color,
}
msg, err := client.Session.ChannelMessageSendEmbed(message.ChannelID, &response)
if err != nil {
log.Println("Failed to send message!")
} else {
go func() {
time.Sleep(time.Duration(ephemeralTime) * time.Second)
client.Session.ChannelMessageDelete(msg.ChannelID, msg.ID)
client.Session.ChannelMessageDelete(message.ChannelID, message.ID)
}()
}
}
func (client *Client) EventMessage(title string, color int) {
response := discordgo.MessageEmbed{
Type: discordgo.EmbedTypeRich,
Title: title,
Color: color,
}
msg, err := client.Session.ChannelMessageSendEmbed(client.AnchorTextChannel, &response)
if err != nil {
log.Println("Failed to send message!")
} else {
go func() {
time.Sleep(time.Duration(ephemeralTime) * time.Second)
client.Session.ChannelMessageDelete(msg.ChannelID, msg.ID)
}()
}
}
func (client *Client) DefautlFooter(message *discordgo.MessageCreate) *discordgo.MessageEmbedFooter {
guild, _ := client.Session.Guild(message.GuildID)
return &discordgo.MessageEmbedFooter{
Text: guild.Name,
IconURL: guild.IconURL(),
}
}
func (client *Client) DeleteAfterEphemeralTime(message *discordgo.MessageCreate) {
go func() {
time.Sleep(time.Duration(ephemeralTime) * time.Second)
client.Session.ChannelMessageDelete(message.ChannelID, message.ID)
}()
}
func CurrentTimestamp() string {
return time.Now().Format(time.RFC3339)
}
func min(a, b int) int {
if a > b {
return b
} else {
return a
}
}
func max(a, b int) int {
if a > b {
return a
} else {
return b
}
}
func ceil(a, b int) int {
if a%b == 0 {
return a / b
} else {
return (a / b) + 1
}
}
func (client *Client) CheckPermissionsForUser(message *discordgo.MessageCreate) (PermissionType, *discordgo.VoiceState, *discordgo.VoiceState) {
guild, err := client.Session.Guild(message.GuildID)
if err != nil {
return PERM_ERROR, nil, nil
}
err = client.Session.State.GuildAdd(guild)
if err != nil {
return PERM_ERROR, nil, nil
}
var liveGuild *discordgo.Guild
for _, g := range client.Session.State.Guilds {
if g.ID == guild.ID {
liveGuild = g
break
}
}
var userVoiceState *discordgo.VoiceState = nil
var botVoiceState *discordgo.VoiceState = nil
for _, v := range liveGuild.VoiceStates {
if v.UserID == message.Author.ID {
userVoiceState = v
} else if v.UserID == client.BotConfig.BotID {
botVoiceState = v
}
}
if userVoiceState == nil {
return PERM_NOT_IN_VC, nil, nil
}
if botVoiceState != nil && botVoiceState.ChannelID != userVoiceState.ChannelID {
return PERM_WRONG_VC, nil, nil
}
return PERM_IN_VC, userVoiceState, botVoiceState
}
func ProgressBar(current, total uint) string {
var progress uint = (current * 100) / total
var i uint
var bar string = ""
emptyProgress := total - progress
for i = 0; i < progress; i++ {
bar = bar + "▬"
}
bar = bar + "🔵"
for i = 0; i < emptyProgress; i++ {
bar = bar + "▬"
}
return bar
}
func DurationToMS(durationStr string) (int, error) {
daysPattern := regexp.MustCompile("^([0-9]+):(\\d?\\d):(\\d?\\d):(\\d?\\d)$")
hoursPattern := regexp.MustCompile("^(\\d?\\d):(\\d?\\d):(\\d?\\d)$")
minutesPattern := regexp.MustCompile("^(\\d?\\d):(\\d?\\d)$")
secondsPattern := regexp.MustCompile("^(\\d?\\d)$")
regexDays := daysPattern.FindStringSubmatch(durationStr)
regexHours := hoursPattern.FindStringSubmatch(durationStr)
regexMinutes := minutesPattern.FindStringSubmatch(durationStr)
regexSeconds := secondsPattern.FindStringSubmatch(durationStr)
if len(regexDays) > 0 {
days, err := strconv.Atoi(regexDays[1])
if err != nil {
return 0, err
}
hours, err := strconv.Atoi(regexDays[2])
if err != nil {
return 0, err
}
minutes, err := strconv.Atoi(regexDays[3])
if err != nil {
return 0, err
}
seconds, err := strconv.Atoi(regexDays[4])
if err != nil {
return 0, err
}
return ((days * 86400000) + (hours * 3600000) + (minutes * 60000) + (seconds * 1000)), nil
} else if len(regexHours) > 0 {
hours, err := strconv.Atoi(regexHours[1])
if err != nil {
return 0, err
}
minutes, err := strconv.Atoi(regexHours[2])
if err != nil {
return 0, err
}
seconds, err := strconv.Atoi(regexHours[3])
if err != nil {
return 0, err
}
return ((hours * 3600000) + (minutes * 60000) + (seconds * 1000)), nil
} else if len(regexMinutes) > 0 {
minutes, err := strconv.Atoi(regexMinutes[1])
if err != nil {
return 0, err
}
seconds, err := strconv.Atoi(regexMinutes[2])
if err != nil {
return 0, err
}
return ((minutes * 60000) + (seconds * 1000)), nil
} else if len(regexSeconds) > 0 {
seconds, err := strconv.Atoi(regexSeconds[1])
if err != nil {
return 0, err
}
return (seconds * 1000), nil
} else {
return -1, nil
}
}