2020-11-30 00:57:58 +00:00
|
|
|
package defaultstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/internal/moreatomic"
|
|
|
|
"github.com/diamondburned/arikawa/v3/state/store"
|
2020-11-30 00:57:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Role struct {
|
|
|
|
guilds moreatomic.Map
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ store.RoleStore = (*Role)(nil)
|
|
|
|
|
|
|
|
type roles struct {
|
2021-11-03 22:16:02 +00:00
|
|
|
mut sync.RWMutex
|
2020-11-30 00:57:58 +00:00
|
|
|
roles map[discord.RoleID]discord.Role
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRole() *Role {
|
|
|
|
return &Role{
|
|
|
|
guilds: *moreatomic.NewMap(func() interface{} {
|
|
|
|
return &roles{
|
|
|
|
roles: make(map[discord.RoleID]discord.Role, 1),
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Role) Reset() error {
|
|
|
|
return s.guilds.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Role) Role(guildID discord.GuildID, roleID discord.RoleID) (*discord.Role, error) {
|
|
|
|
iv, ok := s.guilds.Load(guildID)
|
|
|
|
if !ok {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
rs := iv.(*roles)
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
rs.mut.RLock()
|
|
|
|
defer rs.mut.RUnlock()
|
2020-11-30 00:57:58 +00:00
|
|
|
|
|
|
|
r, ok := rs.roles[roleID]
|
|
|
|
if ok {
|
|
|
|
return &r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Role) Roles(guildID discord.GuildID) ([]discord.Role, error) {
|
|
|
|
iv, ok := s.guilds.Load(guildID)
|
|
|
|
if !ok {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
rs := iv.(*roles)
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
rs.mut.RLock()
|
|
|
|
defer rs.mut.RUnlock()
|
2020-11-30 00:57:58 +00:00
|
|
|
|
|
|
|
var roles = make([]discord.Role, 0, len(rs.roles))
|
|
|
|
for _, role := range rs.roles {
|
|
|
|
roles = append(roles, role)
|
|
|
|
}
|
|
|
|
|
|
|
|
return roles, nil
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
func (s *Role) RoleSet(guildID discord.GuildID, role *discord.Role, update bool) error {
|
2020-11-30 00:57:58 +00:00
|
|
|
iv, _ := s.guilds.LoadOrStore(guildID)
|
|
|
|
|
|
|
|
rs := iv.(*roles)
|
|
|
|
|
|
|
|
rs.mut.Lock()
|
2021-06-03 19:39:49 +00:00
|
|
|
if _, ok := rs.roles[role.ID]; !ok || update {
|
2021-11-03 22:16:02 +00:00
|
|
|
rs.roles[role.ID] = *role
|
2021-06-03 19:39:49 +00:00
|
|
|
}
|
2020-11-30 00:57:58 +00:00
|
|
|
rs.mut.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Role) RoleRemove(guildID discord.GuildID, roleID discord.RoleID) error {
|
|
|
|
iv, ok := s.guilds.Load(guildID)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rs := iv.(*roles)
|
|
|
|
|
|
|
|
rs.mut.Lock()
|
|
|
|
delete(rs.roles, roleID)
|
|
|
|
rs.mut.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|