mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-01-20 19:47:12 +00:00
Added GuildOnly into bot handler
This commit is contained in:
parent
f6f4b14f33
commit
09d8c5bc43
|
@ -9,44 +9,44 @@ import (
|
|||
"github.com/diamondburned/arikawa/gateway"
|
||||
)
|
||||
|
||||
func (ctx *Context) filter(
|
||||
check func(sub *Subcommand, cmd *CommandContext) bool) []reflect.Value {
|
||||
|
||||
var callers []reflect.Value
|
||||
|
||||
for _, cmd := range ctx.Commands {
|
||||
if check(nil, cmd) {
|
||||
callers = append(callers, cmd.value)
|
||||
}
|
||||
}
|
||||
|
||||
for _, sub := range ctx.Subcommands {
|
||||
for _, cmd := range sub.Commands {
|
||||
if check(sub, cmd) {
|
||||
callers = append(callers, cmd.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return callers
|
||||
}
|
||||
|
||||
func (ctx *Context) callCmd(ev interface{}) error {
|
||||
evT := reflect.TypeOf(ev)
|
||||
|
||||
if evT != typeMessageCreate {
|
||||
var callers []reflect.Value
|
||||
var isAdmin *bool // i want to die
|
||||
var isGuild *bool
|
||||
|
||||
for _, cmd := range ctx.Commands {
|
||||
if cmd.event == evT {
|
||||
if cmd.Flag.Is(AdminOnly) &&
|
||||
!ctx.eventIsAdmin(ev, &isAdmin) {
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
callers = append(callers, cmd.value)
|
||||
}
|
||||
}
|
||||
|
||||
for _, sub := range ctx.Subcommands {
|
||||
if sub.Flag.Is(AdminOnly) &&
|
||||
!ctx.eventIsAdmin(ev, &isAdmin) {
|
||||
|
||||
continue
|
||||
callers := ctx.filter(func(sub *Subcommand, cmd *CommandContext) bool {
|
||||
if sub != nil {
|
||||
cmd.Flag |= sub.Flag
|
||||
}
|
||||
|
||||
for _, cmd := range sub.Commands {
|
||||
if cmd.event == evT {
|
||||
if cmd.Flag.Is(AdminOnly) &&
|
||||
!ctx.eventIsAdmin(ev, &isAdmin) {
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
callers = append(callers, cmd.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
return true &&
|
||||
!(cmd.Flag.Is(AdminOnly) && !ctx.eventIsAdmin(ev, &isAdmin)) &&
|
||||
!(cmd.Flag.Is(GuildOnly) && !ctx.eventIsGuild(ev, &isGuild))
|
||||
})
|
||||
|
||||
for _, c := range callers {
|
||||
if err := callWith(c, ev); err != nil {
|
||||
|
@ -98,6 +98,7 @@ func (ctx *Context) callCmd(ev interface{}) error {
|
|||
// Can't find command, look for subcommands of len(args) has a 2nd
|
||||
// entry.
|
||||
if cmd == nil && len(args) > 1 {
|
||||
SubcommandLoop:
|
||||
for _, s := range ctx.Subcommands {
|
||||
if s.name != args[0] {
|
||||
continue
|
||||
|
@ -107,7 +108,11 @@ func (ctx *Context) callCmd(ev interface{}) error {
|
|||
if c.name == args[1] {
|
||||
cmd = c
|
||||
start = 2
|
||||
break
|
||||
|
||||
// OR the flags
|
||||
c.Flag |= s.Flag
|
||||
|
||||
break SubcommandLoop
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,6 +135,17 @@ func (ctx *Context) callCmd(ev interface{}) error {
|
|||
}
|
||||
}
|
||||
|
||||
// Check for IsAdmin and IsGuild
|
||||
if cmd.Flag.Is(GuildOnly) && !mc.GuildID.Valid() {
|
||||
return nil
|
||||
}
|
||||
if cmd.Flag.Is(AdminOnly) {
|
||||
p, err := ctx.State.Permissions(mc.ChannelID, mc.Author.ID)
|
||||
if err != nil || !p.Has(discord.PermissionAdministrator) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Start converting
|
||||
var argv []reflect.Value
|
||||
|
||||
|
@ -229,6 +245,26 @@ func (ctx *Context) eventIsAdmin(ev interface{}, is **bool) bool {
|
|||
return res
|
||||
}
|
||||
|
||||
func (ctx *Context) eventIsGuild(ev interface{}, is **bool) bool {
|
||||
if *is != nil {
|
||||
return **is
|
||||
}
|
||||
|
||||
var channelID = reflectChannelID(ev)
|
||||
if !channelID.Valid() {
|
||||
return false
|
||||
}
|
||||
|
||||
c, err := ctx.State.Channel(channelID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
res := c.GuildID.Valid()
|
||||
*is = &res
|
||||
return res
|
||||
}
|
||||
|
||||
func callWith(caller reflect.Value, ev interface{}, values ...reflect.Value) error {
|
||||
return errorReturns(caller.Call(append(
|
||||
[]reflect.Value{reflect.ValueOf(ev)},
|
||||
|
|
|
@ -13,6 +13,7 @@ const (
|
|||
|
||||
Raw // R
|
||||
AdminOnly // A
|
||||
GuildOnly // G
|
||||
)
|
||||
|
||||
func ParseFlag(name string) (NameFlag, string) {
|
||||
|
@ -28,7 +29,9 @@ func ParseFlag(name string) (NameFlag, string) {
|
|||
case 'R':
|
||||
f |= Raw
|
||||
case 'A':
|
||||
f |= AdminOnly
|
||||
f |= AdminOnly | GuildOnly
|
||||
case 'G':
|
||||
f |= GuildOnly
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue