2019-10-10 13:59:00 +07:00
|
|
|
package main
|
|
|
|
|
2019-10-11 15:20:12 +07:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
)
|
2019-10-11 12:25:09 +07:00
|
|
|
|
|
|
|
var trustCache map[string]float64
|
|
|
|
|
2019-10-11 15:20:12 +07:00
|
|
|
var errNotMember = errors.New("trust: only members have trust value")
|
|
|
|
|
|
|
|
func getTrust(s *discordgo.Session, discordID string) (float64, error) {
|
2019-10-11 12:25:09 +07:00
|
|
|
if v, ok := trustCache[discordID]; ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
2019-10-11 15:20:12 +07:00
|
|
|
member, err := s.GuildMember(guildID, discordID)
|
|
|
|
if err != nil {
|
|
|
|
return 0.0, err
|
|
|
|
}
|
|
|
|
isMember := false
|
|
|
|
for _, role := range member.Roles {
|
|
|
|
if role == memberRoleID {
|
|
|
|
isMember = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isMember {
|
|
|
|
return 0.0, errNotMember
|
|
|
|
}
|
2019-10-11 12:25:09 +07:00
|
|
|
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 {
|
2019-10-11 15:20:12 +07:00
|
|
|
isMember := false
|
|
|
|
for _, role := range member.Roles {
|
|
|
|
if role == memberRoleID {
|
|
|
|
isMember = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isMember {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
trust, err := getTrust(s, member.User.ID)
|
2019-10-11 12:25:09 +07:00
|
|
|
if err != nil {
|
|
|
|
return 0.0, err
|
|
|
|
}
|
|
|
|
total += trust
|
|
|
|
}
|
|
|
|
return total, nil
|
2019-10-10 13:59:00 +07:00
|
|
|
}
|
2019-10-11 14:54:31 +07:00
|
|
|
|
|
|
|
func updateTrust(sourceID, targetID string, voteValue int) error {
|
2019-10-12 06:40:46 +07:00
|
|
|
err := updateDbTrust(sourceID, targetID, voteValue, false)
|
2019-10-11 14:54:31 +07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(trustCache, targetID)
|
|
|
|
return nil
|
|
|
|
}
|