github.com/diamondburned/arikawa/v2@v2.1.0/state/store/defaultstore/guild.go (about) 1 package defaultstore 2 3 import ( 4 "sync" 5 6 "github.com/diamondburned/arikawa/v2/discord" 7 "github.com/diamondburned/arikawa/v2/state/store" 8 ) 9 10 type Guild struct { 11 mut sync.RWMutex 12 guilds map[discord.GuildID]discord.Guild 13 } 14 15 var _ store.GuildStore = (*Guild)(nil) 16 17 func NewGuild() *Guild { 18 return &Guild{ 19 guilds: map[discord.GuildID]discord.Guild{}, 20 } 21 } 22 23 func (s *Guild) Reset() error { 24 s.mut.Lock() 25 defer s.mut.Unlock() 26 27 s.guilds = map[discord.GuildID]discord.Guild{} 28 29 return nil 30 } 31 32 func (s *Guild) Guild(id discord.GuildID) (*discord.Guild, error) { 33 s.mut.RLock() 34 defer s.mut.RUnlock() 35 36 ch, ok := s.guilds[id] 37 if !ok { 38 return nil, store.ErrNotFound 39 } 40 41 // implicit copy 42 return &ch, nil 43 } 44 45 func (s *Guild) Guilds() ([]discord.Guild, error) { 46 s.mut.RLock() 47 defer s.mut.RUnlock() 48 49 if len(s.guilds) == 0 { 50 return nil, store.ErrNotFound 51 } 52 53 var gs = make([]discord.Guild, 0, len(s.guilds)) 54 for _, g := range s.guilds { 55 gs = append(gs, g) 56 } 57 58 return gs, nil 59 } 60 61 func (s *Guild) GuildSet(guild discord.Guild) error { 62 s.mut.Lock() 63 s.guilds[guild.ID] = guild 64 s.mut.Unlock() 65 return nil 66 } 67 68 func (s *Guild) GuildRemove(id discord.GuildID) error { 69 s.mut.Lock() 70 delete(s.guilds, id) 71 s.mut.Unlock() 72 return nil 73 }