mirror of https://github.com/velour/catbase.git
bot: respond to commands given by a message suffix
This commit is contained in:
parent
d99ee28370
commit
0b553da32d
10
bot/bot.go
10
bot/bot.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -142,10 +143,16 @@ func (b *bot) Who(channel string) []user.User {
|
||||||
return users
|
return users
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var suffixRegex *regexp.Regexp
|
||||||
|
|
||||||
// IsCmd checks if message is a command and returns its curtailed version
|
// IsCmd checks if message is a command and returns its curtailed version
|
||||||
func IsCmd(c *config.Config, message string) (bool, string) {
|
func IsCmd(c *config.Config, message string) (bool, string) {
|
||||||
cmdcs := c.GetArray("CommandChar", []string{"!"})
|
cmdcs := c.GetArray("CommandChar", []string{"!"})
|
||||||
botnick := strings.ToLower(c.Get("Nick", "bot"))
|
botnick := strings.ToLower(c.Get("Nick", "bot"))
|
||||||
|
r := fmt.Sprintf(`(?i)\s*\W*\s*?%s\W*$`, botnick)
|
||||||
|
if suffixRegex == nil {
|
||||||
|
suffixRegex = regexp.MustCompile(r)
|
||||||
|
}
|
||||||
if botnick == "" {
|
if botnick == "" {
|
||||||
log.Fatal().
|
log.Fatal().
|
||||||
Msgf(`You must run catbase -set nick -val <your bot nick>`)
|
Msgf(`You must run catbase -set nick -val <your bot nick>`)
|
||||||
|
@ -164,6 +171,9 @@ func IsCmd(c *config.Config, message string) (bool, string) {
|
||||||
if message[0] == ':' || message[0] == ',' {
|
if message[0] == ':' || message[0] == ',' {
|
||||||
message = message[1:]
|
message = message[1:]
|
||||||
}
|
}
|
||||||
|
} else if suffixRegex.MatchString(message) {
|
||||||
|
iscmd = true
|
||||||
|
message = suffixRegex.ReplaceAllString(message, "")
|
||||||
} else {
|
} else {
|
||||||
for _, cmdc := range cmdcs {
|
for _, cmdc := range cmdcs {
|
||||||
if strings.HasPrefix(lowerMessage, cmdc) && len(cmdc) > 0 {
|
if strings.HasPrefix(lowerMessage, cmdc) && len(cmdc) > 0 {
|
||||||
|
|
Loading…
Reference in New Issue