github.com/starshine-sys/bcr@v0.21.0/ctx_flags.go (about)

     1  package bcr
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/diamondburned/arikawa/v3/discord"
     7  )
     8  
     9  // Flags defines methods to retrieve flags from a command input.
    10  type Flags interface {
    11  	GetStringFlag(name string) string
    12  	GetBoolFlag(name string) bool
    13  	GetIntFlag(name string) int64
    14  	GetFloatFlag(name string) float64
    15  
    16  	GetUserFlag(name string) (*discord.User, error)
    17  	GetMemberFlag(name string) (*discord.Member, error)
    18  	GetRoleFlag(name string) (*discord.Role, error)
    19  	GetChannelFlag(name string) (*discord.Channel, error)
    20  }
    21  
    22  // GetStringFlag gets the named flag as a string, or falls back to an empty string.
    23  func (ctx *Context) GetStringFlag(name string) string {
    24  	v, ok := ctx.FlagMap[strings.ToLower(name)]
    25  	if !ok {
    26  		return ""
    27  	}
    28  
    29  	s, ok := v.(*string)
    30  	if !ok || s == nil {
    31  		return ""
    32  	}
    33  	return *s
    34  }
    35  
    36  // GetBoolFlag gets the named flag as a bool, or falls back to false.
    37  func (ctx *Context) GetBoolFlag(name string) bool {
    38  	v, ok := ctx.FlagMap[strings.ToLower(name)]
    39  	if !ok {
    40  		return false
    41  	}
    42  
    43  	b, ok := v.(*bool)
    44  	if !ok || b == nil {
    45  		return false
    46  	}
    47  	return *b
    48  }
    49  
    50  // GetIntFlag gets the named flag as an int64, or falls back to 0.
    51  func (ctx *Context) GetIntFlag(name string) int64 {
    52  	v, ok := ctx.FlagMap[strings.ToLower(name)]
    53  	if !ok {
    54  		return 0
    55  	}
    56  
    57  	i, ok := v.(*int64)
    58  	if !ok || i == nil {
    59  		return 0
    60  	}
    61  	return *i
    62  }
    63  
    64  // GetFloatFlag gets the named flag as a float64, or falls back to 0.
    65  func (ctx *Context) GetFloatFlag(name string) float64 {
    66  	v, ok := ctx.FlagMap[strings.ToLower(name)]
    67  	if !ok {
    68  		return 0
    69  	}
    70  
    71  	f, ok := v.(*float64)
    72  	if !ok || f == nil {
    73  		return 0
    74  	}
    75  	return *f
    76  }
    77  
    78  // GetUserFlag gets the named flag as a user.
    79  func (ctx *Context) GetUserFlag(name string) (*discord.User, error) {
    80  	v, ok := ctx.FlagMap[strings.ToLower(name)]
    81  	if !ok {
    82  		return nil, ErrUserNotFound
    83  	}
    84  
    85  	s, ok := v.(*string)
    86  	if !ok || s == nil {
    87  		return nil, ErrUserNotFound
    88  	}
    89  
    90  	return ctx.ParseUser(*s)
    91  }
    92  
    93  // GetMemberFlag gets the named flag as a member.
    94  func (ctx *Context) GetMemberFlag(name string) (*discord.Member, error) {
    95  	v, ok := ctx.FlagMap[strings.ToLower(name)]
    96  	if !ok {
    97  		return nil, ErrMemberNotFound
    98  	}
    99  
   100  	s, ok := v.(*string)
   101  	if !ok || s == nil {
   102  		return nil, ErrMemberNotFound
   103  	}
   104  
   105  	return ctx.ParseMember(*s)
   106  }
   107  
   108  // GetRoleFlag gets the named flag as a role.
   109  func (ctx *Context) GetRoleFlag(name string) (*discord.Role, error) {
   110  	v, ok := ctx.FlagMap[strings.ToLower(name)]
   111  	if !ok {
   112  		return nil, ErrRoleNotFound
   113  	}
   114  
   115  	s, ok := v.(*string)
   116  	if !ok || s == nil {
   117  		return nil, ErrRoleNotFound
   118  	}
   119  
   120  	return ctx.ParseRole(*s)
   121  }
   122  
   123  // GetChannelFlag gets the named flag as a channel.
   124  func (ctx *Context) GetChannelFlag(name string) (*discord.Channel, error) {
   125  	v, ok := ctx.FlagMap[strings.ToLower(name)]
   126  	if !ok {
   127  		return nil, ErrChannelNotFound
   128  	}
   129  
   130  	s, ok := v.(*string)
   131  	if !ok || s == nil {
   132  		return nil, ErrChannelNotFound
   133  	}
   134  
   135  	return ctx.ParseChannel(*s)
   136  }