-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds.go
117 lines (105 loc) · 3.74 KB
/
cmds.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
package discord
import (
"github.com/bwmarrin/discordgo"
"github.com/Clinet/clinet_cmds"
"github.com/Clinet/clinet_services"
)
func cmdHandler(cmd *cmds.Cmd, interaction *discordgo.Interaction, eventOpts []*discordgo.ApplicationCommandInteractionDataOption, subCmd bool) (string, []*cmds.CmdResp) {
cmdAlias := interaction.ApplicationCommandData().Name
if !subCmd && len(eventOpts) == 1 && eventOpts[0].Type == discordgo.ApplicationCommandOptionSubCommand {
Log.Trace("Checking subcommands for " + cmd.Name + " using subcommand " + eventOpts[0].Name)
for i := 0; i < len(cmd.Subcommands); i++ {
Log.Trace("- " + cmd.Subcommands[i].Name + " == " + eventOpts[0].Name)
if cmd.Subcommands[i].Name == eventOpts[0].Name {
Log.Trace("> Testing subcommand " + cmd.Subcommands[i].Name)
return cmdHandler(cmd.Subcommands[i], interaction, eventOpts[0].Options, true)
}
}
Log.Error("Command " + cmd.Name + " has no subcommand " + eventOpts[0].Name)
return cmdAlias, nil
}
user := &services.User{
ServerID: interaction.GuildID,
UserID: interaction.Member.User.ID,
}
channel := &services.Channel{
ServerID: interaction.GuildID,
ChannelID: interaction.ChannelID,
}
server := &services.Server{
ServerID: interaction.GuildID,
}
message := &services.Message{
MessageID: interaction.ID,
}
cmdCtx := cmds.NewCmdCtx().
SetAlias(cmdAlias).
SetUser(user).
SetChannel(channel).
SetServer(server).
SetMessage(message).
SetService(Discord)
cmdArgs := discordCmdArgs(cmd, cmdCtx, eventOpts)
cmdCtx.AddArgs(cmdArgs...)
if resolved := interaction.ApplicationCommandData().Resolved; resolved != nil {
for _, attachment := range resolved.Attachments {
attach := &services.MessageAttachment{
URL: attachment.URL,
Filename: attachment.Filename,
ContentType: attachment.ContentType,
Width: attachment.Width,
Height: attachment.Height,
Size: attachment.Size,
}
cmdCtx.AddAttachments(attach)
}
}
cmdBuilder := &cmds.CmdBuilderCommand{Command: cmd, Context: cmdCtx} //Build up a command builder
cmdRuntime := cmds.CmdBatch(cmdBuilder) //Prepare a command runtime with just this command (but can be supplied additional cmdBuilders)
cmdResps := cmdRuntime.Run() //Run the commands and return their responses
if len(cmdResps) == 0 {
Log.Warn("No responses for cmd " + cmdAlias)
}
return cmdAlias, cmdResps
}
func discordCmdArgs(cmd *cmds.Cmd, cmdCtx *cmds.CmdCtx, eventArgs []*discordgo.ApplicationCommandInteractionDataOption) []*cmds.CmdArg {
cmdArgs := cmd.Args
finalArgs := make([]*cmds.CmdArg, 0)
for i := 0; i < len(cmdArgs); i++ {
foundArg := false
for j := 0; j < len(eventArgs); j++ {
if cmdArgs[i].Name == eventArgs[j].Name {
cmdArg := *cmdArgs[i]
switch eventArgs[j].Type {
case discordgo.ApplicationCommandOptionBoolean:
Log.Trace("Filled in bool cmdArg: ", cmdArg)
cmdArg.Value = eventArgs[j].BoolValue()
case discordgo.ApplicationCommandOptionString:
Log.Trace("Filled in string cmdArg: ", cmdArg)
cmdArg.Value = eventArgs[j].StringValue()
case discordgo.ApplicationCommandOptionInteger:
Log.Trace("Filled in integer cmdArg: ", cmdArg)
cmdArg.Value = eventArgs[j].IntValue()
case discordgo.ApplicationCommandOptionUser:
Log.Trace("Filled in user cmdArg: ", cmdArg)
user := eventArgs[j].UserValue(Discord.Session)
cmdArg.Value = &services.User{
ServerID: cmdCtx.Server.ServerID,
UserID: user.ID,
}
default:
Log.Trace("Filled in unknown cmdArg: ", cmdArg)
cmdArg.Value = eventArgs[j].Value
}
finalArgs = append(finalArgs, &cmdArg)
foundArg = true
break
}
}
if !foundArg {
Log.Trace("Failed to find cmdArg: ", cmdArgs[i])
finalArgs = append(finalArgs, cmdArgs[i])
}
}
return finalArgs
}