-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord.go
292 lines (270 loc) · 9.25 KB
/
discord.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
package main
import (
"fmt"
"runtime"
"strconv"
"strings"
"time"
"github.com/AvraamMavridis/randomcolor"
"github.com/bwmarrin/discordgo"
xsysinfo "github.com/elastic/go-sysinfo"
"github.com/fatih/color"
"github.com/hako/durafmt"
"github.com/inhies/go-bytesize"
)
func isBotAdmin(m *discordgo.Message) bool {
// No Admins
if len(config.DiscordAdmins) == 0 {
return true
}
// Bypass Check
if isServerPermitted(m.GuildID) {
serverConfig := getPermittedServerConfig(m.GuildID)
if serverConfig.UnlockCommands {
return true
}
}
//
return stringInSlice(m.Author.ID, config.DiscordAdmins)
}
func getUserIdentifier(usr discordgo.User) string {
return fmt.Sprintf("\"%s\"#%s", usr.Username, usr.Discriminator)
}
// For command case-insensitivity
func messageToLower(message *discordgo.Message) *discordgo.Message {
newMessage := *message
newMessage.Content = strings.ToLower(newMessage.Content)
return &newMessage
}
func hasPerms(channelID string, permission int64) bool {
if !config.DiscordCheckPerms {
return true
}
sourceChannel, err := bot.State.Channel(channelID)
if sourceChannel != nil && err == nil {
switch sourceChannel.Type {
case discordgo.ChannelTypeDM:
return true
case discordgo.ChannelTypeGroupDM:
return true
case discordgo.ChannelTypeGuildText:
perms, err := bot.UserChannelPermissions(user.ID, channelID)
if err == nil {
return perms&permission == permission
}
dubLog("Discord", logLevelError, color.HiRedString, "Failed to check permissions (%d) for %s:\t%s", permission, channelID, err)
}
}
return false
}
func dataKeyReplacement(input string) string {
//TODO: Case-insensitive key replacement. -- If no streamlined way to do it, convert to lower to find substring location but replace normally
if strings.Contains(input, "{{") && strings.Contains(input, "}}") {
timeNow := time.Now()
keys := [][]string{
{"{{goVersion}}", runtime.Version()},
{"{{dgVersion}}", discordgo.VERSION},
{"{{dubVersion}}", projectVersion},
{"{{apiVersion}}", discordgo.APIVersion},
{"{{numServers}}", fmt.Sprint(len(bot.State.Guilds))},
{"{{numAdmins}}", fmt.Sprint(len(config.DiscordAdmins))},
{"{{timeNowShort}}", timeNow.Format("3:04pm")},
{"{{timeNowShortTZ}}", timeNow.Format("3:04pm MST")},
{"{{timeNowMid}}", timeNow.Format("3:04pm MST 1/2/2006")},
{"{{timeNowLong}}", timeNow.Format("3:04:05pm MST - January 2, 2006")},
{"{{timeNowShort24}}", timeNow.Format("15:04")},
{"{{timeNowShortTZ24}}", timeNow.Format("15:04 MST")},
{"{{timeNowMid24}}", timeNow.Format("15:04 MST 2/1/2006")},
{"{{timeNowLong24}}", timeNow.Format("15:04:05 MST - 2 January, 2006")},
{"{{uptime}}", durafmt.ParseShort(time.Since(timeLaunched)).String()},
}
// Platform-Specific System Info
if strings.Contains(input, "lsys") || strings.Contains(input, "wsys") {
keys = append(keys, getPlatformKeys()...)
}
// Cross-Platform System Info
if strings.Contains(input, "xsys") {
sys, err := xsysinfo.Host()
if err == nil {
sysinf := sys.Info()
keys = append(keys, [][]string{
{"{{xsysArch}}", sysinf.Architecture},
{"{{xsysHostname}}", sysinf.Hostname},
{"{{xsysKernelVer}}", sysinf.KernelVersion},
{"{{xsysOsBuild}}", sysinf.OS.Build},
{"{{xsysOsCodename}}", sysinf.OS.Codename},
{"{{xsysOsFamily}}", sysinf.OS.Family},
{"{{xsysOsName}}", sysinf.OS.Name},
{"{{xsysOsPlatform}}", sysinf.OS.Platform},
{"{{xsysOsType}}", sysinf.OS.Type},
{"{{xsysOsVersion}}", sysinf.OS.Version},
{"{{xsysTZ}}", sysinf.Timezone},
{"{{xsysBootTimeShort}}", sysinf.BootTime.Format("3:04pm")},
{"{{xsysBootTimeShortTZ}}", sysinf.BootTime.Format("3:04pm MST")},
{"{{xsysBootTimeMid}}", sysinf.BootTime.Format("3:04pm MST 1/2/2006")},
{"{{xsysBootTimeLong}}", sysinf.BootTime.Format("3:04:05pm MST - January 2, 2006")},
{"{{xsysBootTimeShort24}}", sysinf.BootTime.Format("15:04")},
{"{{xsysBootTimeShortTZ24}}", sysinf.BootTime.Format("15:04 MST")},
{"{{xsysBootTimeMid24}}", sysinf.BootTime.Format("15:04 MST 2/1/2006")},
{"{{xsysBootTimeLong24}}", sysinf.BootTime.Format("15:04:05 MST - 2 January, 2006")},
{"{{xsysUptime}}", durafmt.ParseShort(sysinf.Uptime()).String()},
}...)
meminf, err := sys.Memory()
if err == nil {
keys = append(keys, [][]string{
{"{{xsysMemAvailable}}", bytesize.New(float64(meminf.Available)).String()},
{"{{xsysMemFree}}", bytesize.New(float64(meminf.Free)).String()},
{"{{xsysMemUsed}}", bytesize.New(float64(meminf.Used)).String()},
{"{{xsysMemTotal}}", bytesize.New(float64(meminf.Total)).String()},
{"{{xsysMemVirtualFree}}", bytesize.New(float64(meminf.VirtualFree)).String()},
{"{{xsysMemVirtualUsed}}", bytesize.New(float64(meminf.VirtualUsed)).String()},
{"{{xsysMemVirtualTotal}}", bytesize.New(float64(meminf.VirtualTotal)).String()},
}...)
} else if false {
//TODO: error message
}
} else if false {
//TODO: error message
}
procs, err := xsysinfo.Processes()
if err == nil {
keys = append(keys, [][]string{
{"{{xsysProcCount}}", fmt.Sprint(len(procs))},
}...)
} else if false {
//TODO: error message
}
}
for _, key := range keys {
if strings.Contains(input, key[0]) {
input = strings.ReplaceAll(input, key[0], key[1])
}
}
}
return input
}
func runDiscordPresences() {
// Rotate Presences
for presenceKey, presence := range config.Presence {
enabled := false
if presence.Enabled == nil {
enabled = true
} else {
enabled = *presence.Enabled
}
if enabled {
if presence.Duration == 0 {
presence.Duration = 15
}
// Only change status type, no text.
if presence.Status == "" {
bot.UpdateStatusComplex(discordgo.UpdateStatusData{
Status: presence.Type,
})
} else {
// Format state (referring to it as details) - Presence-specific key replacements
dataKeyReplacementPresence := func(input string) string {
input = dataKeyReplacement(input)
if strings.Contains(input, "{{presenceCount}}") {
input = strings.ReplaceAll(input, "{{presenceCount}}",
fmt.Sprintf("%d/%d", presenceKey+1, len(config.Presence)))
}
if strings.Contains(input, "{{presenceDuration}}") {
input = strings.ReplaceAll(input, "{{presenceDuration}}",
shortenTime(durafmt.ParseShort(
time.Duration(presence.Duration*int(time.Second)),
).String()),
)
}
return input
}
// Update
bot.UpdateStatusComplex(discordgo.UpdateStatusData{
Game: &discordgo.Game{
Name: dataKeyReplacementPresence(presence.Status),
State: dataKeyReplacementPresence(presence.StatusDetails),
Type: discordgo.GameType(presence.Label),
},
Status: presence.Type,
})
}
time.Sleep(time.Duration(presence.Duration * int(time.Second)))
}
}
}
//#region Embeds
func getEmbedColor(channelID string) int {
var err error
var color *string
var channelInfo *discordgo.Channel
// Use Defined Color
if color != nil {
// Defined as Role, fetch role color
if *color == "role" || *color == "user" {
botColor := bot.State.UserColor(user.ID, channelID)
if botColor != 0 {
return botColor
}
goto color_random
}
// Defined as Random, jump below (not preferred method but seems to work flawlessly)
if *color == "random" || *color == "rand" {
goto color_random
}
var colorString string = *color
// Input is Hex
colorString = strings.ReplaceAll(colorString, "#", "")
if convertedHex, err := strconv.ParseUint(colorString, 16, 64); err == nil {
return int(convertedHex)
}
// Input is Int
if convertedInt, err := strconv.Atoi(colorString); err == nil {
return convertedInt
}
// Definition is invalid since hasn't returned, so defaults to below...
}
// User color
channelInfo, err = bot.State.Channel(channelID)
if err == nil {
if channelInfo.Type != discordgo.ChannelTypeDM && channelInfo.Type != discordgo.ChannelTypeGroupDM {
if bot.State.UserColor(user.ID, channelID) != 0 {
return bot.State.UserColor(user.ID, channelID)
}
}
}
// Random color
color_random:
var randomColor string = randomcolor.GetRandomColorInHex()
if convertedRandom, err := strconv.ParseUint(strings.ReplaceAll(randomColor, "#", ""), 16, 64); err == nil {
return int(convertedRandom)
}
return 16777215 // white
}
// Shortcut function for quickly constructing a styled embed with Title & Description
func buildEmbed(channelID string, title string, description string) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{
Title: title,
Description: description,
Color: getEmbedColor(channelID),
Footer: &discordgo.MessageEmbedFooter{
IconURL: projectIcon,
Text: fmt.Sprintf("%s v%s", projectName, projectVersion),
},
}
}
// Shortcut function for quickly replying a styled embed with Title & Description
func replyEmbed(m *discordgo.Message, title string, description string) (*discordgo.Message, error) {
if m != nil {
if hasPerms(m.ChannelID, discordgo.PermissionSendMessages) {
return bot.ChannelMessageSendComplex(m.ChannelID,
&discordgo.MessageSend{
Content: m.Author.Mention(),
Embed: buildEmbed(m.ChannelID, title, description),
},
)
}
dubLog("Discord", logLevelError, color.HiRedString, fmtBotSendPerm, m.ChannelID)
}
return nil, nil
}
//#endregion