-
Notifications
You must be signed in to change notification settings - Fork 30
/
util.go
58 lines (47 loc) · 1.24 KB
/
util.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
package util
import (
"crypto/sha256"
"fmt"
"mime"
"strconv"
"github.com/merlinfuchs/discordgo"
"github.com/spf13/viper"
)
func Ptr[T any](val T) *T {
return &val
}
func BotInviteURL() string {
return fmt.Sprintf("https://discord.com/oauth2/authorize?client_id=%s&scope=bot%%20applications.commands&permissions=536871936", viper.GetString("discord.client_id"))
}
func HashBytes(b []byte) string {
hasher := sha256.New()
hasher.Write(b)
return fmt.Sprintf("%x", hasher.Sum(nil))
}
func GetFileExtensionFromMimeType(mimeType string) string {
res, err := mime.ExtensionsByType(mimeType)
if err != nil || len(res) == 0 {
return ""
}
return res[0]
}
func IsDiscordRestErrorCode(err error, codes ...int) bool {
if err, ok := err.(*discordgo.RESTError); ok {
if err.Message == nil {
return false
}
for _, code := range codes {
if err.Message.Code == code {
return true
}
}
}
return false
}
func DiscordAvatarURL(id string, discriminator string, avatar string) string {
if avatar == "" {
parsedDiscriminator, _ := strconv.Atoi(discriminator)
return fmt.Sprintf("https://cdn.discordapp.com/embed/avatars/%d.png", parsedDiscriminator%5)
}
return fmt.Sprintf("https://cdn.discordapp.com/avatars/%s/%s.png", id, avatar)
}