-
Notifications
You must be signed in to change notification settings - Fork 91
/
common.go
85 lines (74 loc) · 1.99 KB
/
common.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
package main
import (
"fmt"
"path"
"strings"
"github.com/bwmarrin/discordgo"
)
// isDiscordEmoji matches https://cdn.discordapp.com/emojis/503141595860959243.gif, and similar URLs/filenames
func isDiscordEmoji(link string) bool {
// always match discord emoji URLs, eg https://cdn.discordapp.com/emojis/340989430460317707.png
if strings.HasPrefix(link, discordEmojiBaseUrl) {
return true
}
return false
}
// deduplicateDownloadItems removes duplicates from a slice of *DownloadItem s identified by the Link
func deduplicateDownloadItems(DownloadItems []*DownloadItem) []*DownloadItem {
var result []*DownloadItem
seen := map[string]bool{}
for _, item := range DownloadItems {
if seen[item.Link] {
continue
}
seen[item.Link] = true
result = append(result, item)
}
return result
}
func updateDiscordStatus() {
if StatusEnabled {
dg.UpdateStatusComplex(discordgo.UpdateStatusData{
Game: &discordgo.Game{
Name: fmt.Sprintf("%d %s", countDownloadedImages(), StatusSuffix),
Type: StatusLabel,
},
Status: StatusType,
})
} else if StatusType != "online" {
dg.UpdateStatusComplex(discordgo.UpdateStatusData{
Status: StatusType,
})
}
}
func Pagify(text string, delimiter string) []string {
result := make([]string, 0)
textParts := strings.Split(text, delimiter)
currentOutputPart := ""
for _, textPart := range textParts {
if len(currentOutputPart)+len(textPart)+len(delimiter) <= 1992 {
if len(currentOutputPart) > 0 || len(result) > 0 {
currentOutputPart += delimiter + textPart
} else {
currentOutputPart += textPart
}
} else {
result = append(result, currentOutputPart)
currentOutputPart = ""
if len(textPart) <= 1992 {
currentOutputPart = textPart
}
}
}
if currentOutputPart != "" {
result = append(result, currentOutputPart)
}
return result
}
func filepathExtension(filepath string) string {
if strings.Contains(filepath, "?") {
filepath = strings.Split(filepath, "?")[0]
}
filepath = path.Ext(filepath)
return filepath
}