-
Notifications
You must be signed in to change notification settings - Fork 927
/
commands.go
89 lines (71 loc) · 2.35 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
package commands
//go:generate sqlboiler --no-hooks psql
//REMOVED: generate easyjson commands.go
import (
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/retryableredis"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/config"
)
var logger = common.GetPluginLogger(&Plugin{})
type CtxKey int
const (
CtxKeyCmdSettings CtxKey = iota
CtxKeyChannelOverride
CtxKeyMS
)
type MessageFilterFunc func(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, dcmd.ArgParserMW)
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 GetCommandPrefix(guild int64) (string, error) {
var prefix string
err := common.RedisPool.Do(retryableredis.Cmd(&prefix, "GET", "command_prefix:"+discordgo.StrID(guild)))
return prefix, err
}