/
subs.go
97 lines (83 loc) · 2.09 KB
/
subs.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
package commands
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/zekrotja/ken"
)
type SubsCommand struct{}
var (
_ ken.SlashCommand = (*SubsCommand)(nil)
_ ken.DmCapable = (*SubsCommand)(nil)
)
func (c *SubsCommand) Name() string {
return "subs"
}
func (c *SubsCommand) Description() string {
return "An example command with sub commands."
}
func (c *SubsCommand) Version() string {
return "1.0.0"
}
func (c *SubsCommand) Type() discordgo.ApplicationCommandType {
return discordgo.ChatApplicationCommand
}
func (c *SubsCommand) Options() []*discordgo.ApplicationCommandOption {
return []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "one",
Description: "First sub command",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "arg",
Description: "Argument",
Required: true,
},
},
},
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "two",
Description: "Second sub command",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "arg",
Description: "Argument",
Required: false,
},
},
},
}
}
func (c *SubsCommand) IsDmCapable() bool {
return true
}
func (c *SubsCommand) Run(ctx ken.Context) (err error) {
err = ctx.HandleSubCommands(
ken.SubCommandHandler{"one", c.one},
ken.SubCommandHandler{"two", c.two},
)
return
}
func (c *SubsCommand) one(ctx ken.SubCommandContext) (err error) {
if err = ctx.Defer(); err != nil {
return
}
arg := ctx.Options().GetByName("arg").StringValue()
err = ctx.FollowUpEmbed(&discordgo.MessageEmbed{
Description: "one: " + arg,
}).Send().Error
return
}
func (c *SubsCommand) two(ctx ken.SubCommandContext) (err error) {
var arg int
if argV, ok := ctx.Options().GetByNameOptional("arg"); ok {
arg = int(argV.IntValue())
}
err = ctx.RespondEmbed(&discordgo.MessageEmbed{
Description: fmt.Sprintf("two: %d", arg),
})
return
}