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,6 +25,9 @@ func RecalculateEverything(dg *discordgo.Session, guildID string) {
return
}
listOfMessages := make(map[string][]*discordgo.Message)
latestMessage := make(map[string]string)
for {
newMessagesFound := false
for _, v := range guild.Channels {
if persistent.IgnoredChannels[v.ID] {
continue
@ -36,19 +39,33 @@ func RecalculateEverything(dg *discordgo.Session, guildID string) {
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 {
if len(messages) < 100 || dupe {
break
}
beforeID = messages[99].ID
}
}
fmt.Printf("[RECALC] Batch step reached in %v.\n", time.Now().Sub(startTime))
if !newMessagesFound {
break
}
}
fetchedTime := time.Now()
totalXP := make(map[string]int64)
for k, v := range listOfMessages {