-
Notifications
You must be signed in to change notification settings - Fork 921
/
tmplexec.go
234 lines (195 loc) · 6.18 KB
/
tmplexec.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"
"github.com/jonas747/yagpdb/bot/paginatedmessages"
"strconv"
"strings"
"emperror.dev/errors"
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/templates"
)
func init() {
templates.RegisterSetupFunc(func(ctx *templates.Context) {
execUser, execBot := TmplExecCmdFuncs(ctx, 5, false)
ctx.ContextFuncs["exec"] = execUser
ctx.ContextFuncs["execAdmin"] = execBot
ctx.ContextFuncs["userArg"] = tmplUserArg(ctx)
})
}
// Returns a user from either id, mention string or if the input is just a user, a user...
func tmplUserArg(tmplCtx *templates.Context) interface{} {
return func(v interface{}) (interface{}, error) {
if tmplCtx.IncreaseCheckCallCounter("commands_user_arg", 5) {
return nil, errors.New("Max calls to userarg (5) reached")
}
if num := templates.ToInt64(v); num != 0 {
// Assume it's an id
member, _ := bot.GetMember(tmplCtx.GS.ID, num)
if member != nil {
return member.DGoUser(), nil
}
return nil, nil
}
if str, ok := v.(string); ok {
// Mention string
if len(str) < 5 {
return nil, nil
}
str = strings.TrimSpace(str)
if strings.HasPrefix(str, "<@") && strings.HasSuffix(str, ">") {
trimmed := str[2 : len(str)-1]
if trimmed[0] == '!' {
trimmed = trimmed[1:]
}
id, _ := strconv.ParseInt(trimmed, 10, 64)
member, _ := bot.GetMember(tmplCtx.GS.ID, id)
if member != nil {
// Found member
return member.DGoUser(), nil
}
}
// No more cases we can handle
return nil, nil
}
// Just return whatever we passed
return v, nil
}
}
type cmdExecFunc func(cmd string, args ...interface{}) (interface{}, error)
// Returns 2 functions to execute commands in user or bot context with limited about of commands executed
func TmplExecCmdFuncs(ctx *templates.Context, maxExec int, dryRun bool) (userCtxCommandExec cmdExecFunc, botCtxCommandExec cmdExecFunc) {
execUser := func(cmd string, args ...interface{}) (interface{}, error) {
mc := &discordgo.MessageCreate{ctx.Msg}
if maxExec < 1 {
return "", errors.New("Max number of commands executed in custom command")
}
maxExec -= 1
return execCmd(ctx, dryRun, mc, cmd, args...)
}
execBot := func(cmd string, args ...interface{}) (interface{}, error) {
botUserCopy := *common.BotUser
botUserCopy.Username = "YAGPDB (cc: " + ctx.Msg.Author.Username + "#" + ctx.Msg.Author.Discriminator + ")"
messageCopy := *ctx.Msg
messageCopy.Author = &botUserCopy
mc := &discordgo.MessageCreate{&messageCopy}
if maxExec < 1 {
return "", errors.New("Max number of commands executed in custom command")
}
maxExec -= 1
return execCmd(ctx, dryRun, mc, cmd, args...)
}
return execUser, execBot
}
func execCmd(tmplCtx *templates.Context, dryRun bool, m *discordgo.MessageCreate, cmd string, args ...interface{}) (interface{}, error) {
ctxMember, err := bot.GetMember(tmplCtx.GS.ID, m.Author.ID)
if err != nil {
return "error retrieving member", err
}
fakeMsg := *m.Message
fakeMsg.Mentions = make([]*discordgo.User, 0)
cmdLine := cmd + " "
for _, arg := range args {
if arg == nil {
return "", errors.New("Nil arg passed")
}
switch t := arg.(type) {
case string:
if strings.HasPrefix(t, "-") {
// Don't put quotes around switches
cmdLine += t
} else if strings.HasPrefix(t, "\\-") {
// Escaped -
cmdLine += "\"" + t[1:] + "\""
} else {
cmdLine += "\"" + t + "\""
}
case int:
cmdLine += strconv.FormatInt(int64(t), 10)
case int32:
cmdLine += strconv.FormatInt(int64(t), 10)
case int64:
cmdLine += strconv.FormatInt(t, 10)
case uint:
cmdLine += strconv.FormatUint(uint64(t), 10)
case uint8:
cmdLine += strconv.FormatUint(uint64(t), 10)
case uint16:
cmdLine += strconv.FormatUint(uint64(t), 10)
case uint32:
cmdLine += strconv.FormatUint(uint64(t), 10)
case uint64:
cmdLine += strconv.FormatUint(t, 10)
case float32:
cmdLine += strconv.FormatFloat(float64(t), 'E', -1, 32)
case float64:
cmdLine += strconv.FormatFloat(t, 'E', -1, 64)
case *discordgo.User:
cmdLine += "<@" + strconv.FormatInt(t.ID, 10) + ">"
fakeMsg.Mentions = append(fakeMsg.Mentions, t)
case []string:
for i, str := range t {
if i != 0 {
cmdLine += " "
}
cmdLine += str
}
default:
return "", errors.New("Unknown type in exec, only strings, numbers, users and string slices are supported")
}
cmdLine += " "
}
logger.Info("Custom template is executing a command:", cmdLine)
fakeMsg.Content = cmdLine
data, err := CommandSystem.FillData(common.BotSession, &fakeMsg)
if err != nil {
return "", errors.WithMessage(err, "tmplExecCmd")
}
data.MsgStrippedPrefix = fakeMsg.Content
ctx := context.WithValue(data.Context(), CtxKeyMS, ctxMember)
data = data.WithContext(context.WithValue(ctx, paginatedmessages.CtxKeyNoPagination, true))
foundCmd, foundContainer, rest := CommandSystem.Root.AbsFindCommandWithRest(cmdLine)
if foundCmd == nil {
return "Unknown command", nil
}
data.MsgStrippedPrefix = rest
data.Cmd = foundCmd
data.ContainerChain = []*dcmd.Container{CommandSystem.Root}
if foundContainer != CommandSystem.Root {
data.ContainerChain = append(data.ContainerChain, foundContainer)
}
cast := foundCmd.Command.(*YAGCommand)
err = dcmd.ParseCmdArgs(data)
if err != nil {
return "", errors.WithMessage(err, "exec/execadmin, parseArgs")
}
runFunc := cast.RunFunc
for i := range foundCmd.Trigger.Middlewares {
runFunc = foundCmd.Trigger.Middlewares[len(foundCmd.Trigger.Middlewares)-1-i](runFunc)
}
for i := range data.ContainerChain {
if i == len(data.ContainerChain)-1 {
// skip middlewares in original container to bypass cooldowns and shit
continue
}
runFunc = data.ContainerChain[len(data.ContainerChain)-1-i].BuildMiddlewareChain(runFunc, foundCmd)
}
// foundCmd.Trigger.
resp, err := runFunc(data)
if err != nil {
return "", errors.WithMessage(err, "exec/execadmin, run")
}
switch v := resp.(type) {
case error:
return "Error: " + v.Error(), nil
case string:
return v, nil
case *discordgo.MessageEmbed:
return v, nil
case []*discordgo.MessageEmbed:
return v, nil
}
return "", nil
}