This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
handler.go
297 lines (277 loc) · 10.5 KB
/
handler.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
package main
import (
"fmt"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"mvdan.cc/xurls/v2"
)
// helpHandler displays the help message
func helpHandler(message *discordgo.Message) {
dg.ChannelMessageSend(
message.ChannelID,
"**<link>** to download a link\n"+
"**version** to find out the version\n"+
"**stats** to view stats\n"+
"**channels** to list active channels\n"+
"**history** to download a full channel history\n"+
"**help** to open this help\n",
)
}
// versionHandler displays the current version
func versionHandler(message *discordgo.Message) {
result := fmt.Sprintf("discord-image-downloder-go **v%s**\n", VERSION)
if !isLatestRelease() {
result += fmt.Sprintf("**update available on <%s>**", RELEASE_URL)
} else {
result += "version is up to date"
}
dg.ChannelMessageSend(message.ChannelID, result)
}
// channelsHandler displays enabled channels
func channelsHandler(message *discordgo.Message) {
var err error
var channel *discordgo.Channel
var channelRecipientUsername string
result := "**Channels**\n"
for channelID, channelFolder := range ChannelWhitelist {
channel, err = dg.State.Channel(channelID)
if err != nil {
continue
}
switch channel.Type {
case discordgo.ChannelTypeDM:
channelRecipientUsername = ""
for _, recipient := range channel.Recipients {
channelRecipientUsername = recipient.Username + ", "
}
channelRecipientUsername = strings.TrimRight(channelRecipientUsername, ", ")
if channelRecipientUsername == "" {
channelRecipientUsername = "N/A"
}
result += fmt.Sprintf("@%s (`#%s`): `%s`\n", channelRecipientUsername, channel.ID, channelFolder)
default:
result += fmt.Sprintf("<#%s> (`#%s`): `%s`\n", channel.ID, channel.ID, channelFolder)
}
}
result += "**Interactive Channels**\n"
for channelID, channelFolder := range InteractiveChannelWhitelist {
channel, err = dg.State.Channel(channelID)
if err != nil {
continue
}
switch channel.Type {
case discordgo.ChannelTypeDM:
channelRecipientUsername = ""
for _, recipient := range channel.Recipients {
channelRecipientUsername = recipient.Username + ", "
}
channelRecipientUsername = strings.TrimRight(channelRecipientUsername, ", ")
if channelRecipientUsername == "" {
channelRecipientUsername = "N/A"
}
result += fmt.Sprintf("@%s (`#%s`): `%s`\n", channelRecipientUsername, channel.ID, channelFolder)
default:
result += fmt.Sprintf("<#%s> (`#%s`): `%s`\n", channel.ID, channel.ID, channelFolder)
}
}
for _, page := range Pagify(result, "\n") {
dg.ChannelMessageSend(message.ChannelID, page)
}
}
func statsHandler(message *discordgo.Message) {
channelStats := make(map[string]int)
userStats := make(map[string]int)
userGuilds := make(map[string]string)
var i int
myDB.Use("Downloads").ForEachDoc(func(id int, docContent []byte) (willMoveOn bool) {
downloadedImage := findDownloadedImageById(id)
channelStats[downloadedImage.ChannelId] += 1
userStats[downloadedImage.UserId] += 1
if _, ok := userGuilds[downloadedImage.UserId]; !ok {
channel, err := dg.State.Channel(downloadedImage.ChannelId)
if err == nil && channel.GuildID != "" {
userGuilds[downloadedImage.UserId] = channel.GuildID
}
}
i++
return true
})
channelStatsSorted := sortStringIntMapByValue(channelStats)
userStatsSorted := sortStringIntMapByValue(userStats)
replyMessage := fmt.Sprintf("I downloaded **%d** pictures in **%d** channels by **%d** users\n", i, len(channelStats), len(userStats))
replyMessage += "**channel breakdown**\n"
for _, downloads := range channelStatsSorted {
channel, err := dg.State.Channel(downloads.Key)
if err == nil {
if channel.Type == discordgo.ChannelTypeDM {
channelRecipientUsername := "N/A"
for _, recipient := range channel.Recipients {
channelRecipientUsername = recipient.Username
}
replyMessage += fmt.Sprintf("@%s (`#%s`): **%d** downloads\n", channelRecipientUsername, downloads.Key, downloads.Value)
} else {
guild, err := dg.State.Guild(channel.GuildID)
if err == nil {
replyMessage += fmt.Sprintf("#%s/%s (`#%s`): **%d** downloads\n", guild.Name, channel.Name, downloads.Key, downloads.Value)
} else {
fmt.Println(err)
}
}
} else {
fmt.Println(err)
}
}
replyMessage += "**user breakdown**\n"
var userI int
for _, downloads := range userStatsSorted {
userI++
if userI > 10 {
replyMessage += "_only the top 10 users get shown_\n"
break
}
if guildId, ok := userGuilds[downloads.Key]; ok {
user, err := dg.State.Member(guildId, downloads.Key)
if err == nil {
replyMessage += fmt.Sprintf("@%s: **%d** downloads\n", user.User.Username, downloads.Value)
} else {
replyMessage += fmt.Sprintf("@`%s`: **%d** downloads\n", downloads.Key, downloads.Value)
}
} else {
replyMessage += fmt.Sprintf("@`%s`: **%d** downloads\n", downloads.Key, downloads.Value)
}
}
for _, page := range Pagify(replyMessage, "\n") {
dg.ChannelMessageSend(message.ChannelID, page)
}
}
func historyHandler(message *discordgo.Message) {
i := 0
_, historyCommandIsSet := historyCommandActive[message.ChannelID]
if !historyCommandIsSet || historyCommandActive[message.ChannelID] == "" {
historyCommandActive[message.ChannelID] = ""
idArray := strings.Split(message.Content, ",")
for _, channelValue := range idArray {
channelValue = strings.TrimSpace(channelValue)
if folder, ok := ChannelWhitelist[channelValue]; ok {
dg.ChannelMessageSend(message.ChannelID, fmt.Sprintf("downloading to `%s`", folder))
historyCommandActive[message.ChannelID] = "downloading"
lastBefore := ""
var lastBeforeTime time.Time
MessageRequestingLoop:
for true {
if lastBeforeTime != (time.Time{}) {
fmt.Printf("[%s] Requesting 100 more messages, (before %s)\n", time.Now().Format(time.Stamp), lastBeforeTime)
dg.ChannelMessageSend(message.ChannelID, fmt.Sprintf("Requesting 100 more messages, (before %s)\n", lastBeforeTime))
}
messages, err := dg.ChannelMessages(channelValue, 100, lastBefore, "", "")
if err == nil {
if len(messages) <= 0 {
delete(historyCommandActive, message.ChannelID)
break MessageRequestingLoop
}
lastBefore = messages[len(messages)-1].ID
lastBeforeTime, err = messages[len(messages)-1].Timestamp.Parse()
if err != nil {
fmt.Println(err)
}
for _, msg := range messages {
fileTime := time.Now()
if msg.Timestamp != "" {
fileTime, err = msg.Timestamp.Parse()
if err != nil {
fmt.Println(err)
}
}
if historyCommandActive[message.ChannelID] == "cancel" {
delete(historyCommandActive, message.ChannelID)
dg.ChannelMessageSend(message.ChannelID, "cancelled history downloading")
break MessageRequestingLoop
}
for _, iAttachment := range msg.Attachments {
if len(findDownloadedImageByUrl(iAttachment.URL)) == 0 {
i++
startDownload(iAttachment.URL, iAttachment.Filename, folder, msg.ChannelID, msg.Author.ID, fileTime)
}
}
foundUrls := xurls.Strict().FindAllString(msg.Content, -1)
for _, iFoundUrl := range foundUrls {
links, _ := getDownloadLinks(iFoundUrl, msg.ChannelID, false)
for link, filename := range links {
if len(findDownloadedImageByUrl(link)) == 0 {
i++
startDownload(link, filename, folder, msg.ChannelID, msg.Author.ID, fileTime)
}
}
}
}
} else {
dg.ChannelMessageSend(message.ChannelID, err.Error())
fmt.Println(err)
delete(historyCommandActive, message.ChannelID)
break MessageRequestingLoop
}
}
dg.ChannelMessageSend(message.ChannelID, fmt.Sprintf("done, %d download links started!", i))
} else {
dg.ChannelMessageSend(message.ChannelID, "Please tell me one or multiple Channel IDs (separated by commas)\nPlease make sure the channels have been whitelisted before submitting.")
}
}
} else if historyCommandActive[message.ChannelID] == "downloading" && strings.ToLower(message.Content) == "cancel" {
historyCommandActive[message.ChannelID] = "cancel"
}
}
func defaultHandler(message *discordgo.Message) {
folderName := InteractiveChannelWhitelist[message.ChannelID]
if link, ok := interactiveChannelLinkTemp[message.ChannelID]; ok {
fileTime := time.Now()
var err error
if message.Timestamp != "" {
fileTime, err = message.Timestamp.Parse()
if err != nil {
fmt.Println(err)
}
}
if message.Content == "." {
dg.ChannelMessageSend(message.ChannelID, fmt.Sprintf("Download of <%s> started", link))
dg.ChannelTyping(message.ChannelID)
delete(interactiveChannelLinkTemp, message.ChannelID)
links, _ := getDownloadLinks(link, message.ChannelID, true)
for linkR, filename := range links {
startDownload(linkR, filename, folderName, message.ChannelID, message.Author.ID, fileTime)
}
dg.ChannelMessageSend(message.ChannelID, fmt.Sprintf("Download of <%s> finished", link))
} else if strings.ToLower(message.Content) == "cancel" {
delete(interactiveChannelLinkTemp, message.ChannelID)
dg.ChannelMessageSend(message.ChannelID, fmt.Sprintf("Download of <%s> cancelled", link))
} else if IsValid(message.Content) {
dg.ChannelMessageSend(message.ChannelID, fmt.Sprintf("Download of <%s> started", link))
dg.ChannelTyping(message.ChannelID)
delete(interactiveChannelLinkTemp, message.ChannelID)
links, _ := getDownloadLinks(link, message.ChannelID, true)
for linkR, filename := range links {
startDownload(linkR, filename, message.Content, message.ChannelID, message.Author.ID, fileTime)
}
dg.ChannelMessageSend(message.ChannelID, fmt.Sprintf("Download of <%s> finished", link))
} else {
dg.ChannelMessageSend(message.ChannelID, "invalid path")
}
} else {
_ = folderName
foundLinks := false
for _, iAttachment := range message.Attachments {
dg.ChannelMessageSend(message.ChannelID, fmt.Sprintf("Where do you want to save <%s>?\nType **.** for default path or **cancel** to cancel the download %s", iAttachment.URL, folderName))
interactiveChannelLinkTemp[message.ChannelID] = iAttachment.URL
foundLinks = true
}
foundUrls := xurls.Strict().FindAllString(message.Content, -1)
for _, iFoundUrl := range foundUrls {
dg.ChannelMessageSend(message.ChannelID, fmt.Sprintf("Where do you want to save <%s>?\nType **.** for default path or **cancel** to cancel the download %s", iFoundUrl, folderName))
interactiveChannelLinkTemp[message.ChannelID] = iFoundUrl
foundLinks = true
}
if foundLinks == false {
dg.ChannelMessageSend(message.ChannelID, "unable to find valid link")
}
}
}