Don't return slash in Command, strip bot name if needed.

pull/40/head^2
Syfaro 10 years ago
parent 4a1b9aa938
commit 93225440a7
  1. 10
      types.go
  2. 10
      types_test.go

@ -129,12 +129,20 @@ func (m *Message) IsCommand() bool {
// Command checks if the message was a command and if it was, returns the
// command. If the Message was not a command, it returns an empty string.
//
// If the command contains the at bot syntax, it removes the bot name.
func (m *Message) Command() string {
if !m.IsCommand() {
return ""
}
return strings.SplitN(m.Text, " ", 2)[0]
command := strings.SplitN(m.Text, " ", 2)[0][1:]
if i := strings.Index(command, "@"); i != -1 {
command = command[:i]
}
return command
}
// CommandArguments checks if the message was a command and if it was,

@ -58,7 +58,7 @@ func TestIsCommandWithEmptyText(t *testing.T) {
func TestCommandWithCommand(t *testing.T) {
message := tgbotapi.Message{Text: "/command"}
if message.Command() != "/command" {
if message.Command() != "command" {
t.Fail()
}
}
@ -79,6 +79,14 @@ func TestCommandWithNonCommand(t *testing.T) {
}
}
func TestCommandWithBotName(t *testing.T) {
message := tgbotapi.Message{Text: "/command@testbot"}
if message.Command() != "command" {
t.Fail()
}
}
func TestMessageCommandArgumentsWithArguments(t *testing.T) {
message := tgbotapi.Message{Text: "/command with arguments"}
if message.CommandArguments() != "with arguments" {