-
Notifications
You must be signed in to change notification settings - Fork 1
/
execute_slash.go
147 lines (126 loc) · 4.42 KB
/
execute_slash.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
package bcr
import (
"fmt"
"strings"
"sync"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
)
// InteractionCreate is called when an interaction create event is received.
func (r *Router) InteractionCreate(ic *gateway.InteractionCreateEvent) {
if ic.Data.InteractionType() != discord.CommandInteractionType {
return
}
ctx, err := r.NewSlashContext(ic)
if err != nil {
r.Logger.Error("Couldn't create slash context: %v", err)
return
}
err = r.ExecuteSlash(ctx)
if err != nil {
r.Logger.Error("Couldn't create slash context: %v", err)
}
}
// ExecuteSlash executes slash commands. Only one layer for now, so no subcommands, sorry :(
func (r *Router) ExecuteSlash(ctx *SlashContext) (err error) {
err = r.executeSlash(true, ctx, r.cmds, &r.cmdMu)
if err == errCommandRun {
return nil
}
return
}
func errCommand(err error) error {
if err == nil {
return errCommandRun
}
return err
}
func (r *Router) executeSlash(isTopLevel bool, ctx *SlashContext, cmds map[string]*Command, mu *sync.RWMutex) (err error) {
// first, check subcommands
if len(ctx.CommandOptions) > 0 && isTopLevel {
for _, g := range r.SlashGroups {
if strings.EqualFold(g.Name, ctx.CommandName) {
nctx := &SlashContext{}
*nctx = *ctx
nctx.CommandName = ctx.CommandOptions[0].Name
nctx.CommandOptions = ctx.CommandOptions[0].Options
// convert subcommands slice to a map
m := map[string]*Command{}
for _, cmd := range g.Subcommands {
m[strings.ToLower(cmd.Name)] = cmd
}
var nmu sync.RWMutex // this doesn't matter so we just create a new one
return r.executeSlash(false, nctx, m, &nmu)
}
}
}
// else, we try top-level commands (or skip to this immediately if it isn't the top level)
mu.RLock()
cmd, ok := cmds[ctx.CommandName]
if !ok || cmd.SlashCommand == nil {
mu.RUnlock()
err = ctx.SendEphemeral(fmt.Sprintf("Looks like you found a command (``%v``) that's registered as a slash command, but doesn't work as one :(\nPlease report this to the bot developer as this is a bug!", EscapeBackticks(ctx.CommandName)))
return errCommand(err)
}
mu.RUnlock()
ctx.Command = cmd
if (cmd.GuildOnly || cmd.Permissions != 0) && !ctx.Event.GuildID.IsValid() {
err = ctx.SendEphemeral(":x: This command cannot be run in DMs.")
return errCommand(err)
}
if r.BlacklistFunc != nil && cmd.Blacklistable {
if r.BlacklistFunc(ctx) {
err = ctx.SendEphemeral("This command can't be used here.")
return errCommand(err)
}
}
if cmd.GuildPermissions != 0 {
if ctx.Guild == nil || ctx.Member == nil {
err = ctx.SendEphemeral(":x: This command cannot be used in DMs.")
return errCommand(err)
}
if !ctx.GuildPerms().Has(cmd.GuildPermissions) {
err = ctx.SendEphemeral(fmt.Sprintf(":x: You are not allowed to use this command. You are missing the following permissions:\n> ```%v```", strings.Join(PermStrings(cmd.GuildPermissions), ", ")))
return errCommand(err)
}
}
if cmd.Permissions != 0 {
if ctx.Member != nil && ctx.Guild != nil {
if !discord.CalcOverrides(*ctx.Guild, *ctx.Channel, *ctx.Member, ctx.Guild.Roles).Has(cmd.Permissions) {
err = ctx.SendEphemeral(fmt.Sprintf(":x: You are not allowed to use this command. You are missing the following permissions:\n> ```%v```", strings.Join(PermStrings(cmd.Permissions), ", ")))
return errCommand(err)
}
} else {
err = ctx.SendEphemeral(":x: This command cannot be run in DMs.")
return errCommand(err)
}
}
if cmd.OwnerOnly {
isOwner := false
for _, u := range ctx.Router.BotOwners {
if ctx.Author.ID.String() == u {
isOwner = true
break
}
}
if !isOwner {
err = ctx.SendEphemeral(":x: This command can only be used by a bot owner.")
return errCommand(err)
}
}
if cmd.CustomPermissions != nil {
b, err := cmd.CustomPermissions.Check(ctx)
// if it errored, send that error and return
if err != nil {
err = ctx.SendEphemeral(fmt.Sprintf(":x: An internal error occurred when checking your permissions.\nThe following permission(s) could not be checked:\n> ```%s```", cmd.CustomPermissions.String(ctx)))
return errCommand(err)
}
// else if it returned false, show that error and return
if !b {
err = ctx.SendEphemeral(fmt.Sprintf(":x: You are not allowed to use this command. You are missing the following permission(s):\n> ```%s```", cmd.CustomPermissions.String(ctx)))
return errCommand(err)
}
}
err = cmd.SlashCommand(ctx)
return errCommand(err)
}