Skip to content

Commit

Permalink
Thread History pt.2 + message logging fix
Browse files Browse the repository at this point in the history
  • Loading branch information
get-got committed May 10, 2023
1 parent d8db249 commit 45d9719
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 43 deletions.
4 changes: 2 additions & 2 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func handleCommands() *exrouter.Route {
len(config.AdminChannels),
bot.HeartbeatLatency().Milliseconds(),
)
if sourceConfig := getSource(ctx.Msg); sourceConfig != emptyConfig {
if sourceConfig := getSource(ctx.Msg, nil); sourceConfig != emptyConfig {
configJson, _ := json.MarshalIndent(sourceConfig, "", "\t")
message = message + fmt.Sprintf("\n• **Channel Settings...** ```%s```", string(configJson))
}
Expand All @@ -157,7 +157,7 @@ func handleCommands() *exrouter.Route {
if !hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
log.Println(lg("Command", "Stats", color.HiRedString, fmtBotSendPerm, ctx.Msg.ChannelID))
} else {
if sourceConfig := getSource(ctx.Msg); sourceConfig != emptyConfig {
if sourceConfig := getSource(ctx.Msg, nil); sourceConfig != emptyConfig {
if *sourceConfig.AllowCommands {
content := fmt.Sprintf("• **Total Downloads —** %s\n"+
"• **Downloads in this Channel —** %s",
Expand Down
36 changes: 22 additions & 14 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1062,18 +1062,26 @@ func isNestedMessage(subjectMessage *discordgo.Message, targetChannel string) bo

var emptyConfig configurationSource = configurationSource{}

func getSource(m *discordgo.Message) configurationSource {
func getSource(m *discordgo.Message, c *discordgo.Channel) configurationSource {

subjectID := m.ChannelID

if c != nil {
if c.ParentID != "" {
subjectID = c.ParentID
}
}

// Channel
for _, item := range config.Channels {
// Single Channel Config
if m.ChannelID == item.ChannelID || isNestedMessage(m, item.ChannelID) {
if subjectID == item.ChannelID || isNestedMessage(m, item.ChannelID) {
return item
}
// Multi-Channel Config
if item.ChannelIDs != nil {
for _, subchannel := range *item.ChannelIDs {
if m.ChannelID == subchannel || isNestedMessage(m, subchannel) {
if subjectID == subchannel || isNestedMessage(m, subchannel) {
return item
}
}
Expand All @@ -1083,7 +1091,7 @@ func getSource(m *discordgo.Message) configurationSource {
// Category Config
for _, item := range config.Categories {
if item.CategoryID != "" {
channel, err := bot.State.Channel(m.ChannelID)
channel, err := bot.State.Channel(subjectID)
if err == nil {
if channel.ParentID == item.CategoryID {
return item
Expand All @@ -1093,7 +1101,7 @@ func getSource(m *discordgo.Message) configurationSource {
// Multi-Category Config
if item.CategoryIDs != nil {
for _, subcategory := range *item.CategoryIDs {
channel, err := bot.State.Channel(m.ChannelID)
channel, err := bot.State.Channel(subjectID)
if err == nil {
if channel.ParentID == subcategory {
if item.CategoryBlacklist != nil {
Expand All @@ -1114,10 +1122,10 @@ func getSource(m *discordgo.Message) configurationSource {
guild, err := bot.State.Guild(item.ServerID)
if err == nil {
for _, channel := range guild.Channels {
if m.ChannelID == channel.ID || isNestedMessage(m, channel.ID) {
if subjectID == channel.ID || isNestedMessage(m, channel.ID) {
// Channel Blacklisting within Server
if item.ServerBlacklist != nil {
if stringInSlice(m.ChannelID, *item.ServerBlacklist) {
if stringInSlice(subjectID, *item.ServerBlacklist) {
return emptyConfig
}
// Categories
Expand All @@ -1138,10 +1146,10 @@ func getSource(m *discordgo.Message) configurationSource {
guild, err := bot.State.Guild(subserver)
if err == nil {
for _, channel := range guild.Channels {
if m.ChannelID == channel.ID || isNestedMessage(m, channel.ID) {
if subjectID == channel.ID || isNestedMessage(m, channel.ID) {
// Channel Blacklisting within Servers
if item.ServerBlacklist != nil {
if stringInSlice(m.ChannelID, *item.ServerBlacklist) {
if stringInSlice(subjectID, *item.ServerBlacklist) {
return emptyConfig
}
// Categories
Expand Down Expand Up @@ -1179,14 +1187,14 @@ func getSource(m *discordgo.Message) configurationSource {
// All
if config.All != nil {
if config.AllBlacklistChannels != nil {
if stringInSlice(m.ChannelID, *config.AllBlacklistChannels) {
if stringInSlice(subjectID, *config.AllBlacklistChannels) {
return emptyConfig
}
}
if config.AllBlacklistCategories != nil {
chinf, err := bot.State.Channel(m.ChannelID)
chinf, err := bot.State.Channel(subjectID)
if err == nil {
if stringInSlice(chinf.ParentID, *config.AllBlacklistCategories) || stringInSlice(m.ChannelID, *config.AllBlacklistCategories) {
if stringInSlice(chinf.ParentID, *config.AllBlacklistCategories) || stringInSlice(subjectID, *config.AllBlacklistCategories) {
return emptyConfig
}
}
Expand Down Expand Up @@ -1251,7 +1259,7 @@ func isCommandableChannel(m *discordgo.Message) bool {
}
if isAdminChannelRegistered(m.ChannelID) {
return true
} else if channelConfig := getSource(m); channelConfig != emptyConfig {
} else if channelConfig := getSource(m, nil); channelConfig != emptyConfig {
if *channelConfig.AllowCommands || isBotAdmin(m) || m.Author.ID == bot.State.User.ID {
return true
}
Expand Down Expand Up @@ -1365,7 +1373,7 @@ func getAllRegisteredChannels() []string {
}
}
for _, channel := range guild.Channels {
if r := getSource(&discordgo.Message{ChannelID: channel.ID}); r == emptyConfig { // easier than redoing it all but way less efficient, im lazy
if r := getSource(&discordgo.Message{ChannelID: channel.ID}, nil); r == emptyConfig { // easier than redoing it all but way less efficient, im lazy
continue
} else {
if hasPerms(channel.ID, discordgo.PermissionViewChannel) && hasPerms(channel.ID, discordgo.PermissionReadMessageHistory) {
Expand Down
8 changes: 4 additions & 4 deletions downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func trimDuplicateLinks(fileItems []*fileItem) []*fileItem {

// Trim files already downloaded and stored in database
func trimDownloadedLinks(linkList map[string]string, m *discordgo.Message) map[string]string {
channelConfig := getSource(m)
channelConfig := getSource(m, nil)

newList := make(map[string]string, 0)
for link, filename := range linkList {
Expand Down Expand Up @@ -453,7 +453,7 @@ func (download downloadRequestStruct) handleDownload() (downloadStatusStruct, in
log.Println(lg("Download", "", color.RedString,
"Gave up on downloading %s after %d failed attempts...\t%s",
download.InputURL, config.DownloadRetryMax, getDownloadStatusString(status.Status)))
if channelConfig := getSource(download.Message); channelConfig != emptyConfig {
if channelConfig := getSource(download.Message, nil); channelConfig != emptyConfig {
if !download.HistoryCmd && *channelConfig.SendErrorMessages {
content := fmt.Sprintf(
"Gave up trying to download\n<%s>\nafter %d failed attempts...\n\n``%s``",
Expand Down Expand Up @@ -492,7 +492,7 @@ func (download downloadRequestStruct) handleDownload() (downloadStatusStruct, in
}

// Log Links to File
if channelConfig := getSource(download.Message); channelConfig != emptyConfig {
if channelConfig := getSource(download.Message, nil); channelConfig != emptyConfig {
if channelConfig.LogLinks != nil {
if channelConfig.LogLinks.Destination != "" {
logPath := channelConfig.LogLinks.Destination
Expand Down Expand Up @@ -623,7 +623,7 @@ func (download downloadRequestStruct) tryDownload() (downloadStatusStruct, int64

var channelConfig configurationSource
sourceDefault(&channelConfig)
_channelConfig := getSource(download.Message)
_channelConfig := getSource(download.Message, nil)
if _channelConfig != emptyConfig {
channelConfig = _channelConfig
}
Expand Down
29 changes: 22 additions & 7 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ var lastMessageID string

func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if lastMessageID != m.ID {
handleMessage(m.Message, false, false)
handleMessage(m.Message, nil, false, false)
}
lastMessageID = m.ID
}

func messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) {
if lastMessageID != m.ID {
if m.EditedTimestamp != nil {
handleMessage(m.Message, true, false)
handleMessage(m.Message, nil, true, false)
}
}
lastMessageID = m.ID
}

func handleMessage(m *discordgo.Message, edited bool, history bool) (int64, int64) {
func handleMessage(m *discordgo.Message, c *discordgo.Channel, edited bool, history bool) (int64, int64) {
// Ignore own messages unless told not to
if m.Author.ID == botUser.ID && !config.ScanOwnMessages {
return -1, 0
Expand Down Expand Up @@ -68,7 +68,7 @@ func handleMessage(m *discordgo.Message, edited bool, history bool) (int64, int6
}

// Registered Channel
if channelConfig := getSource(m); channelConfig != emptyConfig {
if channelConfig := getSource(m, c); channelConfig != emptyConfig {
// Ignore bots if told to do so
if m.Author.Bot && *channelConfig.IgnoreBots {
return -1, 0
Expand Down Expand Up @@ -119,16 +119,31 @@ func handleMessage(m *discordgo.Message, edited bool, history bool) (int64, int6
if *channelConfig.LogMessages.DivideLogsByServer {
if m.GuildID == "" {
ch, err := bot.State.Channel(m.ChannelID)
if err == nil {
if err != nil && c != nil {
ch = c
}
if ch != nil {
if ch.Type == discordgo.ChannelTypeDM {
logPath += " DM"
} else if ch.Type == discordgo.ChannelTypeGroupDM {
logPath += " GroupDM"
} else if ch.Type == discordgo.ChannelTypeGuildText {
logPath += " Text"
} else if ch.Type == discordgo.ChannelTypeGuildCategory {
logPath += " Category"
} else if ch.Type == discordgo.ChannelTypeGuildForum {
logPath += " Forum"
} else if ch.Type == discordgo.ChannelTypeGuildPrivateThread || ch.Type == discordgo.ChannelTypeGuildPublicThread {
logPath += " Thread"
} else {
logPath += " Unknown"
}
} else {
logPath += " Unknown"

if ch.Name != "" {
logPath += " - " + clearPath(ch.Name) + " -"
} else if ch.Topic != "" {
logPath += " - " + clearPath(ch.Topic) + " -"
}
}
} else {
logPath += " SID_" + m.GuildID
Expand Down
30 changes: 15 additions & 15 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,16 @@ func handleHistory(commandingMessage *discordgo.Message, subjectChannelID string
subjectChannels = append(subjectChannels, *baseChannelInfo)
baseChannelIsForum = false
}

// Index Threads
now := time.Now()
if threads, err := bot.ThreadsArchived(subjectChannelID, &now, 0); err == nil {
for _, thread := range threads.Threads {
log.Printf("found archived thread: %s", thread.ID)
subjectChannels = append(subjectChannels, *thread)
}
}
if threads, err := bot.ThreadsActive(subjectChannelID); err == nil {
for _, thread := range threads.Threads {
log.Printf("found active thread: %s", thread.ID)
subjectChannels = append(subjectChannels, *thread)
}
}
Expand Down Expand Up @@ -234,21 +233,22 @@ func handleHistory(commandingMessage *discordgo.Message, subjectChannelID string

//#endregion

if channelConfig := getSource(responseMsg); channelConfig != emptyConfig {
for _, channel := range subjectChannels {
logPrefix = fmt.Sprintf("%s/%s: ", channel.ID, commander)

if channelConfig := getSource(responseMsg, &channel); channelConfig != emptyConfig {

// Overwrite Send Status
if channelConfig.SendAutoHistoryStatus != nil {
if autorun && !*channelConfig.SendAutoHistoryStatus {
sendStatus = false
// Overwrite Send Status
if channelConfig.SendAutoHistoryStatus != nil {
if autorun && !*channelConfig.SendAutoHistoryStatus {
sendStatus = false
}
}
}
if channelConfig.SendHistoryStatus != nil {
if !autorun && !*channelConfig.SendHistoryStatus {
sendStatus = false
if channelConfig.SendHistoryStatus != nil {
if !autorun && !*channelConfig.SendHistoryStatus {
sendStatus = false
}
}
}

for _, channel := range subjectChannels {

hasPermsToRespond := hasPerms(channel.ID, discordgo.PermissionSendMessages)
if !autorun {
Expand Down Expand Up @@ -501,7 +501,7 @@ func handleHistory(commandingMessage *discordgo.Message, subjectChannelID string
}

// Process Message
downloadCount, filesize := handleMessage(message, false, true)
downloadCount, filesize := handleMessage(message, &channel, false, true)
if downloadCount > 0 {
totalDownloads += downloadCount
totalFilesize += filesize
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func main() {
var autoHistoryChannels []arh
// Compile list of channels to autorun history
for _, channel := range getAllRegisteredChannels() {
channelConfig := getSource(&discordgo.Message{ChannelID: channel})
channelConfig := getSource(&discordgo.Message{ChannelID: channel}, nil)
if channelConfig.AutoHistory != nil {
if *channelConfig.AutoHistory {
var autoHistoryChannel arh
Expand Down

0 comments on commit 45d9719

Please sign in to comment.