github.com/starshine-sys/bcr@v0.21.0/ctx_args_slash.go (about) 1 package bcr 2 3 import ( 4 "strings" 5 6 "emperror.dev/errors" 7 "github.com/diamondburned/arikawa/v3/discord" 8 "github.com/diamondburned/arikawa/v3/gateway" 9 ) 10 11 // SlashCommandOption is a single slash command option with a name and value. 12 type SlashCommandOption struct { 13 ctx *SlashContext 14 gateway.InteractionOption 15 } 16 17 // Option returns an option by name, or an empty option if it's not found. 18 func (ctx *SlashContext) Option(name string) (option SlashCommandOption) { 19 for _, o := range ctx.CommandOptions { 20 if strings.EqualFold(o.Name, name) { 21 return SlashCommandOption{ 22 ctx: ctx, 23 InteractionOption: o, 24 } 25 } 26 } 27 return SlashCommandOption{ctx: ctx} 28 } 29 30 // SlashCommandOptions is a slice of slash command options. 31 type SlashCommandOptions struct { 32 ctx *SlashContext 33 s []gateway.InteractionOption 34 } 35 36 // NewSlashCommandOptions returns a new SlashCommandOptions. 37 func NewSlashCommandOptions(ctx *SlashContext, s []gateway.InteractionOption) SlashCommandOptions { 38 return SlashCommandOptions{ 39 ctx: ctx, s: s, 40 } 41 } 42 43 // Get returns an option by name, or an empty option if it's not found. 44 func (options SlashCommandOptions) Get(name string) SlashCommandOption { 45 for _, o := range options.s { 46 if strings.EqualFold(o.Name, name) { 47 return SlashCommandOption{ 48 ctx: options.ctx, 49 InteractionOption: o, 50 } 51 } 52 } 53 return SlashCommandOption{ctx: options.ctx} 54 } 55 56 // Option returns an option by name, or an empty option if it's not found. 57 func (o SlashCommandOption) Option(name string) SlashCommandOption { 58 for _, option := range o.Options { 59 if strings.EqualFold(option.Name, name) { 60 return SlashCommandOption{ 61 ctx: o.ctx, 62 InteractionOption: option, 63 } 64 } 65 } 66 return SlashCommandOption{} 67 } 68 69 // Int returns the option as an integer, or 0 if it can't be converted. 70 func (o SlashCommandOption) Int() int64 { 71 i, err := o.InteractionOption.Int() 72 if err != nil { 73 return 0 74 } 75 return i 76 } 77 78 // Float returns the option as a float, or 0 if it can't be converted. 79 func (o SlashCommandOption) Float() float64 { 80 i, err := o.InteractionOption.Float() 81 if err != nil { 82 return 0 83 } 84 return i 85 } 86 87 // Bool returns the option as a bool, or false if it can't be converted. 88 func (o SlashCommandOption) Bool() bool { 89 b, err := o.InteractionOption.Bool() 90 if err != nil { 91 return false 92 } 93 return b 94 } 95 96 // User returns the option as a user. 97 func (o SlashCommandOption) User() (*discord.User, error) { 98 id, err := o.Snowflake() 99 if err != nil { 100 return nil, err 101 } 102 103 return o.ctx.State.User(discord.UserID(id)) 104 } 105 106 // Member returns the option as a member. 107 func (o SlashCommandOption) Member() (*discord.Member, error) { 108 if o.ctx.Guild == nil { 109 return nil, errors.Sentinel("not in a guild") 110 } 111 112 id, err := o.Snowflake() 113 if err != nil { 114 return nil, err 115 } 116 117 return o.ctx.State.Member(o.ctx.Guild.ID, discord.UserID(id)) 118 } 119 120 // Role returns the option as a role. 121 func (o SlashCommandOption) Role() (*discord.Role, error) { 122 if o.ctx.Guild == nil { 123 return nil, errors.Sentinel("not in a guild") 124 } 125 126 id, err := o.Snowflake() 127 if err != nil { 128 return nil, err 129 } 130 131 return o.ctx.State.Role(o.ctx.Guild.ID, discord.RoleID(id)) 132 } 133 134 // Channel returns the option as a channel. 135 func (o SlashCommandOption) Channel() (*discord.Channel, error) { 136 id, err := o.Snowflake() 137 if err != nil { 138 return nil, err 139 } 140 141 return o.ctx.State.Channel(discord.ChannelID(id)) 142 } 143 144 // GetStringFlag gets the named flag as a string, or falls back to an empty string. 145 func (ctx *SlashContext) GetStringFlag(name string) string { 146 return ctx.Option(name).String() 147 } 148 149 // GetBoolFlag gets the named flag as a bool, or falls back to false. 150 func (ctx *SlashContext) GetBoolFlag(name string) bool { 151 return ctx.Option(name).Bool() 152 } 153 154 // GetIntFlag gets the named flag as an int64, or falls back to 0. 155 func (ctx *SlashContext) GetIntFlag(name string) int64 { 156 return ctx.Option(name).Int() 157 } 158 159 // GetFloatFlag gets the named flag as a float64, or falls back to 0. 160 func (ctx *SlashContext) GetFloatFlag(name string) float64 { 161 return ctx.Option(name).Float() 162 } 163 164 // GetUserFlag gets the named flag as a user. 165 func (ctx *SlashContext) GetUserFlag(name string) (*discord.User, error) { 166 return ctx.Option(name).User() 167 } 168 169 // GetMemberFlag gets the named flag as a member. 170 func (ctx *SlashContext) GetMemberFlag(name string) (*discord.Member, error) { 171 return ctx.Option(name).Member() 172 } 173 174 // GetRoleFlag gets the named flag as a role. 175 func (ctx *SlashContext) GetRoleFlag(name string) (*discord.Role, error) { 176 return ctx.Option(name).Role() 177 } 178 179 // GetChannelFlag gets the named flag as a channel. 180 func (ctx *SlashContext) GetChannelFlag(name string) (*discord.Channel, error) { 181 return ctx.Option(name).Channel() 182 }