Skip to content

Commit df6da4a

Browse files
authored
fix: ignore common use command with exact mismatched with bot username (#142)
1 parent cc19d5a commit df6da4a

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

pkg/bots/tgbot/cancel_command.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package tgbot
22

33
import (
4+
"fmt"
5+
46
"github.com/nekomeowww/fo"
57
"github.com/nekomeowww/insights-bot/pkg/types/telegram"
68
"github.com/samber/lo"
@@ -38,8 +40,8 @@ func (h *cancelCommandHandler) handle(c *Context) (Response, error) {
3840
if is &&
3941
c.Update.Message != nil &&
4042
c.Update.Message.Chat != nil &&
41-
c.Update.Message.CommandWithAt() == h.Command() &&
42-
lo.Contains([]telegram.ChatType{telegram.ChatTypeGroup, telegram.ChatTypeSuperGroup}, telegram.ChatType(c.Update.Message.Chat.Type)) {
43+
lo.Contains([]telegram.ChatType{telegram.ChatTypeGroup, telegram.ChatTypeSuperGroup}, telegram.ChatType(c.Update.Message.Chat.Type)) &&
44+
c.Update.Message.CommandWithAt() != fmt.Sprintf("%s@%s", h.Command(), c.Bot.Self.UserName) {
4345
return nil, nil
4446
}
4547

pkg/bots/tgbot/help_command.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ func (h *helpCommandHandler) handle(c *Context) (Response, error) {
4848
if is &&
4949
c.Update.Message != nil &&
5050
c.Update.Message.Chat != nil &&
51-
c.Update.Message.CommandWithAt() == h.Command() &&
52-
lo.Contains([]telegram.ChatType{telegram.ChatTypeGroup, telegram.ChatTypeSuperGroup}, telegram.ChatType(c.Update.Message.Chat.Type)) {
51+
lo.Contains([]telegram.ChatType{telegram.ChatTypeGroup, telegram.ChatTypeSuperGroup}, telegram.ChatType(c.Update.Message.Chat.Type)) &&
52+
!lo.Contains([]string{
53+
fmt.Sprintf("%s@%s", h.Command(), c.Bot.Self.UserName),
54+
fmt.Sprintf("%s@%s", "start", c.Bot.Self.UserName),
55+
}, c.Update.Message.CommandWithAt()) {
5356
return nil, nil
5457
}
5558

pkg/bots/tgbot/start_command.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package tgbot
22

33
import (
4+
"fmt"
5+
46
"github.com/nekomeowww/insights-bot/pkg/types/telegram"
57
"github.com/samber/lo"
68
)
@@ -34,8 +36,8 @@ func (h *startCommandHandler) handle(c *Context) (Response, error) {
3436
if is &&
3537
c.Update.Message != nil &&
3638
c.Update.Message.Chat != nil &&
37-
c.Update.Message.CommandWithAt() == h.Command() &&
38-
lo.Contains([]telegram.ChatType{telegram.ChatTypeGroup, telegram.ChatTypeSuperGroup}, telegram.ChatType(c.Update.Message.Chat.Type)) {
39+
lo.Contains([]telegram.ChatType{telegram.ChatTypeGroup, telegram.ChatTypeSuperGroup}, telegram.ChatType(c.Update.Message.Chat.Type)) &&
40+
c.Update.Message.CommandWithAt() != fmt.Sprintf("%s@%s", h.Command(), c.Bot.Self.UserName) {
3941
return nil, nil
4042
}
4143

0 commit comments

Comments
 (0)