-
Notifications
You must be signed in to change notification settings - Fork 7
/
options.go
131 lines (110 loc) · 3.45 KB
/
options.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
package ken
import (
"strings"
"github.com/bwmarrin/discordgo"
)
// CommandOptions provides additional functionailities to
// an array of ApplicationCommandInteractionDataOptions.
type CommandOptions []*discordgo.ApplicationCommandInteractionDataOption
// Get safely returns an options from command options
// by index.
func (co CommandOptions) Get(i int) *CommandOption {
if i < 0 {
i = 0
}
if i >= len(co) {
i = len(co) - 1
}
return &CommandOption{co[i]}
}
// Options returns wrapped underlying options
// of a sub command by ID.
func (co CommandOptions) Options(i int) CommandOptions {
return co.Get(i).Options
}
// GetByNameOptional returns an option by name. If the option with the
// name does not exist, the returned value for ok is false.
//
// This should be used for non-required options.
func (co CommandOptions) GetByNameOptional(name string) (opt *CommandOption, ok bool) {
for _, c := range co {
if c.Name == name {
ok = true
opt = &CommandOption{c}
break
}
}
return
}
// GetByName returns an option by name.
//
// This should only be used on required options.
func (co CommandOptions) GetByName(name string) (opt *CommandOption) {
opt, _ = co.GetByNameOptional(name)
return
}
// CommandOption wraps a ApplicationCommandInteractionDataOption
// to provide additional functionalities and method overrides.
type CommandOption struct {
*discordgo.ApplicationCommandInteractionDataOption
}
// ChannelValue is a utility function for casting option value to channel object.
//
// The object is taken from the specified state instance.
func (o *CommandOption) ChannelValue(ctx Context) *discordgo.Channel {
if o.Type != discordgo.ApplicationCommandOptionChannel {
panic("ChannelValue called on data option of type " + o.Type.String())
}
chanID := o.Value.(string)
if ctx == nil {
return &discordgo.Channel{ID: chanID}
}
ch, err := ctx.GetKen().opt.State.Channel(ctx.GetSession(), chanID)
if err != nil {
return &discordgo.Channel{ID: chanID}
}
return ch
}
// RoleValue is a utility function for casting option value to role object.
//
// The object is taken from the specified state instance.
func (o *CommandOption) RoleValue(ctx Context) *discordgo.Role {
if o.Type != discordgo.ApplicationCommandOptionRole {
panic("RoleValue called on data option of type " + o.Type.String())
}
roleID := o.Value.(string)
if ctx == nil {
return &discordgo.Role{ID: roleID}
}
role, err := ctx.GetKen().opt.State.Role(ctx.GetSession(), ctx.GetEvent().GuildID, roleID)
if err != nil {
return &discordgo.Role{ID: roleID}
}
return role
}
// UserValue is a utility function for casting option value to user object.
//
// The object is taken from the specified state instance.
func (o *CommandOption) UserValue(ctx Context) *discordgo.User {
if o.Type != discordgo.ApplicationCommandOptionUser {
panic("UserValue called on data option of type " + o.Type.String())
}
userID := o.Value.(string)
if ctx == nil {
return &discordgo.User{ID: userID}
}
user, err := ctx.GetKen().opt.State.User(ctx.GetSession(), userID)
if err != nil {
return &discordgo.User{ID: userID}
}
return user
}
// StringValue is a utility function for casting option value to string.
//
// Because you can not pass multiline string entries to slash commands,
// this converts `\n` in a message to an actual line break.
func (o *CommandOption) StringValue() (v string) {
v = o.ApplicationCommandInteractionDataOption.StringValue()
v = strings.ReplaceAll(v, "\\n", "\n")
return
}