1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-01-21 03:57:26 +00:00

Bot: changed to a better command parser

This commit is contained in:
diamondburned (Forefront) 2020-01-25 21:58:06 -08:00
parent 9d3528190f
commit 0751f45bdb
5 changed files with 12 additions and 124 deletions

View file

@ -5,6 +5,8 @@ import (
"reflect"
"strconv"
"strings"
"github.com/mattn/go-shellwords"
)
type argumentValueFn func(string) (reflect.Value, error)
@ -97,6 +99,10 @@ type Argument struct {
custom *reflect.Method
}
var ParseArgs = func(args string) ([]string, error) {
return shellwords.Parse(args)
}
// nilV, only used to return an error
var nilV = reflect.Value{}

View file

@ -1,114 +0,0 @@
package bot
/*
// UserPermissions but userID is after channelID.
func (ctx *Context) UserPermissions(channelID, userID string,
) (apermissions int, err error) {
// Try to just get permissions from state.
apermissions, err = ctx.Session.State.UserChannelPermissions(
userID, channelID)
if err == nil {
return
}
// Otherwise try get as much data from state as possible, falling back to the network.
channel, err := ctx.Channel(channelID)
if err != nil {
return
}
guild, err := ctx.Guild(channel.GuildID)
if err != nil {
return
}
if userID == guild.OwnerID {
apermissions = discordgo.PermissionAll
return
}
member, err := ctx.Member(guild.ID, userID)
if err != nil {
return
}
return MemberPermissions(guild, channel, member), nil
}
// Why this isn't exported, I have no idea.
func MemberPermissions(guild *discordgo.Guild, channel *discordgo.Channel,
member *discordgo.Member) (apermissions int) {
userID := member.User.ID
if userID == guild.OwnerID {
apermissions = discordgo.PermissionAll
return
}
for _, role := range guild.Roles {
if role.ID == guild.ID {
apermissions |= role.Permissions
break
}
}
for _, role := range guild.Roles {
for _, roleID := range member.Roles {
if role.ID == roleID {
apermissions |= role.Permissions
break
}
}
}
if apermissions&discordgo.PermissionAdministrator ==
discordgo.PermissionAdministrator {
apermissions |= discordgo.PermissionAll
}
// Apply @everyone overrides from the channel.
for _, overwrite := range channel.PermissionOverwrites {
if guild.ID == overwrite.ID {
apermissions &= ^overwrite.Deny
apermissions |= overwrite.Allow
break
}
}
denies := 0
allows := 0
// Member overwrites can override role overrides, so do two passes
for _, overwrite := range channel.PermissionOverwrites {
for _, roleID := range member.Roles {
if overwrite.Type == "role" && roleID == overwrite.ID {
denies |= overwrite.Deny
allows |= overwrite.Allow
break
}
}
}
apermissions &= ^denies
apermissions |= allows
for _, overwrite := range channel.PermissionOverwrites {
if overwrite.Type == "member" && overwrite.ID == userID {
apermissions &= ^overwrite.Deny
apermissions |= overwrite.Allow
break
}
}
if apermissions&discordgo.PermissionAdministrator ==
discordgo.PermissionAdministrator {
apermissions |= discordgo.PermissionAllChannel
}
return apermissions
}
*/

View file

@ -1,7 +1,6 @@
package bot
import (
"encoding/csv"
"reflect"
"strings"
@ -376,15 +375,6 @@ func callWith(caller reflect.Value, ev interface{}, values ...reflect.Value) err
)))
}
var ParseArgs = func(args string) ([]string, error) {
// TODO: make modular
// TODO: actual tokenizer+parser
r := csv.NewReader(strings.NewReader(args))
r.Comma = ' '
return r.Read()
}
func errorReturns(returns []reflect.Value) error {
// assume first is always error, since we checked for this in parseCommands
v := returns[0].Interface()

2
go.mod
View file

@ -5,8 +5,10 @@ go 1.13
require (
github.com/davecgh/go-spew v1.1.1
github.com/gorilla/schema v1.1.0
github.com/mattn/go-shellwords v1.0.9
github.com/pkg/errors v0.8.1
github.com/sasha-s/go-csync v0.0.0-20160729053059-3bc6c8bdb3fa
github.com/zyedidia/micro v1.4.1
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
nhooyr.io/websocket v1.7.4

4
go.sum
View file

@ -20,6 +20,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-shellwords v1.0.9 h1:eaB5JspOwiKKcHdqcjbfe5lA9cNn/4NRRtddXJCimqk=
github.com/mattn/go-shellwords v1.0.9/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -29,6 +31,8 @@ github.com/sasha-s/go-csync v0.0.0-20160729053059-3bc6c8bdb3fa/go.mod h1:KKzWrLi
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/zyedidia/micro v1.4.1 h1:OuszISyaEPK/8xxkklkh7dp2ragvKDEnr4RyHfJcQdo=
github.com/zyedidia/micro v1.4.1/go.mod h1:/wcvhlXPvvvb6v176yUQE4gNzr+Erwz4pWfx7PU/cuE=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=