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
}
listOfMessages := make(map[string][]*discordgo.Message)
for _, v := range guild.Channels {
if persistent.IgnoredChannels[v.ID] {
continue
}
beforeID := ""
for {
messages, err := dg.ChannelMessages(v.ID, 100, beforeID, "", "")
if err != nil {
util.ReportError(dg, err)
return
latestMessage := make(map[string]string)
for {
newMessagesFound := false
for _, v := range guild.Channels {
if persistent.IgnoredChannels[v.ID] {
continue
}
for _, msg := range messages {
if list, ok := listOfMessages[msg.Author.ID]; ok {
listOfMessages[msg.Author.ID] = append(list, msg)
} else {
listOfMessages[msg.Author.ID] = []*discordgo.Message{msg}
beforeID := ""
for {
messages, err := dg.ChannelMessages(v.ID, 100, beforeID, "", "")
if err != nil {
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
}
beforeID = messages[99].ID
}
fmt.Printf("[RECALC] Batch step reached in %v.\n", time.Now().Sub(startTime))
if !newMessagesFound {
break
}
}
fetchedTime := time.Now()