Port over saveable command from Python

This commit is contained in:
Michał Gdula 2025-04-13 17:15:31 +01:00
parent 46e98473d0
commit 5bce2f6898
2 changed files with 112 additions and 0 deletions

110
commands/img/img.go Normal file
View file

@ -0,0 +1,110 @@
package img
import (
"errors"
"net/http"
"time"
"github.com/Fluffy-Bean/lynxie/app"
"github.com/Fluffy-Bean/lynxie/utils"
"github.com/bwmarrin/discordgo"
)
const maxFileSize = 1024 * 1024 * 10 // 10MB
var client = http.Client{
Timeout: 10 * time.Second,
}
func RegisterImgCommands(a *app.App) {
a.RegisterCommand("saveable", registerSaveable(a))
a.RegisterCommandAlias("gif", "saveable")
}
func registerSaveable(a *app.App) app.Callback {
return func(h *app.Handler, args []string) app.Error {
fileEndpoint, err := getClosestImage(h)
if err != nil {
return app.Error{
Msg: "Could not get image",
Err: err,
}
}
req, err := http.NewRequest(http.MethodGet, fileEndpoint, nil)
if err != nil {
return app.Error{
Msg: "",
Err: err,
}
}
if req.ContentLength > maxFileSize {
return app.Error{
Msg: "Could not get image",
Err: errors.New("requested file is too big"),
}
}
res, err := client.Do(req)
if err != nil {
return app.Error{
Msg: "",
Err: err,
}
}
defer res.Body.Close()
h.Session.ChannelMessageSendComplex(h.Message.ChannelID, &discordgo.MessageSend{
Embed: &discordgo.MessageEmbed{
Title: "Saveable",
Description: "Image converted to GIF :3",
Image: &discordgo.MessageEmbedImage{
URL: "attachment://image.gif",
},
Color: utils.ColorFromRGB(255, 255, 255),
},
Files: []*discordgo.File{
{
Name: "image.gif",
ContentType: "image/gif",
Reader: res.Body,
},
},
Reference: h.Reference,
})
return app.Error{}
}
}
func getClosestImage(h *app.Handler) (string, error) {
// Get message attachments
if len(h.Message.Attachments) >= 1 {
if h.Message.Attachments[0].Size > maxFileSize {
return "", errors.New("file size is too big")
}
return h.Message.Attachments[0].ProxyURL, nil
}
// If no attachments exist... see if the message is replying to someone
if h.Message.ReferencedMessage != nil {
if len(h.Message.ReferencedMessage.Attachments) >= 1 {
if h.Message.ReferencedMessage.Attachments[0].Size > maxFileSize {
return "", errors.New("file size is too big")
}
return h.Message.ReferencedMessage.Attachments[0].ProxyURL, nil
}
// Maybe replying to an embed...?
if len(h.Message.ReferencedMessage.Embeds) >= 1 {
//... no file size is provided
return h.Message.ReferencedMessage.Embeds[0].Image.ProxyURL, nil
}
}
return "", errors.New("no files exists")
}

View file

@ -5,6 +5,7 @@ import (
"github.com/Fluffy-Bean/lynxie/app" "github.com/Fluffy-Bean/lynxie/app"
"github.com/Fluffy-Bean/lynxie/commands/debug" "github.com/Fluffy-Bean/lynxie/commands/debug"
"github.com/Fluffy-Bean/lynxie/commands/img"
"github.com/Fluffy-Bean/lynxie/commands/porb" "github.com/Fluffy-Bean/lynxie/commands/porb"
"github.com/Fluffy-Bean/lynxie/commands/tinyfox" "github.com/Fluffy-Bean/lynxie/commands/tinyfox"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@ -18,6 +19,7 @@ func main() {
}) })
debug.RegisterDebugCommands(a) debug.RegisterDebugCommands(a)
img.RegisterImgCommands(a)
tinyfox.RegisterTinyfoxCommands(a) tinyfox.RegisterTinyfoxCommands(a)
porb.RegisterPorbCommands(a) porb.RegisterPorbCommands(a)