2020-01-19 06:06:00 +00:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
type NameFlag uint64
|
|
|
|
|
|
|
|
const FlagSeparator = 'ー'
|
|
|
|
|
|
|
|
const (
|
|
|
|
None NameFlag = 1 << iota
|
|
|
|
|
2020-01-24 03:17:03 +00:00
|
|
|
// !!!
|
|
|
|
//
|
|
|
|
// These flags are applied to all events, if possible. The defined behavior
|
|
|
|
// is to search for "ChannelID" fields or "ID" fields in structs with
|
|
|
|
// "Channel" in its name. It doesn't handle individual events, as such, will
|
|
|
|
// not be able to guarantee it will always work.
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-01-24 03:17:03 +00:00
|
|
|
// R - Raw, which tells the library to use the method name as-is (flags will
|
|
|
|
// still be stripped). For example, if a method is called Reset its
|
|
|
|
// command will also be Reset, without being all lower-cased.
|
|
|
|
Raw
|
|
|
|
|
|
|
|
// A - AdminOnly, which tells the library to only run the Subcommand/method
|
|
|
|
// if the user is admin or not. This will automatically add GuildOnly as
|
|
|
|
// well.
|
|
|
|
AdminOnly
|
|
|
|
|
|
|
|
// G - GuildOnly, which tells the library to only run the Subcommand/method
|
|
|
|
// if the user is inside a guild.
|
|
|
|
GuildOnly
|
|
|
|
|
|
|
|
// M - Middleware, which tells the library that the method is a middleware.
|
|
|
|
// The method will be executed anytime a method of the same struct is
|
|
|
|
// matched.
|
|
|
|
//
|
|
|
|
// Using this flag inside the subcommand will drop all methods (this is an
|
|
|
|
// undefined behavior/UB).
|
|
|
|
Middleware
|
2020-01-19 06:06:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ParseFlag(name string) (NameFlag, string) {
|
|
|
|
parts := strings.SplitN(name, string(FlagSeparator), 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return 0, name
|
|
|
|
}
|
|
|
|
|
|
|
|
var f NameFlag
|
|
|
|
|
|
|
|
for _, r := range parts[0] {
|
|
|
|
switch r {
|
|
|
|
case 'R':
|
|
|
|
f |= Raw
|
|
|
|
case 'A':
|
2020-01-23 07:20:24 +00:00
|
|
|
f |= AdminOnly | GuildOnly
|
|
|
|
case 'G':
|
|
|
|
f |= GuildOnly
|
2020-01-24 03:17:03 +00:00
|
|
|
case 'M':
|
|
|
|
f |= Middleware
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f NameFlag) Is(flag NameFlag) bool {
|
|
|
|
return f&flag != 0
|
|
|
|
}
|