Drn hace 2 años
padre
commit
b7b3a797ef
Se han modificado 5 ficheros con 79 adiciones y 66 borrados
  1. 21 19
      README.md
  2. 1 0
      commands.go
  3. 2 0
      discord.go
  4. 54 47
      downloads.go
  5. 1 0
      handlers.go

+ 21 - 19
README.md

@@ -1093,25 +1093,27 @@ _For `filenameFormat`_
 <details>
 <summary><b><i>(COLLAPSABLE SECTION)</i></b></summary>
 
-Key                 | Description
----                 | ---
-`{{date}}`          | Timestamp
-`{{file}}`          | Filename
-`{{fileType}}`      | File type
-`{{messageID}}`     | Source Message ID
-`{{userID}}`        | Source Author User ID
-`{{username}}`      | Source Author Username
-`{{channelID}}`     | Source Channel ID
-`{{channelName}}`   | Source Channel Name
-`{{categoryID}}`    | Source Category ID
-`{{categoryName}}`  | Source Category Name
-`{{serverID}}`      | Source Server ID
-`{{serverName}}`    | Source Server Name
-`{{message}}`       | Source Message Content (with path symbols stripped)
-`{{url}}`           | Download Full URL
-`{{domain}}`        | Download URL Domain
-`{{nanoID}}`        | 21 character unique string, eg: i25_rX9zwDdDn7Sg-ZoaH
-`{{shortID}}`       | Short unique string, eg: NVovc6-QQy
+Key                     | Description
+---                     | ---
+`{{date}}`              | Timestamp
+`{{file}}`              | Filename
+`{{fileType}}`          | File type
+`{{messageID}}`         | Source Message ID
+`{{userID}}`            | Source Author User ID
+`{{username}}`          | Source Author Username
+`{{channelID}}`         | Source Channel ID
+`{{channelName}}`       | Source Channel Name
+`{{categoryID}}`        | Source Category ID
+`{{categoryName}}`      | Source Category Name
+`{{serverID}}`          | Source Server ID
+`{{serverName}}`        | Source Server Name
+`{{message}}`           | Source Message Content (with path symbols stripped)
+`{{downloadTime}}`      | Time elapsed to download
+`{{downloadTimeLong}}`  | Time elapsed to download long version
+`{{url}}`               | Download Full URL
+`{{domain}}`            | Download URL Domain
+`{{nanoID}}`            | 21 character unique string, eg: i25_rX9zwDdDn7Sg-ZoaH
+`{{shortID}}`           | Short unique string, eg: NVovc6-QQy
 
 </details>
 

+ 1 - 0
commands.go

