mirror of https://github.com/velour/catbase.git
meme: add signature icon
This commit is contained in:
parent
c184566f07
commit
10a1745502
|
@ -422,7 +422,12 @@ func (s *SlackApp) buildMessage(m *slackevents.MessageEvent) msg.Message {
|
||||||
if m.BotID != "" {
|
if m.BotID != "" {
|
||||||
defaultName = m.Username
|
defaultName = m.Username
|
||||||
}
|
}
|
||||||
|
icon := ""
|
||||||
|
log.Debug().Msgf("Getting user %s", m.User)
|
||||||
name, u := s.getUser(m.User, defaultName)
|
name, u := s.getUser(m.User, defaultName)
|
||||||
|
if u != nil {
|
||||||
|
icon = u.Profile.Image192
|
||||||
|
}
|
||||||
if m.Username != "" && name == "unknown" {
|
if m.Username != "" && name == "unknown" {
|
||||||
name = m.Username
|
name = m.Username
|
||||||
}
|
}
|
||||||
|
@ -438,7 +443,7 @@ func (s *SlackApp) buildMessage(m *slackevents.MessageEvent) msg.Message {
|
||||||
User: &user.User{
|
User: &user.User{
|
||||||
ID: m.User,
|
ID: m.User,
|
||||||
Name: name,
|
Name: name,
|
||||||
Icon: u.Profile.Image192,
|
Icon: icon,
|
||||||
},
|
},
|
||||||
Body: text,
|
Body: text,
|
||||||
Raw: m,
|
Raw: m,
|
||||||
|
@ -637,14 +642,23 @@ func (s *SlackApp) reactionReceived(event *slack.ReactionAddedEvent) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SlackApp) Profile(name string) (user.User, error) {
|
func (s *SlackApp) Profile(name string) (user.User, error) {
|
||||||
n, u := s.getUser(name, "unknown")
|
log.Debug().Msgf("Getting profile for %s", name)
|
||||||
if n == "unknown" {
|
|
||||||
return user.User{}, fmt.Errorf("user %s is not known to us", name)
|
users, err := s.api.GetUsers()
|
||||||
|
if err != nil {
|
||||||
|
return user.User{}, err
|
||||||
}
|
}
|
||||||
return user.User{
|
|
||||||
ID: u.ID,
|
for _, u := range users {
|
||||||
Name: n,
|
if u.Name == name {
|
||||||
Admin: false,
|
return user.User{
|
||||||
Icon: u.Profile.Image192,
|
ID: u.ID,
|
||||||
}, nil
|
Name: stringForUser(&u),
|
||||||
|
Admin: false,
|
||||||
|
Icon: u.Profile.Image192,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return user.User{}, fmt.Errorf("user %s not found", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"image"
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"image/draw"
|
||||||
"image/png"
|
"image/png"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -214,14 +216,25 @@ func (p *MemePlugin) slashMeme(c bot.Connector) http.HandlerFunc {
|
||||||
isCmd, message := bot.IsCmd(p.c, parts[1])
|
isCmd, message := bot.IsCmd(p.c, parts[1])
|
||||||
format := parts[0]
|
format := parts[0]
|
||||||
|
|
||||||
|
bullyIcon := ""
|
||||||
|
|
||||||
for _, bully := range p.c.GetArray("meme.bully", []string{}) {
|
for _, bully := range p.c.GetArray("meme.bully", []string{}) {
|
||||||
if format == bully {
|
if format == bully {
|
||||||
|
if u, err := c.Profile(bully); err == nil {
|
||||||
|
bullyIcon = u.Icon
|
||||||
|
} else {
|
||||||
|
log.Debug().Err(err).Msgf("could not get profile for %s", format)
|
||||||
|
}
|
||||||
formats := p.c.GetMap("meme.memes", defaultFormats)
|
formats := p.c.GetMap("meme.memes", defaultFormats)
|
||||||
format = randEntry(formats)
|
format = randEntry(formats)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if u, err := c.Profile(from); bullyIcon == "" && err == nil {
|
||||||
|
bullyIcon = u.Icon
|
||||||
|
}
|
||||||
|
|
||||||
log.Debug().Strs("parts", parts).Msgf("Meme:\n%+v", text)
|
log.Debug().Strs("parts", parts).Msgf("Meme:\n%+v", text)
|
||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
w.Write(nil)
|
w.Write(nil)
|
||||||
|
@ -239,7 +252,7 @@ func (p *MemePlugin) slashMeme(c bot.Connector) http.HandlerFunc {
|
||||||
message = top
|
message = top
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := p.genMeme(format, top, bottom)
|
id, err := p.genMeme(format, top, bottom, bullyIcon)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := fmt.Sprintf("Hey %s, I couldn't download that image you asked for.", from)
|
msg := fmt.Sprintf("Hey %s, I couldn't download that image you asked for.", from)
|
||||||
p.bot.Send(c, bot.Message, channel, msg)
|
p.bot.Send(c, bot.Message, channel, msg)
|
||||||
|
@ -304,7 +317,7 @@ var defaultFormats = map[string]string{
|
||||||
"raptor": "Philosoraptor.jpg",
|
"raptor": "Philosoraptor.jpg",
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *MemePlugin) genMeme(meme, top, bottom string) (string, error) {
|
func (p *MemePlugin) genMeme(meme, top, bottom, bully string) (string, error) {
|
||||||
fontSizes := []float64{48, 36, 24, 16, 12}
|
fontSizes := []float64{48, 36, 24, 16, 12}
|
||||||
fontSize := fontSizes[0]
|
fontSize := fontSizes[0]
|
||||||
|
|
||||||
|
@ -354,6 +367,10 @@ func (p *MemePlugin) genMeme(meme, top, bottom string) (string, error) {
|
||||||
h = r.Dy()
|
h = r.Dy()
|
||||||
log.Debug().Msgf("resized to %v, %v", w, h)
|
log.Debug().Msgf("resized to %v, %v", w, h)
|
||||||
|
|
||||||
|
if bully != "" {
|
||||||
|
img = p.applyBully(img, bully)
|
||||||
|
}
|
||||||
|
|
||||||
m := gg.NewContext(w, h)
|
m := gg.NewContext(w, h)
|
||||||
m.DrawImage(img, 0, 0)
|
m.DrawImage(img, 0, 0)
|
||||||
fontLocation := p.c.Get("meme.font", "impact.ttf")
|
fontLocation := p.c.Get("meme.font", "impact.ttf")
|
||||||
|
@ -407,3 +424,57 @@ func (p *MemePlugin) genMeme(meme, top, bottom string) (string, error) {
|
||||||
|
|
||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *MemePlugin) applyBully(img image.Image, bully string) image.Image {
|
||||||
|
log.Debug().Msgf("applying bully: %s", bully)
|
||||||
|
dst := image.NewRGBA(img.Bounds())
|
||||||
|
u, err := url.Parse(bully)
|
||||||
|
if err != nil {
|
||||||
|
return img
|
||||||
|
}
|
||||||
|
bullyImg, err := DownloadTemplate(u)
|
||||||
|
if err != nil {
|
||||||
|
return img
|
||||||
|
}
|
||||||
|
|
||||||
|
scaleFactor := p.c.GetFloat64("meme.bullyScale", 0.1)
|
||||||
|
|
||||||
|
newSzX := uint(float64(img.Bounds().Max.X) * scaleFactor)
|
||||||
|
newSzY := uint(float64(img.Bounds().Max.Y) * scaleFactor)
|
||||||
|
|
||||||
|
bullyImg = resize.Resize(newSzX, newSzY, bullyImg, resize.Lanczos3)
|
||||||
|
|
||||||
|
draw.Draw(dst, img.Bounds(), img, image.Point{}, draw.Src)
|
||||||
|
srcSz := img.Bounds().Size()
|
||||||
|
|
||||||
|
w, h := bullyImg.Bounds().Max.X, bullyImg.Bounds().Max.Y
|
||||||
|
|
||||||
|
pt := image.Point{srcSz.X - w, srcSz.Y - h}
|
||||||
|
rect := image.Rect(pt.X, pt.Y, srcSz.X, srcSz.Y)
|
||||||
|
|
||||||
|
//draw.Draw(dst, image.Rect(srcSz.X-128, srcSz.Y-128, srcSz.X, srcSz.Y), bullyImg, image.Point{}, draw.Src)
|
||||||
|
draw.DrawMask(dst, rect, bullyImg, image.Point{}, &circle{image.Point{w / 2, h / 2}, w / 2}, image.Point{}, draw.Over)
|
||||||
|
return dst
|
||||||
|
}
|
||||||
|
|
||||||
|
// the following is ripped off of https://blog.golang.org/image-draw
|
||||||
|
type circle struct {
|
||||||
|
p image.Point
|
||||||
|
r int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *circle) ColorModel() color.Model {
|
||||||
|
return color.AlphaModel
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *circle) Bounds() image.Rectangle {
|
||||||
|
return image.Rect(c.p.X-c.r, c.p.Y-c.r, c.p.X+c.r, c.p.Y+c.r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *circle) At(x, y int) color.Color {
|
||||||
|
xx, yy, rr := float64(x-c.p.X)+0.5, float64(y-c.p.Y)+0.5, float64(c.r)
|
||||||
|
if xx*xx+yy*yy < rr*rr {
|
||||||
|
return color.Alpha{255}
|
||||||
|
}
|
||||||
|
return color.Alpha{0}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue