Skip to content

Commit

Permalink
Don't return slash in Command, strip bot name if needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Syfaro committed Jan 4, 2016
1 parent 7e505ef commit 8cf92d8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -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" {
Expand Down

0 comments on commit 8cf92d8

Please sign in to comment.