forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_bot.go
134 lines (109 loc) · 3.49 KB
/
plugin_bot.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
package commands
import (
"fmt"
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/common"
log "github.com/sirupsen/logrus"
)
var (
CommandSystem *dcmd.System
)
func (p *Plugin) InitBot() {
CommandSystem = dcmd.NewStandardSystem("")
CommandSystem.Prefix = p
CommandSystem.State = bot.State
CommandSystem.Root.RunInDM = true
CommandSystem.Root.IgnoreBots = true
// CommandSystem = commandsystem.NewSystem(nil, "")
// CommandSystem.SendError = false
// CommandSystem.CensorError = CensorError
// CommandSystem.State = bot.State
// CommandSystem.DefaultDMHandler = &commandsystem.Command{
// Run: func(data *commandsystem.ExecData) (interface{}, error) {
// return "Unknwon command, only a subset of commands are available in dms.", nil
// },
// }
// CommandSystem.Prefix = p
// CommandSystem.RegisterCommands(cmdHelp)
CommandSystem.Root.AddCommand(cmdHelp, cmdHelp.GetTrigger())
eventsystem.AddHandler(bot.RedisWrapper(HandleGuildCreate), eventsystem.EventGuildCreate)
eventsystem.AddHandler(handleMsgCreate, eventsystem.EventMessageCreate)
}
func AddRootCommands(cmds ...*YAGCommand) {
for _, v := range cmds {
CommandSystem.Root.AddCommand(v, v.GetTrigger())
}
}
func handleMsgCreate(evt *eventsystem.EventData) {
CommandSystem.HandleMessageCreate(common.BotSession, evt.MessageCreate)
}
func (p *Plugin) Prefix(data *dcmd.Data) string {
client, err := common.RedisPool.Get()
if err != nil {
log.WithError(err).Error("Failed retrieving redis connection from pool")
return ""
}
defer common.RedisPool.Put(client)
prefix, err := GetCommandPrefix(client, data.GS.ID())
if err != nil {
log.WithError(err).Error("Failed retrieving commands prefix")
}
return prefix
}
var cmdHelp = &YAGCommand{
Name: "Help",
Aliases: []string{"commands", "h", "how", "command"},
Description: "Shows help about all or one specific command",
CmdCategory: CategoryGeneral,
RunInDM: true,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "command", Type: dcmd.String},
},
RunFunc: cmdFuncHelp,
Cooldown: 10,
}
func CmdNotFound(search string) string {
return fmt.Sprintf("Couldn't find command %q", search)
}
func cmdFuncHelp(data *dcmd.Data) (interface{}, error) {
target := data.Args[0].Str()
var resp []*discordgo.MessageEmbed
// Send the targetted help in the channel it was requested in
resp = dcmd.GenerateTargettedHelp(target, data, data.ContainerChain[0], &dcmd.StdHelpFormatter{})
if len(resp) < 1 {
return CmdNotFound(target), nil
}
if len(resp) == 1 {
// Send short help in same channel
return resp, nil
}
// Send full help in DM
channel, err := bot.GetCreatePrivateChannel(data.Msg.Author.ID)
if err != nil {
return "Something went wrong", err
}
for _, v := range resp {
common.BotSession.ChannelMessageSendEmbed(channel.ID, v)
}
return nil, nil
}
func HandleGuildCreate(evt *eventsystem.EventData) {
client := bot.ContextRedis(evt.Context())
g := evt.GuildCreate
prefixExists, err := common.RedisBool(client.Cmd("EXISTS", "command_prefix:"+discordgo.StrID(g.ID)))
if err != nil {
log.WithError(err).Error("Failed checking if prefix exists")
return
}
if !prefixExists {
defaultPrefix := "-"
if common.Testing {
defaultPrefix = "("
}
client.Cmd("SET", "command_prefix:"+discordgo.StrID(g.ID), defaultPrefix)
log.WithField("guild", g.ID).WithField("g_name", g.Name).Info("Set command prefix to default (" + defaultPrefix + ")")
}
}