meme: changed to file upload instead of embed

* Added File handling to Discord mesasges
This commit is contained in:
Chris Sexton 2022-10-20 18:32:49 -04:00
parent 7b39ebf534
commit f6dd52a222
3 changed files with 46 additions and 7 deletions

View File

@ -3,8 +3,10 @@
package bot
import (
"github.com/gabriel-vasile/mimetype"
"net/http"
"regexp"
"strings"
"github.com/jmoiron/sqlx"
@ -50,6 +52,28 @@ type EmbedAuthor struct {
IconURL string
}
type File struct {
Description string
Data []byte
mime *mimetype.MIME
}
func (f File) Mime() *mimetype.MIME {
if f.mime == nil {
f.mime = mimetype.Detect(f.Data)
}
return f.mime
}
func (f File) ContentType() string {
return f.Mime().String()
}
func (f File) FileName() string {
ext := f.Mime().Extension()
return strings.ReplaceAll(f.Description, " ", "-") + ext
}
type ImageAttachment struct {
URL string
AltTxt string

View File

@ -1,6 +1,7 @@
package discord
import (
"bytes"
"errors"
"fmt"
"net/http"
@ -111,6 +112,7 @@ func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...a
}
embeds := []*discordgo.MessageEmbed{}
files := []*discordgo.File{}
for _, arg := range args {
switch a := arg.(type) {
@ -130,17 +132,23 @@ func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...a
Height: a.Height,
}
embeds = append(embeds, embed)
case bot.File:
files = append(files, &discordgo.File{
Name: a.FileName(),
ContentType: a.ContentType(),
Reader: bytes.NewBuffer(a.Data),
})
}
}
data := &discordgo.MessageSend{
Content: message,
Embeds: embeds,
Files: files,
}
log.Debug().
Interface("data", data).
Interface("args", args).
Msg("sending message")
st, err := d.client.ChannelMessageSendComplex(channel, data)

View File

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/google/uuid"
"image"
"image/color"
"image/draw"
@ -209,7 +210,7 @@ func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID strin
encodedSpec, _ := json.Marshal(spec)
w, h, err := p.checkMeme(imgURL)
_, _, err = p.checkMeme(imgURL)
if err != nil {
msg := fmt.Sprintf("Hey %v, I couldn't download that image you asked for.", from.Name)
p.bot.Send(c, bot.Ephemeral, channel, from.ID, msg)
@ -222,12 +223,18 @@ func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID strin
q.Add("spec", string(encodedSpec))
u.RawQuery = q.Encode()
img, err := p.genMeme(spec)
if err != nil {
msg := fmt.Sprintf("Hey %v, I couldn't download that image you asked for.", from.Name)
p.bot.Send(c, bot.Ephemeral, channel, from.ID, msg)
return
}
log.Debug().Msgf("image is at %s", u.String())
_, err = p.bot.Send(c, bot.Message, channel, "", bot.ImageAttachment{
URL: u.String(),
AltTxt: fmt.Sprintf("%s: %s", from.Name, message),
Width: w,
Height: h,
p.bot.Send(c, bot.Message, channel, fmt.Sprintf("%s sent a meme:", from.Name))
_, err = p.bot.Send(c, bot.Message, channel, "", bot.File{
Description: uuid.NewString(),
Data: img,
})
if err == nil && msgID != "" {