deadend: add plugin and refactor main

This commit is contained in:
Chris Sexton 2024-03-07 11:53:49 -05:00
parent 95ae28aa0f
commit f602c2ec8d
5 changed files with 137 additions and 263 deletions

106
main.go
View File

@ -6,71 +6,21 @@ package main
import (
"flag"
"github.com/velour/catbase/plugins/gpt"
"github.com/velour/catbase/plugins/pagecomment"
"github.com/velour/catbase/plugins/talker"
"github.com/velour/catbase/plugins/tappd"
"github.com/velour/catbase/plugins/topic"
"github.com/velour/catbase/plugins"
"io"
"math/rand"
"os"
"time"
"github.com/velour/catbase/plugins/cowboy"
"github.com/velour/catbase/plugins/emojy"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/connectors/discord"
"github.com/velour/catbase/plugins/giphy"
"github.com/velour/catbase/plugins/last"
"github.com/velour/catbase/plugins/mayi"
"github.com/velour/catbase/plugins/quotegame"
"github.com/velour/catbase/plugins/rest"
"github.com/velour/catbase/plugins/roles"
"github.com/velour/catbase/plugins/secrets"
"github.com/velour/catbase/plugins/achievements"
"github.com/velour/catbase/plugins/aoc"
"github.com/velour/catbase/plugins/countdown"
"github.com/velour/catbase/plugins/goals"
"github.com/velour/catbase/plugins/meme"
"github.com/velour/catbase/plugins/sms"
"github.com/velour/catbase/plugins/twitter"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/connectors/discord"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/config"
"github.com/velour/catbase/connectors/irc"
"github.com/velour/catbase/connectors/slackapp"
"github.com/velour/catbase/plugins/admin"
"github.com/velour/catbase/plugins/babbler"
"github.com/velour/catbase/plugins/beers"
"github.com/velour/catbase/plugins/couldashouldawoulda"
"github.com/velour/catbase/plugins/counter"
"github.com/velour/catbase/plugins/dice"
"github.com/velour/catbase/plugins/emojifyme"
"github.com/velour/catbase/plugins/fact"
"github.com/velour/catbase/plugins/first"
"github.com/velour/catbase/plugins/git"
"github.com/velour/catbase/plugins/impossible"
"github.com/velour/catbase/plugins/inventory"
"github.com/velour/catbase/plugins/leftpad"
"github.com/velour/catbase/plugins/nerdepedia"
"github.com/velour/catbase/plugins/newsbid"
"github.com/velour/catbase/plugins/picker"
"github.com/velour/catbase/plugins/reaction"
"github.com/velour/catbase/plugins/remember"
"github.com/velour/catbase/plugins/reminder"
"github.com/velour/catbase/plugins/rpgORdie"
"github.com/velour/catbase/plugins/rss"
"github.com/velour/catbase/plugins/sisyphus"
"github.com/velour/catbase/plugins/stock"
"github.com/velour/catbase/plugins/tell"
"github.com/velour/catbase/plugins/tldr"
"github.com/velour/catbase/plugins/twitch"
"github.com/velour/catbase/plugins/your"
)
var (
@ -132,55 +82,7 @@ func main() {
b.GetWeb().RegisterWeb(r, path)
}
b.AddPlugin(admin.New(b))
b.AddPlugin(roles.New(b))
b.AddPlugin(twitch.New(b))
b.AddPlugin(pagecomment.New(b))
b.AddPlugin(secrets.New(b))
b.AddPlugin(mayi.New(b))
b.AddPlugin(giphy.New(b))
b.AddPlugin(emojifyme.New(b))
b.AddPlugin(last.New(b))
b.AddPlugin(first.New(b))
b.AddPlugin(leftpad.New(b))
b.AddPlugin(dice.New(b))
b.AddPlugin(picker.New(b))
b.AddPlugin(tappd.New(b))
b.AddPlugin(beers.New(b))
b.AddPlugin(remember.New(b))
b.AddPlugin(your.New(b))
b.AddPlugin(counter.New(b))
b.AddPlugin(goals.New(b))
b.AddPlugin(reminder.New(b))
b.AddPlugin(babbler.New(b))
b.AddPlugin(rss.New(b))
b.AddPlugin(reaction.New(b))
b.AddPlugin(inventory.New(b))
b.AddPlugin(rpgORdie.New(b))
b.AddPlugin(sisyphus.New(b))
b.AddPlugin(tell.New(b))
b.AddPlugin(couldashouldawoulda.New(b))
b.AddPlugin(nerdepedia.New(b))
b.AddPlugin(tldr.New(b))
b.AddPlugin(stock.New(b))
b.AddPlugin(newsbid.New(b))
b.AddPlugin(twitter.New(b))
b.AddPlugin(git.New(b))
b.AddPlugin(impossible.New(b))
b.AddPlugin(aoc.New(b))
b.AddPlugin(meme.New(b))
b.AddPlugin(achievements.New(b))
b.AddPlugin(sms.New(b))
b.AddPlugin(countdown.New(b))
b.AddPlugin(rest.New(b))
b.AddPlugin(quotegame.New(b))
b.AddPlugin(emojy.New(b))
b.AddPlugin(cowboy.New(b))
b.AddPlugin(topic.New(b))
b.AddPlugin(talker.New(b))
b.AddPlugin(fact.New(b))
// catches anything left, will always return true
b.AddPlugin(gpt.New(b))
plugins.Register(b)
if err := client.Serve(); err != nil {
log.Fatal().Err(err)

View File

@ -0,0 +1,29 @@
package deadend
import (
"github.com/velour/catbase/bot"
"github.com/velour/catbase/config"
"regexp"
)
const defaultMessage = "I don't know how to respond to that. If you'd like to ask GPT, use the `gpt` command."
type DeadEndPlugin struct {
b bot.Bot
c *config.Config
}
func New(b bot.Bot) *DeadEndPlugin {
p := &DeadEndPlugin{
b: b,
c: b.Config(),
}
b.RegisterRegexCmd(p, bot.Message, regexp.MustCompile(`.*`), p.message)
return p
}
func (p *DeadEndPlugin) message(r bot.Request) bool {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel,
p.c.Get("deadend.message", defaultMessage))
return true
}

View File

@ -57,11 +57,6 @@ func (p *GPTPlugin) register() {
HelpText: "set the ChatGPT prompt",
Handler: p.setPromptMessage,
},
{
Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?P<text>.*)`),
Handler: p.chatMessage,
},
}
p.b.RegisterTable(p, p.h)
}

View File

@ -1,156 +0,0 @@
package impossible
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/rs/zerolog/log"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/config"
)
type Impossible struct {
b bot.Bot
c *config.Config
handlers bot.HandlerTable
title string
content []string
updated time.Time
}
func New(b bot.Bot) *Impossible {
i := &Impossible{
b: b,
c: b.Config(),
title: "",
content: []string{},
updated: getTodaysMidnight().Add(time.Hour * -24),
}
b.Register(i, bot.Help, i.help)
i.register()
return i
}
func (p *Impossible) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...any) bool {
p.b.Send(c, bot.Message, message.Channel, "You don't need to do anything. I'll take care of it. But guess what I'm thinking.")
return true
}
func (p *Impossible) tryRefresh(r bot.Request) (sent bool) {
if p.updated.Before(time.Now()) {
if p.title != "" {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("The last impossible wikipedia article was: \"%s\"", p.title))
sent = true
}
for !p.refreshImpossible() {
}
}
return sent
}
func (p *Impossible) register() {
p.handlers = bot.HandlerTable{
{Kind: bot.Message, IsCmd: false,
Regex: regexp.MustCompile(`(?i)^hint|clue$`),
Handler: func(r bot.Request) bool {
if p.tryRefresh(r) {
return true
}
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, p.content[rand.Intn(len(p.content))])
return true
}},
{Kind: bot.Message, IsCmd: false,
Regex: regexp.MustCompile(`(?i)^i friggin give up.?$`),
Handler: func(r bot.Request) bool {
if p.tryRefresh(r) {
return true
}
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", p.title))
for !p.refreshImpossible() {
}
return true
}},
{Kind: bot.Message, IsCmd: false,
Regex: regexp.MustCompile(`.*`),
Handler: func(r bot.Request) bool {
if p.tryRefresh(r) {
return true
}
if strings.Contains(strings.ToLower(r.Msg.Body), strings.ToLower(p.title)) {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", p.title))
for !p.refreshImpossible() {
}
return true
}
return false
}},
}
p.b.RegisterTable(p, p.handlers)
}
func (p *Impossible) refreshImpossible() bool {
p.updated = getTodaysMidnight()
resp, err := http.Get("https://en.wikipedia.org/wiki/Special:Random")
if err != nil {
log.Fatal().Err(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
titleRegex := regexp.MustCompile(`id="firstHeading"[^>]*(?P<Title>[^<]*)`)
results := titleRegex.FindStringSubmatch(string(body))
title := results[1][1:] //remove the leading <
if title == "" {
return false
}
p.title = title
p.content = []string{}
resp, err = http.Get("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&explaintext&titles=" + url.PathEscape(title))
if err != nil {
log.Fatal().Err(err)
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
var object map[string]any
json.Unmarshal([]byte(body), &object)
pages := object["query"].(map[string]any)["pages"].(map[string]any)
for _, page := range pages {
descriptionText := page.(map[string]any)["extract"].(string)
sentences := strings.Split(strings.ReplaceAll(descriptionText, "\n", " "), ". ")
for _, sentence := range sentences {
trimmed := strings.ToLower(strings.TrimSpace(sentence))
if len(trimmed) == 0 || strings.HasPrefix(trimmed, "==") || len(strings.Split(trimmed, " ")) < 5 {
continue
}
censored := strings.ReplaceAll(trimmed, strings.ToLower(title), "?????")
p.content = append(p.content, censored)
}
}
return true
}
func getTodaysMidnight() time.Time {
y, m, d := time.Now().Date()
return time.Date(y, m, d, 0, 0, 0, 0, time.Local)
}

View File

@ -1,3 +1,107 @@
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package plugins
import (
"github.com/velour/catbase/bot"
"github.com/velour/catbase/plugins/achievements"
"github.com/velour/catbase/plugins/admin"
"github.com/velour/catbase/plugins/aoc"
"github.com/velour/catbase/plugins/babbler"
"github.com/velour/catbase/plugins/beers"
"github.com/velour/catbase/plugins/couldashouldawoulda"
"github.com/velour/catbase/plugins/countdown"
"github.com/velour/catbase/plugins/counter"
"github.com/velour/catbase/plugins/cowboy"
"github.com/velour/catbase/plugins/deadend"
"github.com/velour/catbase/plugins/dice"
"github.com/velour/catbase/plugins/emojifyme"
"github.com/velour/catbase/plugins/emojy"
"github.com/velour/catbase/plugins/fact"
"github.com/velour/catbase/plugins/first"
"github.com/velour/catbase/plugins/giphy"
"github.com/velour/catbase/plugins/git"
"github.com/velour/catbase/plugins/goals"
"github.com/velour/catbase/plugins/gpt"
"github.com/velour/catbase/plugins/inventory"
"github.com/velour/catbase/plugins/last"
"github.com/velour/catbase/plugins/leftpad"
"github.com/velour/catbase/plugins/mayi"
"github.com/velour/catbase/plugins/meme"
"github.com/velour/catbase/plugins/nerdepedia"
"github.com/velour/catbase/plugins/newsbid"
"github.com/velour/catbase/plugins/pagecomment"
"github.com/velour/catbase/plugins/picker"
"github.com/velour/catbase/plugins/quotegame"
"github.com/velour/catbase/plugins/reaction"
"github.com/velour/catbase/plugins/remember"
"github.com/velour/catbase/plugins/reminder"
"github.com/velour/catbase/plugins/rest"
"github.com/velour/catbase/plugins/roles"
"github.com/velour/catbase/plugins/rpgORdie"
"github.com/velour/catbase/plugins/rss"
"github.com/velour/catbase/plugins/secrets"
"github.com/velour/catbase/plugins/sisyphus"
"github.com/velour/catbase/plugins/sms"
"github.com/velour/catbase/plugins/stock"
"github.com/velour/catbase/plugins/talker"
"github.com/velour/catbase/plugins/tappd"
"github.com/velour/catbase/plugins/tell"
"github.com/velour/catbase/plugins/tldr"
"github.com/velour/catbase/plugins/topic"
"github.com/velour/catbase/plugins/twitch"
"github.com/velour/catbase/plugins/twitter"
"github.com/velour/catbase/plugins/your"
)
func Register(b bot.Bot) {
b.AddPlugin(admin.New(b))
b.AddPlugin(roles.New(b))
b.AddPlugin(twitch.New(b))
b.AddPlugin(pagecomment.New(b))
b.AddPlugin(secrets.New(b))
b.AddPlugin(mayi.New(b))
b.AddPlugin(giphy.New(b))
b.AddPlugin(emojifyme.New(b))
b.AddPlugin(last.New(b))
b.AddPlugin(first.New(b))
b.AddPlugin(leftpad.New(b))
b.AddPlugin(dice.New(b))
b.AddPlugin(picker.New(b))
b.AddPlugin(tappd.New(b))
b.AddPlugin(beers.New(b))
b.AddPlugin(remember.New(b))
b.AddPlugin(your.New(b))
b.AddPlugin(counter.New(b))
b.AddPlugin(goals.New(b))
b.AddPlugin(reminder.New(b))
b.AddPlugin(babbler.New(b))
b.AddPlugin(rss.New(b))
b.AddPlugin(reaction.New(b))
b.AddPlugin(inventory.New(b))
b.AddPlugin(rpgORdie.New(b))
b.AddPlugin(sisyphus.New(b))
b.AddPlugin(tell.New(b))
b.AddPlugin(couldashouldawoulda.New(b))
b.AddPlugin(nerdepedia.New(b))
b.AddPlugin(tldr.New(b))
b.AddPlugin(stock.New(b))
b.AddPlugin(newsbid.New(b))
b.AddPlugin(twitter.New(b))
b.AddPlugin(git.New(b))
b.AddPlugin(aoc.New(b))
b.AddPlugin(meme.New(b))
b.AddPlugin(achievements.New(b))
b.AddPlugin(sms.New(b))
b.AddPlugin(countdown.New(b))
b.AddPlugin(rest.New(b))
b.AddPlugin(quotegame.New(b))
b.AddPlugin(emojy.New(b))
b.AddPlugin(cowboy.New(b))
b.AddPlugin(topic.New(b))
b.AddPlugin(talker.New(b))
b.AddPlugin(fact.New(b))
b.AddPlugin(gpt.New(b))
// catches anything left, will always return true
b.AddPlugin(deadend.New(b))
}