mirror of https://github.com/velour/catbase.git
meme: changed to file upload instead of embed
* Added File handling to Discord mesasges
This commit is contained in:
parent
7b39ebf534
commit
f6dd52a222
|
@ -3,8 +3,10 @@
|
||||||
package bot
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/gabriel-vasile/mimetype"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
|
||||||
|
@ -50,6 +52,28 @@ type EmbedAuthor struct {
|
||||||
IconURL string
|
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 {
|
type ImageAttachment struct {
|
||||||
URL string
|
URL string
|
||||||
AltTxt string
|
AltTxt string
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package discord
|
package discord
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -111,6 +112,7 @@ func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...a
|
||||||
}
|
}
|
||||||
|
|
||||||
embeds := []*discordgo.MessageEmbed{}
|
embeds := []*discordgo.MessageEmbed{}
|
||||||
|
files := []*discordgo.File{}
|
||||||
|
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
switch a := arg.(type) {
|
switch a := arg.(type) {
|
||||||
|
@ -130,17 +132,23 @@ func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...a
|
||||||
Height: a.Height,
|
Height: a.Height,
|
||||||
}
|
}
|
||||||
embeds = append(embeds, embed)
|
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{
|
data := &discordgo.MessageSend{
|
||||||
Content: message,
|
Content: message,
|
||||||
Embeds: embeds,
|
Embeds: embeds,
|
||||||
|
Files: files,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Interface("data", data).
|
Interface("data", data).
|
||||||
Interface("args", args).
|
|
||||||
Msg("sending message")
|
Msg("sending message")
|
||||||
|
|
||||||
st, err := d.client.ChannelMessageSendComplex(channel, data)
|
st, err := d.client.ChannelMessageSendComplex(channel, data)
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/google/uuid"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/draw"
|
"image/draw"
|
||||||
|
@ -209,7 +210,7 @@ func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID strin
|
||||||
|
|
||||||
encodedSpec, _ := json.Marshal(spec)
|
encodedSpec, _ := json.Marshal(spec)
|
||||||
|
|
||||||
w, h, err := p.checkMeme(imgURL)
|
_, _, err = p.checkMeme(imgURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := fmt.Sprintf("Hey %v, I couldn't download that image you asked for.", from.Name)
|
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)
|
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))
|
q.Add("spec", string(encodedSpec))
|
||||||
u.RawQuery = q.Encode()
|
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())
|
log.Debug().Msgf("image is at %s", u.String())
|
||||||
_, err = p.bot.Send(c, bot.Message, channel, "", bot.ImageAttachment{
|
p.bot.Send(c, bot.Message, channel, fmt.Sprintf("%s sent a meme:", from.Name))
|
||||||
URL: u.String(),
|
_, err = p.bot.Send(c, bot.Message, channel, "", bot.File{
|
||||||
AltTxt: fmt.Sprintf("%s: %s", from.Name, message),
|
Description: uuid.NewString(),
|
||||||
Width: w,
|
Data: img,
|
||||||
Height: h,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if err == nil && msgID != "" {
|
if err == nil && msgID != "" {
|
||||||
|
|
Loading…
Reference in New Issue