From b089488d78ff1acdee603e030bf70f02a17df3cb Mon Sep 17 00:00:00 2001 From: 5elenay <5elenay@protonmail.com> Date: Sat, 28 Aug 2021 18:39:47 +0300 Subject: [PATCH] moved cache functions to the cache.go --- cache.go | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ client.go | 120 ---------------------------------------------------- 2 files changed, 123 insertions(+), 120 deletions(-) create mode 100644 cache.go diff --git a/cache.go b/cache.go new file mode 100644 index 0000000..a9e5d3a --- /dev/null +++ b/cache.go @@ -0,0 +1,123 @@ +package revoltgo + +import "fmt" + +// Client cache struct. +type Cache struct { + Users []*User `json:"users"` + Servers []*Server `json:"servers"` + Channels []*Channel `json:"channels"` + Members []*Member `json:"members"` +} + +// Get a channel from cache by Id. +// Will return an empty channel struct if not found. +func (c *Cache) GetChannel(id string) *Channel { + for _, i := range c.Channels { + if i.Id == id { + return i + } + } + + return &Channel{} +} + +// Get a server from cache by Id. +// Will return an empty server struct if not found. +func (c *Cache) GetServer(id string) *Server { + for _, i := range c.Servers { + if i.Id == id { + return i + } + } + + return &Server{} +} + +// Get an user from cache by Id. +// Will return an empty user struct if not found. +func (c *Cache) GetUser(id string) *User { + for _, i := range c.Users { + if i.Id == id { + return i + } + } + + return &User{} +} + +// Get a member from cache by Id. +// Will return an empty member struct if not found. +func (c *Cache) GetMember(id string) *Member { + for _, i := range c.Members { + if i.Informations.UserId == id { + return i + } + } + + return &Member{} +} + +// Remove a channel from cache by Id. +// Will not delete the channel, just deletes the channel from cache. +// Will change the entire channel cache order! +func (c *Cache) RemoveChannel(id string) error { + for i, v := range c.Channels { + if v.Id == id { + c.Channels[i] = c.Channels[len(c.Channels)-1] + c.Channels = c.Channels[:len(c.Channels)-1] + + return nil + } + } + + return fmt.Errorf("channel not found") +} + +// Remove a server from cache by Id. +// Will not delete the server, just deletes the server from cache. +// Will change the entire server cache order! +func (c *Cache) RemoveServer(id string) error { + for i, v := range c.Servers { + if v.Id == id { + c.Servers[i] = c.Servers[len(c.Servers)-1] + c.Servers = c.Servers[:len(c.Servers)-1] + + return nil + } + } + + return fmt.Errorf("server not found") +} + +// Remove an user from cache by Id. +// Will not delete the user, just deletes the user from cache. +// Will change the entire user cache order! +func (c *Cache) RemoveUser(id string) error { + for i, v := range c.Users { + if v.Id == id { + c.Users[i] = c.Users[len(c.Users)-1] + c.Users = c.Users[:len(c.Users)-1] + + return nil + } + } + + return fmt.Errorf("user not found") +} + +// Remove a member from cache by Id. +// Will not delete the member, just deletes the member from cache. +// Will change the entire member cache order! +func (c *Cache) RemoveMember(id string) error { + for i, v := range c.Members { + if v.Informations.UserId == id { + c.Members[i] = c.Members[len(c.Members)-1] + c.Members = c.Members[:len(c.Members)-1] + + return nil + } + } + + return fmt.Errorf("member not found") +} diff --git a/client.go b/client.go index 2fe3804..587b1f8 100644 --- a/client.go +++ b/client.go @@ -36,14 +36,6 @@ type Client struct { OnServerDeleteFunctions []func(server_id string) } -// Client cache struct. -type Cache struct { - Users []*User `json:"users"` - Servers []*Server `json:"servers"` - Channels []*Channel `json:"channels"` - Members []*Member `json:"members"` -} - // Self bot struct. type SelfBot struct { Email string `json:"-"` @@ -212,115 +204,3 @@ func (c *Client) FetchDirectMessages() ([]*Channel, error) { return dmChannels, nil } - -// Get a channel from cache by Id. -// Will return an empty channel struct if not found. -func (c *Cache) GetChannel(id string) *Channel { - for _, i := range c.Channels { - if i.Id == id { - return i - } - } - - return &Channel{} -} - -// Get a server from cache by Id. -// Will return an empty server struct if not found. -func (c *Cache) GetServer(id string) *Server { - for _, i := range c.Servers { - if i.Id == id { - return i - } - } - - return &Server{} -} - -// Get an user from cache by Id. -// Will return an empty user struct if not found. -func (c *Cache) GetUser(id string) *User { - for _, i := range c.Users { - if i.Id == id { - return i - } - } - - return &User{} -} - -// Get a member from cache by Id. -// Will return an empty member struct if not found. -func (c *Cache) GetMember(id string) *Member { - for _, i := range c.Members { - if i.Informations.UserId == id { - return i - } - } - - return &Member{} -} - -// Remove a channel from cache by Id. -// Will not delete the channel, just deletes the channel from cache. -// Will change the entire channel cache order! -func (c *Cache) RemoveChannel(id string) error { - for i, v := range c.Channels { - if v.Id == id { - c.Channels[i] = c.Channels[len(c.Channels)-1] - c.Channels = c.Channels[:len(c.Channels)-1] - - return nil - } - } - - return fmt.Errorf("channel not found") -} - -// Remove a server from cache by Id. -// Will not delete the server, just deletes the server from cache. -// Will change the entire server cache order! -func (c *Cache) RemoveServer(id string) error { - for i, v := range c.Servers { - if v.Id == id { - c.Servers[i] = c.Servers[len(c.Servers)-1] - c.Servers = c.Servers[:len(c.Servers)-1] - - return nil - } - } - - return fmt.Errorf("server not found") -} - -// Remove an user from cache by Id. -// Will not delete the user, just deletes the user from cache. -// Will change the entire user cache order! -func (c *Cache) RemoveUser(id string) error { - for i, v := range c.Users { - if v.Id == id { - c.Users[i] = c.Users[len(c.Users)-1] - c.Users = c.Users[:len(c.Users)-1] - - return nil - } - } - - return fmt.Errorf("user not found") -} - -// Remove a member from cache by Id. -// Will not delete the member, just deletes the member from cache. -// Will change the entire member cache order! -func (c *Cache) RemoveMember(id string) error { - for i, v := range c.Members { - if v.Informations.UserId == id { - c.Members[i] = c.Members[len(c.Members)-1] - c.Members = c.Members[:len(c.Members)-1] - - return nil - } - } - - return fmt.Errorf("member not found") -}