forked from go-chat-bot/bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
55 lines (43 loc) · 1.07 KB
/
parser.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
package bot
import (
"errors"
"regexp"
"strings"
"github.com/mattn/go-shellwords"
)
var (
re = regexp.MustCompile("\\s+") // Matches one or more spaces
)
func parse(s string, channel *ChannelData, user *User) (*Cmd, error) {
c := &Cmd{Raw: s}
s = strings.TrimSpace(s)
if !strings.HasPrefix(s, CmdPrefix) {
return nil, nil
}
c.Channel = strings.TrimSpace(channel.Channel)
c.ChannelData = channel
c.User = user
// Trim the prefix and extra spaces
c.Message = strings.TrimPrefix(s, CmdPrefix)
c.Message = strings.TrimSpace(c.Message)
// check if we have the command and not only the prefix
if c.Message == "" {
return nil, nil
}
// get the command
pieces := strings.SplitN(c.Message, " ", 2)
c.Command = pieces[0]
if len(pieces) > 1 {
// get the arguments and remove extra spaces
c.RawArgs = strings.TrimSpace(pieces[1])
parsedArgs, err := shellwords.Parse(c.RawArgs)
if err != nil {
return nil, errors.New("Error parsing arguments: " + err.Error())
}
c.Args = parsedArgs
}
c.MessageData = &Message{
Text: c.Message,
}
return c, nil
}