mirror of
https://github.com/Fluffy-Bean/Lynxie.git
synced 2025-05-24 20:34:58 +00:00
Port over saveable command from Python
This commit is contained in:
parent
46e98473d0
commit
5bce2f6898
2 changed files with 112 additions and 0 deletions
110
commands/img/img.go
Normal file
110
commands/img/img.go
Normal 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")
|
||||||
|
}
|
2
main.go
2
main.go
|
@ -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)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue