forked from PaulSonOfLars/gotgbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
98 lines (84 loc) · 2.21 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package handlers
import (
"strings"
"unicode/utf8"
"github.com/asvedr/gotgbot/v2"
"github.com/asvedr/gotgbot/v2/ext"
)
type Command struct {
Triggers []rune
AllowEdited bool
AllowChannel bool
Command string // should be lowercase for case-insensitivity
Response Response
}
func NewCommand(c string, r Response) Command {
return Command{
Triggers: []rune{'/'},
AllowEdited: false,
AllowChannel: false,
Command: strings.ToLower(c),
Response: r,
}
}
func (c Command) CheckUpdate(b *gotgbot.Bot, ctx *ext.Context) bool {
if ctx.Message != nil {
if ctx.Message.Text == "" && ctx.Message.Caption == "" {
return false
}
return c.checkMessage(b, ctx.Message)
}
// if no edits and message is edited
if c.AllowEdited && ctx.EditedMessage != nil {
if ctx.EditedMessage.Text == "" && ctx.EditedMessage.Caption == "" {
return false
}
return c.checkMessage(b, ctx.EditedMessage)
}
// if no channel and message is channel message
if c.AllowChannel && ctx.ChannelPost != nil {
if ctx.ChannelPost.Text == "" && ctx.ChannelPost.Caption == "" {
return false
}
return c.checkMessage(b, ctx.ChannelPost)
}
// if no channel, no edits, and post is edited
if c.AllowChannel && c.AllowEdited && ctx.EditedChannelPost != nil {
if ctx.EditedChannelPost.Text == "" && ctx.EditedChannelPost.Caption == "" {
return false
}
return c.checkMessage(b, ctx.EditedChannelPost)
}
return false
}
func (c Command) HandleUpdate(b *gotgbot.Bot, ctx *ext.Context) error {
return c.Response(b, ctx)
}
func (c Command) Name() string {
return "command_" + c.Command
}
func (c Command) checkMessage(b *gotgbot.Bot, msg *gotgbot.Message) bool {
text := msg.Text
if msg.Caption != "" {
text = msg.Caption
}
var cmd string
for _, t := range c.Triggers {
if r, _ := utf8.DecodeRuneInString(text); r != t {
continue
}
split := strings.Split(strings.ToLower(strings.Fields(text)[0]), "@")
if len(split) > 1 && split[1] != strings.ToLower(b.User.Username) {
return false
}
cmd = split[0][1:]
break
}
if cmd == "" {
return false
}
if len(msg.Entities) != 0 && msg.Entities[0].Offset == 0 && msg.Entities[0].Type != "bot_command" {
return false
}
return cmd == c.Command
}