/
channels.go
82 lines (72 loc) · 2.11 KB
/
channels.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package dgrs
import "github.com/bwmarrin/discordgo"
// SetChannel sets the given channel object to the cache.
func (s *State) SetChannel(channel *discordgo.Channel) (err error) {
err = s.set(s.joinKeys(KeyChannel, channel.ID), channel, s.getLifetime(channel))
return
}
// Channel tries to retrieve a channel by the given channel ID.
//
// If no channel was found and FetchAndStore is enabled, the object
// will be tried to be retrieved from the API. When this was successful,
// it is stored in the cache and the object is returned.
//
// Otherwise, if the object was not found in the cache and FetchAndStore
// is disabled, nil is returned.
func (s *State) Channel(id string) (v *discordgo.Channel, err error) {
v = &discordgo.Channel{}
ok, err := s.get(s.joinKeys(KeyChannel, id), v)
if !ok {
if s.options.FetchAndStore {
if v, err = s.session.Channel(id); v != nil && err == nil {
err = s.SetChannel(v)
}
} else {
v = nil
}
}
return
}
// Channels returns a list of channels of the given guild ID
// which are stored in the cache at the given moment.
func (s *State) Channels(guildID string, forceFetch ...bool) (v []*discordgo.Channel, err error) {
v = make([]*discordgo.Channel, 0)
if err = s.list(s.joinKeys(KeyChannel, "*"), &v); err != nil {
return
}
vg := make([]*discordgo.Channel, 0)
if guildID != "" {
for _, c := range v {
if c.GuildID == guildID {
vg = append(vg, c)
}
}
v = vg
if (len(v) == 0 || optBool(forceFetch)) && s.options.FetchAndStore {
if v, err = s.session.GuildChannels(guildID); err != nil {
return
}
for _, c := range v {
if err = s.SetChannel(c); err != nil {
return
}
}
}
}
return
}
// RemoveChannel removes a channel object from the cache by the given ID.
//
// When dehydrate is passed as true, messages in this channel which
// are cached are purged from cache as well.
func (s *State) RemoveChannel(id string, dehydrate ...bool) (err error) {
if err = s.del(s.joinKeys(KeyChannel, id)); err != nil {
return
}
if optBool(dehydrate) {
if err = s.delPattern(s.joinKeys(KeyMessage, id, "*")); err != nil {
return
}
}
return
}