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
|
package config
import (
"strings"
"github.com/anhgelus/gokord"
discordgo "github.com/nyttikord/gokord"
)
type GuildConfig struct {
ID uint `gorm:"primarykey"`
GuildID string `gorm:"not null;unique"`
XpRoles []XpRole
DisabledChannels string
FallbackChannel string
DaysXPRemains uint `gorm:"default:90"` // 30 * 3 = 90 (three months)
}
func GetGuildConfig(guildID string) *GuildConfig {
cfg := GuildConfig{GuildID: guildID}
if err := cfg.Load(); err != nil {
panic(err)
}
return &cfg
}
func (cfg *GuildConfig) Load() error {
return gokord.DB.Where("guild_id = ?", cfg.GuildID).Preload("XpRoles").FirstOrCreate(cfg).Error
}
func (cfg *GuildConfig) Save() error {
return gokord.DB.Save(cfg).Error
}
func (cfg *GuildConfig) IsDisabled(s *discordgo.Session, channelID string) bool {
ok := true
s.LogInfo("Configuration: %s", cfg.DisabledChannels)
s.LogInfo("Channel %s, ok %t", channelID, ok)
for channelID != "" && ok {
s.LogInfo("Channel %s, ok %t", channelID, ok)
ok = !strings.Contains(cfg.DisabledChannels, channelID)
c, err := s.State.Channel(channelID)
if err != nil {
s.LogError(err, "Unable to find channel %s in state", c)
c, err = s.ChannelAPI().Channel(channelID)
if err == nil {
s.State.ChannelAdd(c)
} else {
s.LogError(err, "Unable to fetch channel %s", s)
return false
}
}
if err != nil {
return false
}
channelID = c.ParentID
}
return !ok
}
func (cfg *GuildConfig) FindXpRole(roleID string) (int, *XpRole) {
for i, r := range cfg.XpRoles {
if r.RoleID == roleID {
return i, &r
}
}
return 0, nil
}
func (cfg *GuildConfig) FindXpRoleID(ID uint) (int, *XpRole) {
for i, r := range cfg.XpRoles {
if r.ID == ID {
return i, &r
}
}
return -1, nil
}
|