go: Do not include non-members in trust calculation

master
Luther Wen Xu 2019-10-11 23:20:12 +07:00
parent 8210ac59b2
commit fa8259fb07
Signed by untrusted user: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
3 changed files with 37 additions and 6 deletions

@ -45,7 +45,7 @@ func viewTrustLevel(s *discordgo.Session, m *discordgo.MessageCreate, command []
return
}
if len(command) > 1 {
value, err := getTrust(command[1])
value, err := getTrust(s, command[1])
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
@ -71,7 +71,10 @@ func viewTrustLevel(s *discordgo.Session, m *discordgo.MessageCreate, command []
return
}
for _, v := range members {
value, err := getTrust(v.User.ID)
value, err := getTrust(s, v.User.ID)
if err == errNotMember {
continue
}
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return

@ -1,13 +1,32 @@
package main
import "github.com/bwmarrin/discordgo"
import (
"errors"
"github.com/bwmarrin/discordgo"
)
var trustCache map[string]float64
func getTrust(discordID string) (float64, error) {
var errNotMember = errors.New("trust: only members have trust value")
func getTrust(s *discordgo.Session, discordID string) (float64, error) {
if v, ok := trustCache[discordID]; ok {
return v, nil
}
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
}
votes, err := getTrustVote(discordID)
if err != nil {
return 0.0, err
@ -27,7 +46,16 @@ func getTotalTrust(s *discordgo.Session) (float64, error) {
}
var total float64
for _, member := range members {
trust, err := getTrust(member.User.ID)
isMember := false
for _, role := range member.Roles {
if role == memberRoleID {
isMember = true
}
}
if !isMember {
continue
}
trust, err := getTrust(s, member.User.ID)
if err != nil {
return 0.0, err
}

@ -53,7 +53,7 @@ func checkForVoteResult(s *discordgo.Session, id int) {
if vote.Value == forceRejectionVote {
absoluteRejectionVote = true
}
trust, err := getTrust(vote.UserID)
trust, err := getTrust(s, vote.UserID)
if err != nil {
auditLog(s, fmt.Sprintf("Error while getting vote trust for user ID %d: %s", vote.UserID, err.Error()))
return