Browse Source

crude thread support

Drn 2 years ago
parent
commit
278a8b9cb5
7 changed files with 70 additions and 45 deletions
  1. 7 5
      commands.go
  2. 34 23
      config.go
  3. 6 4
      discord.go
  4. 10 7
      downloads.go
  5. 3 2
      handlers.go
  6. 4 3
      history.go
  7. 6 1
      main.go

+ 7 - 5
commands.go

@@ -128,8 +128,9 @@ func handleCommands() *exrouter.Route {
 					len(config.AdminChannels),
 					bot.HeartbeatLatency().Milliseconds(),
 				)
-				if isChannelRegistered(ctx.Msg.ChannelID) {
-					configJson, _ := json.MarshalIndent(getChannelConfig(ctx.Msg.ChannelID), "", "\t")
+				ch := channelRegistered(ctx.Msg)
+				if ch != "" {
+					configJson, _ := json.MarshalIndent(getChannelConfig(ch), "", "\t")
 					message = message + fmt.Sprintf("\n• **Channel Settings...** ```%s```", string(configJson))
 				}
 				_, err := replyEmbed(ctx.Msg, "Command — Status", message)
@@ -147,8 +148,9 @@ func handleCommands() *exrouter.Route {
 	router.On("stats", func(ctx *exrouter.Context) {
 		logPrefixHere := color.CyanString("[dgrouter:stats]")
 		if hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
-			if isChannelRegistered(ctx.Msg.ChannelID) {
-				channelConfig := getChannelConfig(ctx.Msg.ChannelID)
+			ch := channelRegistered(ctx.Msg)
+			if ch != "" {
+				channelConfig := getChannelConfig(ch)
 				if *channelConfig.AllowCommands {
 					content := fmt.Sprintf("• **Total Downloads —** %s\n"+
 						"• **Downloads in this Channel —** %s",
@@ -266,7 +268,7 @@ func handleCommands() *exrouter.Route {
 							}
 						}
 					} else if strings.Contains(strings.ToLower(target), "all") {
-						channels = getAllChannels()
+						channels = getAllRegisteredChannels()
 					}
 				}
 			}

+ 34 - 23
config.go

@@ -863,16 +863,26 @@ func adminChannelDefault(channel *configurationAdminChannel) {
 
 //#region Channel Checks/Returns
 
-func isChannelRegistered(ChannelID string) bool {
+func isNestedMessage(subjectMessage *discordgo.Message, targetChannel string) bool {
+	_, err := bot.State.Message(targetChannel, subjectMessage.ChannelID)
+	return err == nil
+}
+
+func channelRegistered(m *discordgo.Message) string {
 	for _, item := range config.Channels {
 		// Single Channel Config
-		if ChannelID == item.ChannelID {
-			return true
+		if m.ChannelID == item.ChannelID || isNestedMessage(m, item.ChannelID) {
+			return item.ChannelID
 		}
 		// Multi-Channel Config
 		if item.ChannelIDs != nil {
-			if stringInSlice(ChannelID, *item.ChannelIDs) {
-				return true
+			if stringInSlice(m.ChannelID, *item.ChannelIDs) {
+				return m.ChannelID
+			}
+			for _, channel := range *item.ChannelIDs {
+				if m.ChannelID == channel || isNestedMessage(m, channel) {
+					return channel
+				}
 			}
 		}
 	}
@@ -882,20 +892,20 @@ func isChannelRegistered(ChannelID string) bool {
 			guild, err := bot.State.Guild(item.ServerID)
 			if err == nil {
 				for _, channel := range guild.Channels {
-					if ChannelID == channel.ID {
+					if m.ChannelID == channel.ID || isNestedMessage(m, channel.ID) {
 						// Channel Blacklisting within Server
 						if item.ServerBlacklist != nil {
-							if stringInSlice(ChannelID, *item.ServerBlacklist) {
-								return false
+							if stringInSlice(m.ChannelID, *item.ServerBlacklist) {
+								return ""
 							}
 							// Categories
 							if channel.ParentID != "" {
 								if stringInSlice(channel.ParentID, *item.ServerBlacklist) {
-									return false
+									return ""
 								}
 							}
 						}
-						return true
+						return channel.ID
 					}
 				}
 			}
@@ -906,20 +916,20 @@ func isChannelRegistered(ChannelID string) bool {
 				guild, err := bot.State.Guild(subserver)
 				if err == nil {
 					for _, channel := range guild.Channels {
-						if ChannelID == channel.ID {
+						if m.ChannelID == channel.ID || isNestedMessage(m, channel.ID) {
 							// Channel Blacklisting within Servers
 							if item.ServerBlacklist != nil {
-								if stringInSlice(ChannelID, *item.ServerBlacklist) {
-									return false
+								if stringInSlice(m.ChannelID, *item.ServerBlacklist) {
+									return ""
 								}
 								// Categories
 								if channel.ParentID != "" {
 									if stringInSlice(channel.ParentID, *item.ServerBlacklist) {
-										return false
+										return ""
 									}
 								}
 							}
-							return true
+							return channel.ID
 						}
 					}
 				}
@@ -929,23 +939,23 @@ func isChannelRegistered(ChannelID string) bool {
 	// All
 	if config.All != nil {
 		if config.AllBlacklistChannels != nil {
-			if stringInSlice(ChannelID, *config.AllBlacklistChannels) {
-				return false
+			if stringInSlice(m.ChannelID, *config.AllBlacklistChannels) {
+				return ""
 			}
 		}
 		if config.AllBlacklistServers != nil {
-			guild, err := bot.State.Guild(ChannelID)
+			guild, err := bot.State.Guild(m.ChannelID)
 			if err == nil {
 				if stringInSlice(guild.ID, *config.AllBlacklistServers) {
-					return false
+					return ""
 				}
 			} else {
 				log.Println(color.HiRedString("Error finding server info for channel:\t%s", err))
 			}
 		}
-		return true
+		return "1"
 	}
-	return false
+	return ""
 }
 
 func getChannelConfig(ChannelID string) configurationChannel {
@@ -1034,10 +1044,11 @@ func getAdminChannelConfig(ChannelID string) configurationAdminChannel {
 }
 
 func isCommandableChannel(m *discordgo.Message) bool {
+	ch := channelRegistered(m)
 	if isAdminChannelRegistered(m.ChannelID) {
 		return true
-	} else if isChannelRegistered(m.ChannelID) {
-		channelConfig := getChannelConfig(m.ChannelID)
+	} else if ch != "" {
+		channelConfig := getChannelConfig(ch)
 		if *channelConfig.AllowCommands || isBotAdmin(m) || m.Author.ID == bot.State.User.ID {
 			return true
 		}

+ 6 - 4
discord.go

@@ -42,7 +42,7 @@ func discordSnowflakeToTimestamp(snowflake string, format string) string {
 	return t.Local().Format(format)
 }
 
-func getAllChannels() []string {
+func getAllRegisteredChannels() []string {
 	var channels []string
 	if config.All != nil { // ALL MODE
 		for _, guild := range bot.State.Guilds {
@@ -266,14 +266,16 @@ func getEmbedColor(channelID string) int {
 		}
 	}
 	// Overwrite with Defined Color for Channel
-	if isChannelRegistered(channelID) {
+	/*var msg *discordgo.Message
+	msg.ChannelID = channelID
+	if channelRegistered(msg) {
 		channelConfig := getChannelConfig(channelID)
 		if channelConfig.OverwriteEmbedColor != nil {
 			if *channelConfig.OverwriteEmbedColor != "" {
 				color = channelConfig.OverwriteEmbedColor
 			}
 		}
-	}
+	}*/
 
 	// Use Defined Color
 	if color != nil {
@@ -394,7 +396,7 @@ func logStatusMessage(status logStatusType) {
 				if config.All != nil {
 					message += "\n• **ALL MODE ENABLED -** Bot will use all available channels"
 				}
-				allChannels := getAllChannels()
+				allChannels := getAllRegisteredChannels()
 				message += fmt.Sprintf("\n• ***Listening to %s channel%s...***\n", formatNumber(int64(len(allChannels))), pluralS(len(allChannels)))
 				if twitterConnected {
 					message += "\n• Connected to Twitter API"

+ 10 - 7
downloads.go

@@ -441,8 +441,9 @@ func startDownload(download downloadRequestStruct) downloadStatusStruct {
 	// Any kind of failure
 	if status.Status >= downloadFailed && !download.HistoryCmd && !download.EmojiCmd {
 		log.Println(logPrefixErrorHere, color.RedString("Gave up on downloading %s after %d failed attempts...\t%s", download.InputURL, config.DownloadRetryMax, getDownloadStatusString(status.Status)))
-		if isChannelRegistered(download.Message.ChannelID) {
-			channelConfig := getChannelConfig(download.Message.ChannelID)
+		ch := channelRegistered(download.Message)
+		if ch != "" {
+			channelConfig := getChannelConfig(ch)
 			if !download.HistoryCmd && *channelConfig.SendErrorMessages {
 				content := fmt.Sprintf(
 					"Gave up trying to download\n<%s>\nafter %d failed attempts...\n\n``%s``",
@@ -478,8 +479,9 @@ func startDownload(download downloadRequestStruct) downloadStatusStruct {
 	}
 
 	// Log Links to File
-	if isChannelRegistered(download.Message.ChannelID) {
-		channelConfig := getChannelConfig(download.Message.ChannelID)
+	ch := channelRegistered(download.Message)
+	if ch != "" {
+		channelConfig := getChannelConfig(ch)
 		if channelConfig.LogLinks != nil {
 			if channelConfig.LogLinks.Destination != "" {
 				logPath := channelConfig.LogLinks.Destination
@@ -601,10 +603,11 @@ func tryDownload(download downloadRequestStruct) downloadStatusStruct {
 		logPrefix = logPrefixHistory + " "
 	}
 
-	if stringInSlice(download.Message.ChannelID, getAllChannels()) || download.EmojiCmd || download.ManualDownload {
+	ch := channelRegistered(download.Message)
+	if ch != "" || download.EmojiCmd || download.ManualDownload {
 		var channelConfig configurationChannel
-		if isChannelRegistered(download.Message.ChannelID) {
-			channelConfig = getChannelConfig(download.Message.ChannelID)
+		if ch != "" {
+			channelConfig = getChannelConfig(ch)
 		} else {
 			channelDefault(&channelConfig)
 		}

+ 3 - 2
handlers.go

@@ -66,8 +66,9 @@ func handleMessage(m *discordgo.Message, edited bool, history bool) int64 {
 	}
 
 	// Registered Channel
-	if isChannelRegistered(m.ChannelID) {
-		channelConfig := getChannelConfig(m.ChannelID)
+	ch := channelRegistered(m)
+	if ch != "" {
+		channelConfig := getChannelConfig(ch)
 		// Ignore bots if told to do so
 		if m.Author.Bot && *channelConfig.IgnoreBots {
 			return -1

+ 4 - 3
history.go

@@ -71,9 +71,10 @@ func handleHistory(commandingMessage *discordgo.Message, subjectChannelID string
 
 	var err error
 	var message *discordgo.Message = nil
-
-	if isChannelRegistered(subjectChannelID) {
-		channelConfig := getChannelConfig(subjectChannelID)
+	message.ChannelID = subjectChannelID
+	ch := channelRegistered(message)
+	if ch != "" {
+		channelConfig := getChannelConfig(ch)
 
 		// Open Cache File?
 		if historyCachePath != "" {

+ 6 - 1
main.go

@@ -271,7 +271,7 @@ func main() {
 	// Compile list of channels to autorun history
 	type arh struct{ channel, before, since string }
 	var autorunHistoryChannels []arh
-	for _, channel := range getAllChannels() {
+	for _, channel := range getAllRegisteredChannels() {
 		channelConfig := getChannelConfig(channel)
 		if channelConfig.OverwriteAutorunHistory != nil {
 			if *channelConfig.OverwriteAutorunHistory {
@@ -462,6 +462,11 @@ func botLogin() {
 		dur, _ = time.ParseDuration("180s")
 	}
 	bot.Client.Timeout = dur
+	bot.State.MaxMessageCount = 100000
+	bot.State.TrackChannels = true
+	bot.State.TrackThreads = true
+	bot.State.TrackMembers = true
+	bot.State.TrackThreadMembers = true
 
 	// Fetch Bot's User Info
 	user, err = bot.User("@me")