forked from pothtonswer/discordmuffin
Support getting the color of a user. (#299)
This commit is contained in:
parent
fb346787be
commit
db2f9eb29b
2 changed files with 56 additions and 0 deletions
42
state.go
42
state.go
|
@ -14,6 +14,7 @@ package discordgo
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
|
@ -749,3 +750,44 @@ func (s *State) UserChannelPermissions(userID, channelID string) (apermissions i
|
|||
|
||||
return memberPermissions(guild, channel, member), nil
|
||||
}
|
||||
|
||||
// UserColor returns the color of a user in a channel.
|
||||
// While colors are defined at a Guild level, determining for a channel is more useful in message handlers.
|
||||
// 0 is returned in cases of error, which is the color of @everyone.
|
||||
// userID : The ID of the user to calculate the color for.
|
||||
// channelID : The ID of the channel to calculate the color for.
|
||||
func (s *State) UserColor(userID, channelID string) int {
|
||||
if s == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
channel, err := s.Channel(channelID)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
guild, err := s.Guild(channel.GuildID)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
member, err := s.Member(guild.ID, userID)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
roles := Roles(guild.Roles)
|
||||
sort.Sort(roles)
|
||||
|
||||
for _, role := range roles {
|
||||
for _, roleID := range member.Roles {
|
||||
if role.ID == roleID {
|
||||
if role.Color != 0 {
|
||||
return role.Color
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
|
14
structs.go
14
structs.go
|
@ -252,6 +252,20 @@ type Role struct {
|
|||
Permissions int `json:"permissions"`
|
||||
}
|
||||
|
||||
type Roles []*Role
|
||||
|
||||
func (r Roles) Len() int {
|
||||
return len(r)
|
||||
}
|
||||
|
||||
func (r Roles) Less(i, j int) bool {
|
||||
return r[i].Position > r[j].Position
|
||||
}
|
||||
|
||||
func (r Roles) Swap(i, j int) {
|
||||
r[i], r[j] = r[j], r[i]
|
||||
}
|
||||
|
||||
// A VoiceState stores the voice states of Guilds
|
||||
type VoiceState struct {
|
||||
UserID string `json:"user_id"`
|
||||
|
|
Loading…
Reference in a new issue