-
Notifications
You must be signed in to change notification settings - Fork 927
/
plugin_bot.go
234 lines (189 loc) · 6.03 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package commands
import (
"context"
"fmt"
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/common"
"github.com/mediocregopher/radix.v3"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"time"
)
var (
CommandSystem *dcmd.System
)
var _ bot.BotInitHandler = (*Plugin)(nil)
func (p *Plugin) BotInit() {
eventsystem.AddHandler(HandleGuildCreate, eventsystem.EventGuildCreate)
eventsystem.AddHandler(handleMsgCreate, eventsystem.EventMessageCreate)
CommandSystem.State = bot.State
}
func YAGCommandMiddleware(inner dcmd.RunFunc) dcmd.RunFunc {
return func(data *dcmd.Data) (interface{}, error) {
yc, ok := data.Cmd.Command.(*YAGCommand)
if !ok {
resp, err := inner(data)
// Filter the response
if data.GS != nil {
if resp == nil && err != nil {
err = errors.New(FilterResp(err.Error(), data.GS.ID).(string))
} else if resp != nil {
resp = FilterResp(resp, data.GS.ID)
}
}
return resp, err
}
if data.GS != nil {
ms, err := bot.GetMember(data.GS.ID, data.Msg.Author.ID)
if err != nil {
return nil, errors.WithMessage(err, "failed fetching member")
}
data = data.WithContext(context.WithValue(data.Context(), CtxKeyMS, ms))
}
// Check if the user can execute the command
canExecute, resp, settings, err := yc.checkCanExecuteCommand(data, data.CS)
if resp != "" {
// yc.PostCommandExecuted(settings, data, "", errors.WithMessage(err, "checkCanExecuteCommand"))
// m, err := common.BotSession.ChannelMessageSend(cState.ID(), resp)
// go yc.deleteResponse([]*discordgo.Message{m})
return nil, nil
}
if !canExecute {
return nil, nil
}
if err != nil {
return nil, err
}
data = data.WithContext(context.WithValue(data.Context(), CtxKeyCmdSettings, settings))
// Lock the command for execution
err = common.BlockingLockRedisKey(RKeyCommandLock(data.Msg.Author.ID, yc.Name), CommandExecTimeout*2, int((CommandExecTimeout + time.Second).Seconds()))
if err != nil {
return nil, errors.WithMessage(err, "Failed locking command")
}
defer common.UnlockRedisKey(RKeyCommandLock(data.Msg.Author.ID, yc.Name))
innerResp, err := inner(data)
// Send the response
yc.PostCommandExecuted(settings, data, innerResp, err)
return nil, nil
}
}
func FilterResp(in interface{}, guildID int64) interface{} {
switch t := in.(type) {
case string:
return FilterBadInvites(t, guildID, "[removed-invite]")
case error:
return FilterBadInvites(t.Error(), guildID, "[removed-invite]")
}
return in
}
func AddRootCommands(cmds ...*YAGCommand) {
for _, v := range cmds {
CommandSystem.Root.AddCommand(v, v.GetTrigger())
}
}
func handleMsgCreate(evt *eventsystem.EventData) {
abort := false
for _, filterFunc := range MessageFilterFuncs {
if !filterFunc(evt.MessageCreate().Message) {
abort = true
}
}
if abort {
return
}
CommandSystem.HandleMessageCreate(common.BotSession, evt.MessageCreate())
}
func (p *Plugin) Prefix(data *dcmd.Data) string {
prefix, err := GetCommandPrefix(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 := common.BotSession.UserChannelCreate(data.Msg.Author.ID)
if err != nil {
return "Something went wrong, maybe you have DM's disabled? I don't want to spam this channel so here's a external link to available commands: <https://docs.yagpdb.xyz/commands>", err
}
for _, v := range resp {
_, err := common.BotSession.ChannelMessageSendEmbed(channel.ID, v)
if err != nil {
return "Something went wrong, maybe you have DM's disabled? I don't want to spam this channel so here's a external link to available commands: <https://docs.yagpdb.xyz/commands>", err
}
}
return "You've got mail!", nil
}
func HandleGuildCreate(evt *eventsystem.EventData) {
g := evt.GuildCreate()
var prefixExists bool
err := common.RedisPool.Do(radix.Cmd(&prefixExists, "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 = "("
}
common.RedisPool.Do(radix.Cmd(nil, "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 + ")")
}
}
var cmdPrefix = &YAGCommand{
Name: "Prefix",
Description: "Shows command prefix of the current server, or the specified server",
CmdCategory: CategoryTool,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "Server ID", Type: dcmd.Int, Default: 0},
},
RunFunc: func(data *dcmd.Data) (interface{}, error) {
targetGuildID := data.Args[0].Int64()
if targetGuildID == 0 {
targetGuildID = data.GS.ID
}
prefix, err := GetCommandPrefix(targetGuildID)
if err != nil {
return nil, err
}
return fmt.Sprintf("Prefix of `%d`: `%s`", targetGuildID, prefix), nil
},
}
func ContextMS(ctx context.Context) *dstate.MemberState {
v := ctx.Value(CtxKeyMS)
if v == nil {
return nil
}
return v.(*dstate.MemberState)
}