-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ken | ||
|
||
import ( | ||
"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) *discordgo.ApplicationCommandInteractionDataOption { | ||
if i < 0 { | ||
i = 0 | ||
} | ||
if i >= len(co) { | ||
i = len(co) - 1 | ||
} | ||
return 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) (ok bool, opt *discordgo.ApplicationCommandInteractionDataOption) { | ||
for _, c := range co { | ||
if c.Name == name { | ||
ok = true | ||
opt = c | ||
break | ||
} | ||
} | ||
return | ||
} | ||
|
||
// GetByName returns an option by name. | ||
// | ||
// This should only be used on required options. | ||
func (co CommandOptions) GetByName(name string) (opt *discordgo.ApplicationCommandInteractionDataOption) { | ||
_, opt = co.GetByNameOptional(name) | ||
return | ||
} |