-
Notifications
You must be signed in to change notification settings - Fork 927
/
commands.go
157 lines (126 loc) · 4.1 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
package commands
//go:generate sqlboiler --no-hooks psql
//REMOVED: generate easyjson commands.go
import (
"context"
"github.com/botlabs-gg/yagpdb/bot/eventsystem"
"github.com/botlabs-gg/yagpdb/commands/models"
"github.com/botlabs-gg/yagpdb/common"
"github.com/botlabs-gg/yagpdb/common/config"
"github.com/botlabs-gg/yagpdb/common/featureflags"
"github.com/jonas747/dcmd/v4"
"github.com/jonas747/discordgo/v2"
"github.com/mediocregopher/radix/v3"
"github.com/volatiletech/sqlboiler/queries/qm"
)
var logger = common.GetPluginLogger(&Plugin{})
type CtxKey int
const (
CtxKeyCmdSettings CtxKey = iota
CtxKeyChannelOverride
CtxKeyExecutedByCC
)
type MessageFilterFunc func(evt *eventsystem.EventData, msg *discordgo.Message) bool
var (
confSetTyping = config.RegisterOption("yagpdb.commands.typing", "Wether to set typing or not when running commands", true)
)
// These functions are called on every message, and should return true if the message should be checked for commands, false otherwise
var MessageFilterFuncs []MessageFilterFunc
type Plugin struct{}
func (p *Plugin) PluginInfo() *common.PluginInfo {
return &common.PluginInfo{
Name: "Commands",
SysName: "commands",
Category: common.PluginCategoryCore,
}
}
func RegisterPlugin() {
plugin := &Plugin{}
common.RegisterPlugin(plugin)
err := common.GORM.AutoMigrate(&common.LoggedExecutedCommand{}).Error
if err != nil {
logger.WithError(err).Fatal("Failed migrating logged commands database")
}
common.InitSchemas("commands", DBSchemas...)
}
type CommandProvider interface {
// This is where you should register your commands, called on both the webserver and the bot
AddCommands()
}
func InitCommands() {
// Setup the command system
CommandSystem = &dcmd.System{
Root: &dcmd.Container{
HelpTitleEmoji: "ℹ️",
HelpColor: 0xbeff7a,
RunInDM: true,
IgnoreBots: true,
},
ResponseSender: &dcmd.StdResponseSender{LogErrors: true},
Prefix: &Plugin{},
}
// We have our own middleware before the argument parsing, this is to check for things such as whether or not the command is enabled at all
CommandSystem.Root.AddMidlewares(YAGCommandMiddleware)
CommandSystem.Root.AddCommand(cmdHelp, cmdHelp.GetTrigger())
CommandSystem.Root.AddCommand(cmdPrefix, cmdPrefix.GetTrigger())
for _, v := range common.Plugins {
if adder, ok := v.(CommandProvider); ok {
adder.AddCommands()
}
}
}
func GetCommandPrefixRedis(guild int64) (string, error) {
var prefix string
err := common.RedisPool.Do(radix.Cmd(&prefix, "GET", "command_prefix:"+discordgo.StrID(guild)))
if err == nil && prefix == "" {
prefix = defaultCommandPrefix()
}
return prefix, err
}
var _ featureflags.PluginWithFeatureFlags = (*Plugin)(nil)
const (
featureFlagHasCustomPrefix = "commands_has_custom_prefix"
featureFlagHasCustomOverrides = "commands_has_custom_overrides"
)
func (p *Plugin) UpdateFeatureFlags(guildID int64) ([]string, error) {
prefix, err := GetCommandPrefixRedis(guildID)
if err != nil {
return nil, err
}
var flags []string
if defaultCommandPrefix() != prefix {
flags = append(flags, featureFlagHasCustomPrefix)
}
channelOverrides, err := models.CommandsChannelsOverrides(qm.Where("guild_id=?", guildID), qm.Load("CommandsCommandOverrides")).AllG(context.Background())
if err != nil {
return nil, err
}
if isCustomOverrides(channelOverrides) {
flags = append(flags, featureFlagHasCustomOverrides)
}
return flags, nil
}
func isCustomOverrides(overrides []*models.CommandsChannelsOverride) bool {
if len(overrides) == 0 {
return false
}
if len(overrides) == 1 && overrides[0].Global {
// check if this is default
g := overrides[0]
if !g.AutodeleteResponse &&
!g.AutodeleteTrigger &&
g.CommandsEnabled &&
len(g.RequireRoles) == 0 &&
len(g.IgnoreRoles) == 0 &&
len(g.R.CommandsCommandOverrides) == 0 {
return false
}
}
return true
}
func (p *Plugin) AllFeatureFlags() []string {
return []string{
featureFlagHasCustomPrefix, // Set if the server has a custom command prefix
featureFlagHasCustomOverrides, // set if the server has custom command and/or channel overrides
}
}