-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_parser.go
More file actions
66 lines (51 loc) · 1.51 KB
/
message_parser.go
File metadata and controls
66 lines (51 loc) · 1.51 KB
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
package main
import (
"strings"
"github.com/bwmarrin/discordgo"
)
const commandPrefix = "catan!"
const minCommandLength = 2
type messageParser interface {
isCommand() bool
isCommandAction() bool
numArgumentsAtLeast(int) bool
messageLength() int
getCommand() string
getCommandArgument(int) string
getGuildID() string
}
type discordMessageParser struct {
discordMessage *discordgo.MessageCreate
parsedMessage []string
}
func (parser *discordMessageParser) isCommand() bool {
return strings.HasPrefix(parser.discordMessage.Content, commandPrefix)
}
func (parser *discordMessageParser) isCommandAction() bool {
return parser.messageLength() > 1
}
func (parser *discordMessageParser) numArgumentsAtLeast(n int) bool {
return parser.messageLength() >= (minCommandLength + n)
}
func (parser *discordMessageParser) messageLength() int {
if parser.parsedMessage == nil {
parser.parsedMessage = strings.Split(parser.discordMessage.Content, " ")
}
return len(parser.parsedMessage)
}
func (parser *discordMessageParser) getCommand() string {
if parser.messageLength() == 1 {
return "" // make this return an error in the future
}
return parser.parsedMessage[1]
}
func (parser *discordMessageParser) getCommandArgument(position int) string {
i := minCommandLength + position - 1
if (parser.messageLength()-1) < i || i < 1 {
return "" // make this return an error in the future
}
return parser.parsedMessage[i]
}
func (parser *discordMessageParser) getGuildID() string {
return parser.discordMessage.GuildID
}