Specify number of splits to make for command arguments
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
4068a4ff06
commit
139b32094e
@ -21,6 +21,7 @@ type (
|
|||||||
Name string
|
Name string
|
||||||
Config Config
|
Config Config
|
||||||
Func func(cmd *Command, args []string) error
|
Func func(cmd *Command, args []string) error
|
||||||
|
NArgs int
|
||||||
Session *discordgo.Session
|
Session *discordgo.Session
|
||||||
Message *discordgo.MessageCreate
|
Message *discordgo.MessageCreate
|
||||||
}
|
}
|
||||||
@ -66,22 +67,25 @@ func NewCommandHandler(config Config) func(s *discordgo.Session, m *discordgo.Me
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdName, args := lib.SplitCommandAndArgs(m.Content, config.Prefix)
|
cmdName, arg := lib.SplitCommandAndArg(m.Content, config.Prefix)
|
||||||
|
|
||||||
cmd, ok := GetCommand(cmdName)
|
cmd, ok := GetCommand(cmdName)
|
||||||
|
|
||||||
|
args := lib.SplitArgs(arg, cmd.NArgs)
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
cmd.Config = config
|
cmd.Config = config
|
||||||
cmd.Name = cmdName
|
cmd.Name = cmdName
|
||||||
cmd.Session = s
|
cmd.Session = s
|
||||||
cmd.Message = m
|
cmd.Message = m
|
||||||
|
|
||||||
log.Debugf("command: %+v, args: %+v", cmd.Name, args)
|
log.Debugf("command: %v, args: %v, nargs: %d", cmd.Name, args, len(args))
|
||||||
cmd.Func(cmd, args)
|
cmd.Func(cmd, args)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Warnf("unknown command: %+v, args: %+v", cmdName, args)
|
log.Warnf("unknown command: %v, args: %v, nargs: %d", cmdName, args, len(args))
|
||||||
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("unknown command: %s", cmdName))
|
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("unknown command: %s", cmdName))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ func init() {
|
|||||||
bot.AddCommand(&bot.Command{
|
bot.AddCommand(&bot.Command{
|
||||||
Name: "deal",
|
Name: "deal",
|
||||||
Func: commands.DealCommand,
|
Func: commands.DealCommand,
|
||||||
|
NArgs: 1,
|
||||||
})
|
})
|
||||||
bot.AddCommand(&bot.Command{
|
bot.AddCommand(&bot.Command{
|
||||||
Name: "ping",
|
Name: "ping",
|
||||||
@ -45,10 +46,12 @@ func init() {
|
|||||||
bot.AddCommand(&bot.Command{
|
bot.AddCommand(&bot.Command{
|
||||||
Name: "roll",
|
Name: "roll",
|
||||||
Func: commands.RollCommand,
|
Func: commands.RollCommand,
|
||||||
|
NArgs: 1,
|
||||||
})
|
})
|
||||||
bot.AddCommand(&bot.Command{
|
bot.AddCommand(&bot.Command{
|
||||||
Name: "time",
|
Name: "time",
|
||||||
Func: commands.TimeCommand,
|
Func: commands.TimeCommand,
|
||||||
|
NArgs: 1,
|
||||||
})
|
})
|
||||||
bot.AddCommand(&bot.Command{
|
bot.AddCommand(&bot.Command{
|
||||||
Name: "version",
|
Name: "version",
|
||||||
@ -57,6 +60,7 @@ func init() {
|
|||||||
bot.AddCommand(&bot.Command{
|
bot.AddCommand(&bot.Command{
|
||||||
Name: "weather",
|
Name: "weather",
|
||||||
Func: commands.WeatherCommand,
|
Func: commands.WeatherCommand,
|
||||||
|
NArgs: 1,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,24 +99,43 @@ func ContainsCommand(s, prefix, cmd string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func SplitCommandAndArg(s, prefix string) (cmd string, args []string) {
|
func SplitCommandAndArg(s, prefix string) (cmd string, arg string) {
|
||||||
s = strings.TrimSpace(s)
|
s = strings.TrimSpace(s)
|
||||||
|
|
||||||
if !strings.HasPrefix(s, prefix) {
|
if !strings.HasPrefix(s, prefix) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
x := strings.Split(s, " ")
|
// remove the command prefix
|
||||||
|
s = s[len(prefix):]
|
||||||
|
|
||||||
|
// multiple assignment trick
|
||||||
|
cmd, arg = func() (string, string) {
|
||||||
|
x := strings.SplitN(s, " ", 2)
|
||||||
if len(x) > 1 {
|
if len(x) > 1 {
|
||||||
args = x[1:]
|
return x[0], x[1]
|
||||||
}
|
}
|
||||||
|
return x[0], ""
|
||||||
|
}()
|
||||||
|
|
||||||
cmd = x[0]
|
return cmd, arg
|
||||||
|
}
|
||||||
if strings.Index(s, prefix) == 0 {
|
|
||||||
cmd = cmd[len(prefix):]
|
func SplitCommandAndArgs(s, prefix string, n int) (cmd string, args []string) {
|
||||||
}
|
cmd, arg := SplitCommandAndArg(s, prefix)
|
||||||
|
|
||||||
return cmd, args
|
if n == 0 {
|
||||||
|
return cmd, strings.Split(arg, " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd, strings.SplitN(arg, " ", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SplitArgs(s string, n int) (args []string) {
|
||||||
|
if n > 0 {
|
||||||
|
args = strings.SplitN(s, " ", n)
|
||||||
|
} else {
|
||||||
|
args = strings.Split(s, " ")
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
@ -57,20 +57,44 @@ func TestHasCommandCommand(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSplitComandAndArgs(t *testing.T) {
|
func TestSplitCommandAndArg(t *testing.T) {
|
||||||
tables := []struct {
|
tables := []struct {
|
||||||
s string
|
s string
|
||||||
prefix string
|
prefix string
|
||||||
wantCmd string
|
wantCmd string
|
||||||
wantArgs []string
|
wantArg string
|
||||||
}{
|
}{
|
||||||
{"!command x y", "!", "command", []string{"x", "y"}},
|
{"!command x y", "!", "command", "x y"},
|
||||||
{"!command", "!", "command", []string(nil)},
|
{"!command", "!", "command", ""},
|
||||||
{"hey man", "!", "", []string(nil)},
|
{"hey man", "!", "", ""},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
gotCmd, gotArgs := SplitCommandAndArgs(table.s, table.prefix)
|
gotCmd, gotArg := SplitCommandAndArg(table.s, table.prefix)
|
||||||
|
if gotCmd != table.wantCmd {
|
||||||
|
t.Errorf("got: %s, want: %s", gotCmd, table.wantCmd)
|
||||||
|
}
|
||||||
|
if gotArg != table.wantArg {
|
||||||
|
t.Errorf("got: %+v, want: %+v", gotArg, table.wantArg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSplitCommandAndArgs(t *testing.T) {
|
||||||
|
tables := []struct {
|
||||||
|
s string
|
||||||
|
prefix string
|
||||||
|
n int
|
||||||
|
wantCmd string
|
||||||
|
wantArgs []string
|
||||||
|
}{
|
||||||
|
{"!command x y", "!", 2, "command", []string{"x", "y"}},
|
||||||
|
{"!command x y z", "!", 2, "command", []string{"x", "y z"}},
|
||||||
|
{"!command", "!", 1, "command", []string{""}},
|
||||||
|
{"hey man", "!", 1, "", []string{""}},
|
||||||
|
}
|
||||||
|
for _, table := range tables {
|
||||||
|
gotCmd, gotArgs := SplitCommandAndArgs(table.s, table.prefix, table.n)
|
||||||
if gotCmd != table.wantCmd {
|
if gotCmd != table.wantCmd {
|
||||||
t.Errorf("got: %s, want: %s", gotCmd, table.wantCmd)
|
t.Errorf("got: %s, want: %s", gotCmd, table.wantCmd)
|
||||||
}
|
}
|
||||||
@ -79,3 +103,22 @@ func TestSplitComandAndArgs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSplitArgs(t *testing.T) {
|
||||||
|
tables := []struct {
|
||||||
|
s string
|
||||||
|
n int
|
||||||
|
want []string
|
||||||
|
}{
|
||||||
|
{"a b c", 0, []string{"a", "b", "c"}},
|
||||||
|
{"a b c", 1, []string{"a b c"}},
|
||||||
|
{"a b c", 2, []string{"a", "b c"}},
|
||||||
|
{"a b c", 3, []string{"a", "b", "c"}},
|
||||||
|
{"a b c", 4, []string{"a", "b", "c"}},
|
||||||
|
}
|
||||||
|
for _, table := range tables {
|
||||||
|
if got, want := SplitArgs(table.s, table.n), table.want; !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("got: %#v, want: %#v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user