File tree Expand file tree Collapse file tree 2 files changed +18
-2
lines changed Expand file tree Collapse file tree 2 files changed +18
-2
lines changed Original file line number Diff line number Diff 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.
132134func (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,
Original file line number Diff line number Diff line change @@ -58,7 +58,7 @@ func TestIsCommandWithEmptyText(t *testing.T) {
5858func 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+
8290func TestMessageCommandArgumentsWithArguments (t * testing.T ) {
8391 message := tgbotapi.Message {Text : "/command with arguments" }
8492 if message .CommandArguments () != "with arguments" {
You can’t perform that action at this time.
0 commit comments