feat: Catch new messages during level recalculation

The code now goes through all the channels again until no new messages are found.
master
Luther Wen Xu 2020-09-15 00:08:15 +07:00
parent 1605f43d9f
commit 877cf55380
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
1 changed files with 36 additions and 19 deletions

@ -25,28 +25,45 @@ func RecalculateEverything(dg *discordgo.Session, guildID string) {
return return
} }
listOfMessages := make(map[string][]*discordgo.Message) listOfMessages := make(map[string][]*discordgo.Message)
for _, v := range guild.Channels { latestMessage := make(map[string]string)
if persistent.IgnoredChannels[v.ID] { for {
continue newMessagesFound := false
} for _, v := range guild.Channels {
beforeID := "" if persistent.IgnoredChannels[v.ID] {
for { continue
messages, err := dg.ChannelMessages(v.ID, 100, beforeID, "", "")
if err != nil {
util.ReportError(dg, err)
return
} }
for _, msg := range messages { beforeID := ""
if list, ok := listOfMessages[msg.Author.ID]; ok { for {
listOfMessages[msg.Author.ID] = append(list, msg) messages, err := dg.ChannelMessages(v.ID, 100, beforeID, "", "")
} else { if err != nil {
listOfMessages[msg.Author.ID] = []*discordgo.Message{msg} util.ReportError(dg, err)
return
} }
if beforeID == "" && len(messages) > 0 && messages[0].ID != latestMessage[v.ID] {
newMessagesFound = true
latestMessage[v.ID] = messages[0].ID
}
dupe := false
for _, msg := range messages {
if msg.ID == latestMessage[v.ID] {
dupe = true
break
}
if list, ok := listOfMessages[msg.Author.ID]; ok {
listOfMessages[msg.Author.ID] = append(list, msg)
} else {
listOfMessages[msg.Author.ID] = []*discordgo.Message{msg}
}
}
if len(messages) < 100 || dupe {
break
}
beforeID = messages[99].ID
} }
if len(messages) < 100 { }
break fmt.Printf("[RECALC] Batch step reached in %v.\n", time.Now().Sub(startTime))
} if !newMessagesFound {
beforeID = messages[99].ID break
} }
} }
fetchedTime := time.Now() fetchedTime := time.Now()