From b7576bd8554859c1ae7f5e10343ffe4462d61a36 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Mon, 1 Feb 2021 07:38:12 -0500 Subject: [PATCH] gpt2: refactor --- plugins/gpt2/gpt2.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/plugins/gpt2/gpt2.go b/plugins/gpt2/gpt2.go index bd5b4a8..4bbb81c 100644 --- a/plugins/gpt2/gpt2.go +++ b/plugins/gpt2/gpt2.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "net/http" + "regexp" "strings" "github.com/velour/catbase/bot" @@ -23,27 +24,22 @@ func New(b bot.Bot) *GPT2Plugin { c: b.Config(), } - b.Register(p, bot.Message, p.message) + b.RegisterRegexCmd(p, bot.Message, gpt2Regex, p.gpt2Cmd) b.Register(p, bot.Help, p.help) return p } -const prefix = "gpt2" +var gpt2Regex = regexp.MustCompile(`(?i)^gpt2 (?P.*)$`) -func (p *GPT2Plugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { - ch := message.Channel - lowerBody := strings.ToLower(message.Body) - if message.Command && strings.HasPrefix(lowerBody, prefix) { - input := message.Body[len(prefix)+1:] - txt, err := p.getGPTText(input) - if err != nil { - txt = p.c.Get("gpt.error", "The GPT service is unavailable.") - } - p.b.Send(c, bot.Message, ch, txt) - return true +func (p *GPT2Plugin) gpt2Cmd(r bot.Request) bool { + input := r.Values["input"] + txt, err := p.getGPTText(input) + if err != nil { + txt = p.c.Get("gpt.error", "The GPT service is unavailable.") } - return false + p.b.Send(r.Conn, bot.Message, r.Msg.Channel, txt) + return true } func (p *GPT2Plugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {