-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandHandler.go
85 lines (70 loc) · 1.66 KB
/
commandHandler.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
package telegram
import (
"fmt"
"strings"
)
type CommandFunc func(cmd CommandInfo)
type Command struct {
Enable bool
CmdName string
Callback CommandFunc
NeedAuthorization bool
AuthorizedUserID []int
UnauthorizedUserID []int
}
func NewCommand(cmdName string, callback CommandFunc) *Command {
return &Command{
CmdName: cmdName,
Enable: true,
Callback: callback,
}
}
func (c *Command) AddAuthorizedId(id ...int) {
}
func (c *Command) DelAuthorizedId(id ...int) {
}
func (c *Command) AddUnauthorizedId(id ...int) {
}
func (c *Command) DelUnauthorizedId(id ...int) {
}
type CommandInfo struct {
CmdName string
Args []string
Msg *MessageTlg
MessageID int
ChatID int
Restricted bool
isAuthorized bool
isAdmin bool
}
// GetInfo parse command and arguments, check authorization
func (c *Command) GetInfo(msg *MessageTlg) (CommandInfo, error) {
var cmdInfo CommandInfo
if c.NeedAuthorization {
userId := msg.From.ID
cmdInfo.isAuthorized = false // reinitialization to secure value
for _, id := range c.AuthorizedUserID {
if id == userId {
cmdInfo.isAuthorized = true
break
}
}
}
if len(msg.Text) < 1 {
return CommandInfo{}, fmt.Errorf("GetInfo: empty msg.Text")
}
cmdInfo.ChatID = msg.Chat.ID
cmdInfo.MessageID = msg.MessageID
cmdInfo.Args = strings.Fields(msg.Text)[1:]
cmdInfo.Msg = msg
cmdInfo.CmdName = c.CmdName
return cmdInfo, nil
}
func GetCommandName(msg *MessageTlg) string {
if len(msg.Entities) > 0 && len(msg.Text) > 0 {
if msg.Entities[0].Type == "bot_command" && msg.Text[0] == '/' {
return strings.Fields(msg.Text)[0][1:]
}
}
return ""
}