forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
203 lines (166 loc) · 5.4 KB
/
commands.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package commands
//go:generate esc -o assets_gen.go -pkg commands -ignore ".go" assets/
//go:generate easyjson commands.go
import (
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/docs"
"github.com/mediocregopher/radix.v2/redis"
log "github.com/sirupsen/logrus"
)
type Plugin struct{}
func RegisterPlugin() {
plugin := &Plugin{}
common.RegisterPlugin(plugin)
err := common.GORM.AutoMigrate(&common.LoggedExecutedCommand{}).Error
if err != nil {
log.WithError(err).Error("Failed migrating database")
}
docs.AddPage("Commands", FSMustString(false, "/assets/help-page.md"), nil)
}
func (p *Plugin) Name() string {
return "Commands"
}
//easyjson:json
type ChannelCommandSetting struct {
Info *YAGCommand `json:"-"` // Used for template info
Cmd string `json:"cmd"`
CommandEnabled bool `json:"enabled"`
AutoDelete bool `json:"autodelete"`
RequiredRole string `json:"required_role"`
}
//easyjson:json
type ChannelOverride struct {
Settings []*ChannelCommandSetting `json:"settings"`
OverrideEnabled bool `json:"enabled"`
Channel string `json:"channel"`
ChannelName string `json:"-"` // Used for the template rendering
}
//easyjson:json
type CommandsConfig struct {
Prefix string `json:"-"` // Stored in a seperate key for speed
Global []*ChannelCommandSetting `json:"gloabl"`
ChannelOverrides []*ChannelOverride `json:"overrides"`
}
// Fills in the defaults for missing data, for when users create channels or commands are added
func CheckChannelsConfig(conf *CommandsConfig, channels []*discordgo.Channel) {
commands := CommandSystem.Root.Commands
if conf.Global == nil {
conf.Global = []*ChannelCommandSetting{}
}
if conf.ChannelOverrides == nil {
conf.ChannelOverrides = []*ChannelOverride{}
}
ROOT:
for _, channel := range channels {
if channel.Type != discordgo.ChannelTypeGuildText {
continue
}
strCID := discordgo.StrID(channel.ID)
// Look for an existing override
for _, override := range conf.ChannelOverrides {
// Found an existing override, check if it has all the commands
if strCID == override.Channel {
override.Settings = checkCommandSettings(override.Settings, commands, false)
override.ChannelName = channel.Name // Update name if changed
continue ROOT
}
}
// Not found, create a default override
override := &ChannelOverride{
Settings: []*ChannelCommandSetting{},
OverrideEnabled: false,
Channel: strCID,
ChannelName: channel.Name,
}
// Fill in default command settings
override.Settings = checkCommandSettings(override.Settings, commands, false)
conf.ChannelOverrides = append(conf.ChannelOverrides, override)
}
newOverrides := make([]*ChannelOverride, 0, len(conf.ChannelOverrides))
// Check for removed channels
for _, override := range conf.ChannelOverrides {
for _, channel := range channels {
if channel.Type != discordgo.ChannelTypeGuildText {
continue
}
if discordgo.StrID(channel.ID) == override.Channel {
newOverrides = append(newOverrides, override)
break
}
}
}
conf.ChannelOverrides = newOverrides
// Check the global settings
conf.Global = checkCommandSettings(conf.Global, commands, true)
}
// Checks a single list of ChannelCommandSettings and applies defaults if not found
func checkCommandSettings(settings []*ChannelCommandSetting, commands []*dcmd.RegisteredCommand, defaultEnabled bool) []*ChannelCommandSetting {
ROOT:
for _, registeredCmd := range commands {
cast, ok := registeredCmd.Command.(*YAGCommand)
if !ok {
continue
}
for _, settingsCmd := range settings {
if cast.Name == settingsCmd.Cmd {
// Bingo
settingsCmd.Info = cast
continue ROOT
}
}
// Not found, add it to the list of overrides
settingsCmd := &ChannelCommandSetting{
Cmd: cast.Name,
CommandEnabled: defaultEnabled,
AutoDelete: false,
Info: cast,
}
settings = append(settings, settingsCmd)
}
newSettings := make([]*ChannelCommandSetting, 0, len(settings))
// Check for commands that have been removed (e.g the config contains commands from an older version)
for _, settingsCmd := range settings {
for _, registeredCmd := range commands {
cast, ok := registeredCmd.Command.(*YAGCommand)
if !ok {
continue
}
if cast.Name == settingsCmd.Cmd {
newSettings = append(newSettings, settingsCmd)
break
}
}
}
return newSettings
}
func GetConfig(client *redis.Client, guild int64, channels []*discordgo.Channel) *CommandsConfig {
var config *CommandsConfig
err := common.GetRedisJson(client, "commands_settings:"+discordgo.StrID(guild), &config)
if err != nil {
log.WithError(err).Error("Error retrieving command settings")
}
if config == nil {
config = &CommandsConfig{}
}
prefix, err := GetCommandPrefix(client, guild)
if err != nil {
// Continue as normal with defaults
log.WithError(err).Error("Error fetching command prefix")
}
config.Prefix = prefix
// Fill in defaults
CheckChannelsConfig(config, channels)
return config
}
func GetCommandPrefix(client *redis.Client, guild int64) (string, error) {
reply := client.Cmd("GET", "command_prefix:"+discordgo.StrID(guild))
if reply.Err != nil {
return "", reply.Err
}
if reply.IsType(redis.Nil) {
return "", nil
}
return reply.Str()
}