From 7ebfd3d5ff86bbabdd95c8d0a395e0690db90792 Mon Sep 17 00:00:00 2001 From: uakci Date: Mon, 15 Aug 2022 18:38:21 +0200 Subject: fix !'s chattiness; fix / handling in ? --- bot.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'bot.go') diff --git a/bot.go b/bot.go index 3eea7d4..2ea9075 100644 --- a/bot.go +++ b/bot.go @@ -32,8 +32,9 @@ const ( ) var ( - markdownLinkRegex = regexp.MustCompile(`!?\[(.*)\]\((.*)\)`) - ports struct { + markdownLinkRe = regexp.MustCompile(`!?\[(.*)\]\((.*)\)`) + alphaHyphenRe = regexp.MustCompile(`^[a-z-]+$`) + ports struct { toa, spe, nui string } ) @@ -191,9 +192,17 @@ func respond(message string, callback func(Response)) { return } } else if strings.HasPrefix(cmd, "?") && len(cmd) > 1 { - returnText(fmt.Sprintf(wikiPageUrl, strings.ReplaceAll(strings.TrimSpace(cmd[1:]+" "+rest), " ", "_"))) + fragments := strings.Split(strings.TrimSpace(cmd[1:]+" "+rest), "/") + for i, fragment := range fragments { + _, offset, err := strings.NewReader(fragment).ReadRune() + if err != nil { + continue + } + fragments[i] = strings.ToUpper(fragment[:offset]) + strings.ReplaceAll(fragment[offset:], " ", "_") + } + returnText(fmt.Sprintf(wikiPageUrl, strings.Join(fragments, "/"))) return - } else if strings.HasPrefix(cmd, "!") && len(cmd) > 1 && cmd != "!iamvoicechat" { + } else if strings.HasPrefix(cmd, "!") && len(args) == 0 && alphaHyphenRe.MatchString(cmd[1:]) && cmd != "!iamvoicechat" { all, err := get(wikiCommandsUrl) if err != nil { returnText(err.Error()) @@ -226,7 +235,7 @@ func respond(message string, callback func(Response)) { } } if selected != "" { - selected = markdownLinkRegex.ReplaceAllString(selected, "$2") + selected = markdownLinkRe.ReplaceAllString(selected, "$2") returnText(selected) } else { returnText(fmt.Sprintf("could not find !%s", cmd[1:])) -- cgit v1.2.3