forked from Clinet/clinet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd-image.go
88 lines (81 loc) · 2.2 KB
/
cmd-image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"bytes"
"image"
"image/png"
"net/http"
"github.com/bwmarrin/discordgo"
"github.com/disintegration/gift"
)
func commandImage(args []string, env *CommandEnvironment) *discordgo.MessageEmbed {
if len(env.Message.Attachments) > 0 {
for _, attachment := range env.Message.Attachments {
srcImageURL := attachment.URL
srcImageHTTP, err := http.Get(srcImageURL)
if err != nil {
return NewErrorEmbed("Image Error", "Unable to fetch image.")
}
srcImage, _, err := image.Decode(srcImageHTTP.Body)
if err != nil {
return NewErrorEmbed("Image Error", "Unable to decode image.")
}
g := &gift.GIFT{}
var outImage bytes.Buffer
switch args[0] {
case "fliphorizontal":
g = gift.New(gift.FlipHorizontal())
case "flipvertical":
g = gift.New(gift.FlipVertical())
case "grayscale", "greyscale":
g = gift.New(gift.Grayscale())
case "invert":
g = gift.New(gift.Invert())
case "rotate90":
g = gift.New(gift.Rotate90())
case "rotate180":
g = gift.New(gift.Rotate180())
case "rotate270":
g = gift.New(gift.Rotate270())
case "sobel":
g = gift.New(gift.Sobel())
case "transpose":
g = gift.New(gift.Transpose())
case "transverse":
g = gift.New(gift.Transverse())
case "test":
g = gift.New(gift.Convolution(
[]float32{
-1, -1, 0,
-1, 1, 1,
0, 1, 1,
},
false, false, false, 0.0,
))
}
dstImage := image.NewRGBA(g.Bounds(srcImage.Bounds()))
g.Draw(dstImage, srcImage)
err = png.Encode(&outImage, dstImage)
if err != nil {
return NewErrorEmbed("Image Error", "Unable to encode processed image.")
}
_, err = botData.DiscordSession.ChannelMessageSendComplex(env.Channel.ID, &discordgo.MessageSend{
Content: "Processed image:",
File: &discordgo.File{
Name: args[0] + ".png",
Reader: &outImage,
},
Embed: &discordgo.MessageEmbed{
Image: &discordgo.MessageEmbedImage{
URL: "attachment://" + args[0] + ".png",
},
},
})
if err != nil {
return NewErrorEmbed("Image Error", "Unable to upload processed image.")
}
}
} else {
return NewErrorEmbed("Image Error", "You must upload an image to process.")
}
return nil
}