mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-17 20:32:48 +00:00
diamondburned
aadcbd0767
* Middleware nameflag * Completed M-Middleware feature * Changed Namer/Descriptor API to CanSetup API
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package bot
|
|
|
|
import "strings"
|
|
|
|
type NameFlag uint64
|
|
|
|
const FlagSeparator = 'ー'
|
|
|
|
const (
|
|
None NameFlag = 1 << iota
|
|
|
|
// !!!
|
|
//
|
|
// 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.
|
|
|
|
// 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
|
|
)
|
|
|
|
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':
|
|
f |= AdminOnly | GuildOnly
|
|
case 'G':
|
|
f |= GuildOnly
|
|
case 'M':
|
|
f |= Middleware
|
|
}
|
|
}
|
|
|
|
return f, parts[1]
|
|
}
|
|
|
|
func (f NameFlag) Is(flag NameFlag) bool {
|
|
return f&flag != 0
|
|
}
|