Skip to content

Commit 8cf92d8

Browse files
author
Syfaro
committed
Don't return slash in Command, strip bot name if needed.
1 parent 7e505ef commit 8cf92d8

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

types.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,20 @@ func (m *Message) IsCommand() bool {
129129

130130
// Command checks if the message was a command and if it was, returns the
131131
// command. If the Message was not a command, it returns an empty string.
132+
//
133+
// If the command contains the at bot syntax, it removes the bot name.
132134
func (m *Message) Command() string {
133135
if !m.IsCommand() {
134136
return ""
135137
}
136138

137-
return strings.SplitN(m.Text, " ", 2)[0]
139+
command := strings.SplitN(m.Text, " ", 2)[0][1:]
140+
141+
if i := strings.Index(command, "@"); i != -1 {
142+
command = command[:i]
143+
}
144+
145+
return command
138146
}
139147

140148
// CommandArguments checks if the message was a command and if it was,

types_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestIsCommandWithEmptyText(t *testing.T) {
5858
func TestCommandWithCommand(t *testing.T) {
5959
message := tgbotapi.Message{Text: "/command"}
6060

61-
if message.Command() != "/command" {
61+
if message.Command() != "command" {
6262
t.Fail()
6363
}
6464
}
@@ -79,6 +79,14 @@ func TestCommandWithNonCommand(t *testing.T) {
7979
}
8080
}
8181

82+
func TestCommandWithBotName(t *testing.T) {
83+
message := tgbotapi.Message{Text: "/command@testbot"}
84+
85+
if message.Command() != "command" {
86+
t.Fail()
87+
}
88+
}
89+
8290
func TestMessageCommandArgumentsWithArguments(t *testing.T) {
8391
message := tgbotapi.Message{Text: "/command with arguments"}
8492
if message.CommandArguments() != "with arguments" {

0 commit comments

Comments
 (0)