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/state/store"
|
2020-11-30 00:57:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Guild struct {
|
|
|
|
mut sync.RWMutex
|
|
|
|
guilds map[discord.GuildID]discord.Guild
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ store.GuildStore = (*Guild)(nil)
|
|
|
|
|
|
|
|
func NewGuild() *Guild {
|
|
|
|
return &Guild{
|
|
|
|
guilds: map[discord.GuildID]discord.Guild{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Guild) Reset() error {
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
|
|
|
s.guilds = map[discord.GuildID]discord.Guild{}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Guild) Guild(id discord.GuildID) (*discord.Guild, error) {
|
|
|
|
s.mut.RLock()
|
|
|
|
defer s.mut.RUnlock()
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
g, ok := s.guilds[id]
|
|
|
|
if ok {
|
|
|
|
return &g, nil
|
2020-11-30 00:57:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
return nil, store.ErrNotFound
|
2020-11-30 00:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Guild) Guilds() ([]discord.Guild, error) {
|
|
|
|
s.mut.RLock()
|
|
|
|
defer s.mut.RUnlock()
|
|
|
|
|
|
|
|
if len(s.guilds) == 0 {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
var gs = make([]discord.Guild, 0, len(s.guilds))
|
|
|
|
for _, g := range s.guilds {
|
|
|
|
gs = append(gs, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
return gs, nil
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
func (s *Guild) GuildSet(guild *discord.Guild, update bool) error {
|
|
|
|
cpy := *guild
|
|
|
|
|
2020-11-30 00:57:58 +00:00
|
|
|
s.mut.Lock()
|
2021-06-03 19:39:49 +00:00
|
|
|
if _, ok := s.guilds[guild.ID]; !ok || update {
|
2021-11-03 22:16:02 +00:00
|
|
|
s.guilds[guild.ID] = cpy
|
2021-06-03 19:39:49 +00:00
|
|
|
}
|
2020-11-30 00:57:58 +00:00
|
|
|
s.mut.Unlock()
|
2021-06-03 19:39:49 +00:00
|
|
|
|
2020-11-30 00:57:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Guild) GuildRemove(id discord.GuildID) error {
|
|
|
|
s.mut.Lock()
|
|
|
|
delete(s.guilds, id)
|
|
|
|
s.mut.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|