-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
211 lines (175 loc) · 5.12 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
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/fatih/color"
"github.com/hako/durafmt"
"github.com/hashicorp/go-version"
)
func uptime() time.Duration {
return time.Since(timeLaunched)
}
func properExit() {
// Not formatting string because I only want the exit message to be red.
dubLog("Main", logLevelInfo, color.HiRedString, "EXIT IN 15 SECONDS - Uptime was %s...", durafmt.Parse(time.Since(timeLaunched)).String())
dubLog("Main", logLevelInfo, color.HiCyanString, "---------------------------------------------------------------------")
time.Sleep(15 * time.Second)
os.Exit(1)
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if strings.ToLower(b) == strings.ToLower(a) {
return true
}
}
return false
}
//#region Requests
func getJSON(url string, target interface{}) error {
r, err := http.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(target)
}
func getJSONwithHeaders(url string, target interface{}, headers map[string]string) error {
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
for k, v := range headers {
req.Header.Set(k, v)
}
r, err := client.Do(req)
if err != nil {
return err
}
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(target)
}
//#endregion
//#region Github Release Checking
type githubReleaseApiObject struct {
TagName string `json:"tag_name"`
}
func isLatestGithubRelease() bool {
githubReleaseApiObject := new(githubReleaseApiObject)
err := getJSON(projectReleaseApiURL, githubReleaseApiObject)
if err != nil {
dubLog("Version", logLevelInfo, color.RedString, "Error fetching current Release JSON: %s", err)
return true
}
thisVersion, err := version.NewVersion(projectVersion)
if err != nil {
dubLog("Version", logLevelInfo, color.RedString, "Error parsing current version: %s", err)
return true
}
latestVersion, err := version.NewVersion(githubReleaseApiObject.TagName)
if err != nil {
dubLog("Version", logLevelInfo, color.RedString, "Error parsing latest version: %s", err)
return true
}
if latestVersion.GreaterThan(thisVersion) {
return false
}
return true
}
func shortenTime(input string) string {
input = strings.ReplaceAll(input, " nanoseconds", "ns")
input = strings.ReplaceAll(input, " nanosecond", "ns")
input = strings.ReplaceAll(input, " microseconds", "μs")
input = strings.ReplaceAll(input, " microsecond", "μs")
input = strings.ReplaceAll(input, " milliseconds", "ms")
input = strings.ReplaceAll(input, " millisecond", "ms")
input = strings.ReplaceAll(input, " seconds", "s")
input = strings.ReplaceAll(input, " second", "s")
input = strings.ReplaceAll(input, " minutes", "m")
input = strings.ReplaceAll(input, " minute", "m")
input = strings.ReplaceAll(input, " hours", "h")
input = strings.ReplaceAll(input, " hour", "h")
input = strings.ReplaceAll(input, " days", "d")
input = strings.ReplaceAll(input, " day", "d")
input = strings.ReplaceAll(input, " weeks", "w")
input = strings.ReplaceAll(input, " week", "w")
input = strings.ReplaceAll(input, " months", "mo")
input = strings.ReplaceAll(input, " month", "mo")
return input
}
//#endregion
/* logging system:
implement log leveling
*/
const (
logLevelOff = iota
logLevelFatal
logLevelError
logLevelWarning
logLevelInfo
logLevelDebug
logLevelVerbose
logLevelAll
)
func dubLog(group string, logLevel int, colorFunc func(string, ...interface{}) string, line string, p ...interface{}) {
colorPrefix := group
switch strings.ToLower(group) {
case "main":
colorPrefix = color.CyanString("[~]")
break
case "debug":
colorPrefix = color.HiYellowString("<DEBUG>")
break
case "test":
colorPrefix = color.HiYellowString("<TEST>")
break
case "info":
colorPrefix = color.CyanString("[Info]")
break
case "version":
colorPrefix = color.HiMagentaString("[Version]")
break
case "settings":
colorPrefix = color.GreenString("[Settings]")
break
case "setup":
colorPrefix = color.HiGreenString("[Setup]")
break
case "discord":
colorPrefix = color.HiBlueString("[Discord]")
break
case "spotify":
colorPrefix = color.HiGreenString("[Spotify]")
break
}
if logLevel <= config.LogLevel {
//TODO: trace file+line
log.Println(colorPrefix, colorFunc(line, p...))
}
if bot != nil && botReady {
for _, channelConfig := range config.OutputChannels {
if channelConfig.OutputProgram {
outputToChannel := func(channel string) {
if channel != "" {
if !hasPerms(channel, discordgo.PermissionSendMessages) {
dubLog("Log", logLevelError, color.HiRedString, fmtBotSendPerm, channel)
} else {
if _, err := bot.ChannelMessageSend(channel, fmt.Sprintf("```%s | [%s] %s```", time.Now().Format(time.RFC3339), group, fmt.Sprintf(line, p...))); err != nil {
dubLog("Log", logLevelError, color.HiRedString, "Failed to send message...\t%s", err)
}
}
}
}
outputToChannel(channelConfig.Channel)
if channelConfig.Channels != nil {
for _, ch := range *channelConfig.Channels {
outputToChannel(ch)
}
}
}
}
}
}