@@ -459,6 +459,7 @@ func handleCommands() *exrouter.Route {
 											FileTime:   time.Now(),
 											HistoryCmd: false,
 											EmojiCmd:   true,
+											StartTime:  time.Now(),
 										})
 
 									if status.Status == downloadSuccess {

+ 2 - 0
discord.go

@@ -303,6 +303,8 @@ func dynamicKeyReplacement(channelConfig configurationSource, download downloadR
 			{"{{serverID}}", download.Message.GuildID},
 			{"{{serverName}}", guildName},
 			{"{{message}}", clearPath(download.Message.Content)},
+			{"{{downloadTime}}", durafmt.ParseShort(time.Since(download.StartTime)).String()},
+			{"{{downloadTimeLong}}", durafmt.Parse(time.Since(download.StartTime)).String()},
 			{"{{url}}", clearPath(download.InputURL)},
 			{"{{domain}}", domain},
 			{"{{nanoID}}", nanoID},

+ 54 - 47
downloads.go

@@ -19,6 +19,7 @@ import (
 
 	"github.com/bwmarrin/discordgo"
 	"github.com/fatih/color"
+	"github.com/hako/durafmt"
 	"github.com/rivo/duplo"
 	"mvdan.cc/xurls/v2"
 )
@@ -421,6 +422,7 @@ type downloadRequestStruct struct {
 	HistoryCmd     bool
 	EmojiCmd       bool
 	ManualDownload bool
+	StartTime      time.Time
 }
 
 func handleDownload(download downloadRequestStruct) downloadStatusStruct {
@@ -1010,11 +1012,14 @@ func tryDownload(download downloadRequestStruct) downloadStatusStruct {
 			msgTimestamp := ""
 			if download.HistoryCmd {
 				dlColor = color.HiCyanString
-				msgTimestamp = "on " + download.Message.Timestamp.Format("2006/01/02 @ 15:04:05") + " "
+				msgTimestamp = durafmt.ParseShort(time.Since(download.Message.Timestamp)).String() + "ago on " +
+					download.Message.Timestamp.Format("2006/01/02 @ 15:04:05") + " "
 			}
 			log.Println(lg("Download", "", dlColor,
-				logPrefix+"SAVED %s sent %sin \"%s / %s\" from \"%s\" to %s",
-				strings.ToUpper(contentTypeFound), msgTimestamp, sourceName, sourceChannelName, condenseString(download.InputURL, 50), download.Path+subfolder))
+				logPrefix+"SAVED %s sent %sin \"%s / %s\" from \"%s\" to %s (took %s)",
+				strings.ToUpper(contentTypeFound), msgTimestamp, sourceName, sourceChannelName,
+				condenseString(download.InputURL, 50), download.Path+subfolder,
+				durafmt.ParseShort(time.Since(download.StartTime)).String()))
 		} else {
 			log.Println(lg("Download", "", color.GreenString,
 				logPrefix+"Did not save %s sent in %s#%s --- file saving disabled...",
@@ -1040,61 +1045,63 @@ func tryDownload(download downloadRequestStruct) downloadStatusStruct {
 		}
 
 		// React
-		shouldReact := config.ReactWhenDownloaded
-		if channelConfig.ReactWhenDownloaded != nil {
-			shouldReact = *channelConfig.ReactWhenDownloaded
-		}
-		if download.HistoryCmd {
-			if !config.ReactWhenDownloadedHistory {
-				shouldReact = false
+		{
+			shouldReact := config.ReactWhenDownloaded
+			if channelConfig.ReactWhenDownloaded != nil {
+				shouldReact = *channelConfig.ReactWhenDownloaded
 			}
-			if channelConfig.ReactWhenDownloadedHistory != nil {
-				if *channelConfig.ReactWhenDownloadedHistory {
-					shouldReact = true
+			if download.HistoryCmd {
+				if !config.ReactWhenDownloadedHistory {
+					shouldReact = false
+				}
+				if channelConfig.ReactWhenDownloadedHistory != nil {
+					if *channelConfig.ReactWhenDownloadedHistory {
+						shouldReact = true
+					}
 				}
 			}
-		}
-		if download.Message.Author != nil && shouldReact {
-			reaction := ""
-			if *channelConfig.ReactWhenDownloadedEmoji == "" {
-				if download.Message.GuildID != "" {
-					guild, err := bot.State.Guild(download.Message.GuildID)
-					if err != nil {
-						log.Println(lg("Download", "", color.RedString,
-							"Error fetching guild state for emojis from %s: %s",
-							download.Message.GuildID, err))
-					} else {
-						emojis := guild.Emojis
-						if len(emojis) > 1 {
-							for {
-								rand.Seed(time.Now().UnixNano())
-								chosenEmoji := emojis[rand.Intn(len(emojis))]
-								formattedEmoji := chosenEmoji.APIName()
-								if !chosenEmoji.Animated && !stringInSlice(formattedEmoji,
-									*channelConfig.BlacklistReactEmojis) {
-									reaction = formattedEmoji
-									break
+			if download.Message.Author != nil && shouldReact {
+				reaction := ""
+				if *channelConfig.ReactWhenDownloadedEmoji == "" {
+					if download.Message.GuildID != "" {
+						guild, err := bot.State.Guild(download.Message.GuildID)
+						if err != nil {
+							log.Println(lg("Download", "", color.RedString,
+								"Error fetching guild state for emojis from %s: %s",
+								download.Message.GuildID, err))
+						} else {
+							emojis := guild.Emojis
+							if len(emojis) > 1 {
+								for {
+									rand.Seed(time.Now().UnixNano())
+									chosenEmoji := emojis[rand.Intn(len(emojis))]
+									formattedEmoji := chosenEmoji.APIName()
+									if !chosenEmoji.Animated && !stringInSlice(formattedEmoji,
+										*channelConfig.BlacklistReactEmojis) {
+										reaction = formattedEmoji
+										break
+									}
 								}
+							} else {
+								reaction = defaultReact
 							}
-						} else {
-							reaction = defaultReact
 						}
+					} else {
+						reaction = defaultReact
 					}
 				} else {
-					reaction = defaultReact
+					reaction = *channelConfig.ReactWhenDownloadedEmoji
 				}
-			} else {
-				reaction = *channelConfig.ReactWhenDownloadedEmoji
-			}
-			// Add Reaction
-			if hasPerms(download.Message.ChannelID, discordgo.PermissionAddReactions) {
-				if err = bot.MessageReactionAdd(download.Message.ChannelID, download.Message.ID, reaction); err != nil {
+				// Add Reaction
+				if hasPerms(download.Message.ChannelID, discordgo.PermissionAddReactions) {
+					if err = bot.MessageReactionAdd(download.Message.ChannelID, download.Message.ID, reaction); err != nil {
+						log.Println(lg("Download", "", color.RedString,
+							"Error adding reaction to message: %s", err))
+					}
+				} else {
 					log.Println(lg("Download", "", color.RedString,
-						"Error adding reaction to message: %s", err))
+						"Bot does not have permission to add reactions in %s", download.Message.ChannelID))
 				}
-			} else {
-				log.Println(lg("Download", "", color.RedString,
-					"Bot does not have permission to add reactions in %s", download.Message.ChannelID))
 			}
 		}
 

+ 1 - 0
handlers.go

@@ -324,6 +324,7 @@ func handleMessage(m *discordgo.Message, edited bool, history bool) int64 {
 					FileTime:   file.Time,
 					HistoryCmd: history,
 					EmojiCmd:   false,
+					StartTime:  time.Now(),
 				})
 			if status.Status == downloadSuccess {
 				downloadCount++