From f6dd52a2224139b39c1653bf4e56d833f4ce3306 Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Thu, 20 Oct 2022 18:32:49 -0400 Subject: [PATCH] meme: changed to file upload instead of embed * Added File handling to Discord mesasges --- bot/interfaces.go | 24 ++++++++++++++++++++++++ connectors/discord/discord.go | 10 +++++++++- plugins/meme/meme.go | 19 +++++++++++++------ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/bot/interfaces.go b/bot/interfaces.go index fb27f94..5c11eb6 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -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 diff --git a/connectors/discord/discord.go b/connectors/discord/discord.go index 930a4c8..6e5109a 100644 --- a/connectors/discord/discord.go +++ b/connectors/discord/discord.go @@ -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) diff --git a/plugins/meme/meme.go b/plugins/meme/meme.go index 5865cc2..707da1f 100644 --- a/plugins/meme/meme.go +++ b/plugins/meme/meme.go @@ -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 != "" {