2019-10-10 13:59:00 +07:00
|
|
|
package main
|
|
|
|
|
2019-10-11 12:25:09 +07:00
|
|
|
import "github.com/bwmarrin/discordgo"
|
|
|
|
|
|
|
|
var trustCache map[string]float64
|
|
|
|
|
|
|
|
func getTrust(discordID string) (float64, error) {
|
|
|
|
if v, ok := trustCache[discordID]; ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
votes, err := getTrustVote(discordID)
|
|
|
|
if err != nil {
|
|
|
|
return 0.0, err
|
|
|
|
}
|
|
|
|
votes = append(votes, 3)
|
|
|
|
var total float64
|
|
|
|
for _, v := range votes {
|
|
|
|
total += float64(v)
|
|
|
|
}
|
|
|
|
return total / float64(len(votes)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTotalTrust(s *discordgo.Session) (float64, error) {
|
|
|
|
members, err := getAllMembers(s)
|
|
|
|
if err != nil {
|
|
|
|
return 0.0, err
|
|
|
|
}
|
|
|
|
var total float64
|
|
|
|
for _, member := range members {
|
|
|
|
trust, err := getTrust(member.User.ID)
|
|
|
|
if err != nil {
|
|
|
|
return 0.0, err
|
|
|
|
}
|
|
|
|
total += trust
|
|
|
|
}
|
|
|
|
return total, nil
|
2019-10-10 13:59:00 +07:00
|
|
|
}
|
|
|
|
|
2019-10-11 12:25:09 +07:00
|
|
|
func getAllMembers(s *discordgo.Session) ([]*discordgo.Member, error) {
|
|
|
|
initial, err := s.GuildMembers(guildID, "", 1000)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
next := initial[len(initial)-1].User.ID
|
|
|
|
for next != "" {
|
|
|
|
nextArray, err := s.GuildMembers(guildID, next, 1000)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(nextArray) == 0 {
|
|
|
|
next = ""
|
|
|
|
} else {
|
|
|
|
next = nextArray[len(nextArray)-1].User.ID
|
|
|
|
initial = append(initial, nextArray...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return initial, nil
|
2019-10-10 13:59:00 +07:00
|
|
|
}
|