Compare commits

..

No commits in common. "1d30a252770b6d00a303df01c9153cd454d958dd" and "b2ec081d51da5db3d1488dbca2a0d07ce80fbd63" have entirely different histories.

6 changed files with 10 additions and 176 deletions

View File

@ -4,14 +4,12 @@ package main
import (
"flag"
"github.com/velour/catbase/plugins/emojy"
"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"
@ -172,7 +170,6 @@ func main() {
b.AddPlugin(rest.New(b))
b.AddPlugin(quotegame.New(b))
b.AddPlugin(emojy.New(b))
b.AddPlugin(cowboy.New(b))
// catches anything left, will always return true
b.AddPlugin(fact.New(b))

View File

@ -1,52 +0,0 @@
package cowboy
import (
"fmt"
"regexp"
"github.com/rs/zerolog/log"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/config"
)
type Cowboy struct {
b bot.Bot
c *config.Config
}
func New(b bot.Bot) *Cowboy {
c := Cowboy{
b: b,
c: b.Config(),
}
c.register()
c.registerWeb()
return &c
}
func (p *Cowboy) register() {
tbl := bot.HandlerTable{
{
Kind: bot.Message, IsCmd: false,
Regex: regexp.MustCompile(`(?i)^:cowboy_(?P<what>.+):$`),
Handler: func(r bot.Request) bool {
p.makeCowboy(r)
return true
},
},
}
p.b.RegisterTable(p, tbl)
}
func (p *Cowboy) makeCowboy(r bot.Request) {
log.Debug().Msgf("makeCowboy: %s", r.Values["what"])
base := p.c.Get("baseURL", "http://127.0.0.1:1337")
u := base + "/cowboy/img/" + r.Values["what"]
p.b.Send(r.Conn, bot.Delete, r.Msg.Channel, r.Msg.ID)
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "", bot.ImageAttachment{
URL: u,
AltTxt: fmt.Sprintf("%s: %s", r.Msg.User.Name, r.Msg.Body),
Width: 64,
Height: 64,
})
}

View File

@ -1,83 +0,0 @@
package cowboy
import (
"bytes"
"fmt"
"image"
"image/draw"
"image/png"
"os"
"path"
"github.com/nfnt/resize"
"github.com/velour/catbase/config"
"github.com/velour/catbase/plugins/emojy"
)
func getEmojy(c *config.Config, name string) (image.Image, error) {
files, _, err := emojy.AllFiles(c)
if err != nil {
return nil, err
}
fname, ok := files[name]
if !ok {
return nil, fmt.Errorf("could not find emojy: %s", name)
}
f, err := os.Open(fname)
if err != nil {
return nil, err
}
img, _, err := image.Decode(f)
if err != nil {
return nil, err
}
return img, nil
}
func getCowboyHat(c *config.Config) (image.Image, error) {
emojyPath := c.Get("emojy.path", "emojy")
p := path.Join(emojyPath, c.Get("cowboy.hatname", "hat.png"))
p = path.Clean(p)
f, err := os.Open(p)
if err != nil {
return nil, err
}
img, _, err := image.Decode(f)
if err != nil {
return nil, err
}
return img, nil
}
func cowboyifyImage(c *config.Config, input image.Image) (image.Image, error) {
hat, err := getCowboyHat(c)
if err != nil {
return nil, err
}
targetW := uint(c.GetInt("cowboy.targetw", 64))
hatW, hatH := float64(hat.Bounds().Max.X), float64(hat.Bounds().Max.Y)
newH := uint(float64(targetW) / hatW * hatH)
hat = resize.Resize(targetW, newH, hat, resize.MitchellNetravali)
input = resize.Resize(targetW, targetW, input, resize.MitchellNetravali)
dst := image.NewRGBA(image.Rect(0, 0, 64, 64))
draw.Draw(dst, input.Bounds(), input, image.Point{}, draw.Src)
draw.Draw(dst, hat.Bounds(), hat, image.Point{}, draw.Over)
return dst, nil
}
func cowboy(c *config.Config, name string) ([]byte, error) {
emojy, err := getEmojy(c, name)
if err != nil {
return nil, err
}
img, err := cowboyifyImage(c, emojy)
if err != nil {
return nil, err
}
w := bytes.NewBuffer([]byte{})
err = png.Encode(w, img)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}

View File

@ -1,26 +0,0 @@
package cowboy
import (
"fmt"
"net/http"
"github.com/go-chi/chi/v5"
)
func (p *Cowboy) registerWeb() {
r := chi.NewRouter()
r.HandleFunc("/img/{what}", p.handleImage)
p.b.RegisterWeb(r, "/cowboy")
}
func (p *Cowboy) handleImage(w http.ResponseWriter, r *http.Request) {
what := chi.URLParam(r, "what")
img, err := cowboy(p.c, what)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error: %s", err)
return
}
w.Header().Add("Content-Type", "image/png")
w.Write(img)
}

View File

@ -4,6 +4,8 @@ import (
"bytes"
"encoding/base64"
"fmt"
"github.com/fogleman/gg"
"github.com/gabriel-vasile/mimetype"
"math"
"os"
"path"
@ -12,9 +14,6 @@ import (
"strings"
"time"
"github.com/fogleman/gg"
"github.com/gabriel-vasile/mimetype"
"github.com/forPelevin/gomoji"
"github.com/jmoiron/sqlx"
"github.com/rs/zerolog/log"
@ -193,11 +192,11 @@ func (p *EmojyPlugin) allCounts() (map[string][]EmojyCount, error) {
return out, nil
}
func AllFiles(c *config.Config) (map[string]string, map[string]string, error) {
func (p *EmojyPlugin) allFiles() (map[string]string, map[string]string, error) {
files := map[string]string{}
urls := map[string]string{}
emojyPath := c.Get("emojy.path", "emojy")
baseURL := c.Get("emojy.baseURL", "/emojy/file")
emojyPath := p.c.Get("emojy.path", "emojy")
baseURL := p.c.Get("emojy.baseURL", "/emojy/file")
entries, err := os.ReadDir(emojyPath)
if err != nil {
return nil, nil, err
@ -214,7 +213,7 @@ func AllFiles(c *config.Config) (map[string]string, map[string]string, error) {
}
func (p *EmojyPlugin) isKnownEmojy(name string) (bool, string, string, error) {
allFiles, allURLs, err := AllFiles(p.c)
allFiles, allURLs, err := p.allFiles()
if err != nil {
return false, "", "", err
}

View File

@ -4,6 +4,8 @@ import (
"embed"
"encoding/json"
"fmt"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
"io"
"io/ioutil"
"net/http"
@ -11,9 +13,6 @@ import (
"path"
"path/filepath"
"strings"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
)
//go:embed *.html
@ -47,7 +46,7 @@ func (p *EmojyPlugin) handleAll(w http.ResponseWriter, r *http.Request) {
}
func (p *EmojyPlugin) handleAllFiles(w http.ResponseWriter, r *http.Request) {
_, urlMap, err := AllFiles(p.c)
_, urlMap, err := p.allFiles()
if err != nil {
w.WriteHeader(500)
out, _ := json.Marshal(struct{ err error }{err})