/
color.go
74 lines (59 loc) 路 1.62 KB
/
color.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
package plugins
import (
"strings"
"bytes"
"fmt"
"github.com/Seklfreak/Robyul2/helpers"
"github.com/Seklfreak/Robyul2/shardmanager"
"github.com/bwmarrin/discordgo"
colorful "github.com/lucasb-eyer/go-colorful"
cairo "github.com/ungerik/go-cairo"
)
type Color struct{}
func (c *Color) Commands() []string {
return []string{
"color",
"colour",
}
}
const (
PicSize = 200
)
func (c *Color) Init(session *shardmanager.Manager) {
}
func (c *Color) Action(command string, content string, msg *discordgo.Message, session *discordgo.Session) {
if !helpers.ModuleIsAllowed(msg.ChannelID, msg.ID, msg.Author.ID, helpers.ModulePermColor) {
return
}
session.ChannelTyping(msg.ChannelID)
args := strings.Fields(content)
if len(args) <= 0 {
helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.too-few"))
return
}
colorText := args[0]
if !strings.HasPrefix(colorText, "#") {
colorText = "#" + colorText
}
color, err := colorful.Hex(colorText)
if err != nil {
helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.invalid"))
return
}
surface := cairo.NewSurface(cairo.FORMAT_ARGB32, PicSize, PicSize)
surface.SetSourceRGB(color.R, color.G, color.B)
surface.Rectangle(0, 0, float64(PicSize), float64(PicSize))
surface.Fill()
pngBytes, _ := surface.WriteToPNGStream()
_, err = helpers.SendComplex(
msg.ChannelID, &discordgo.MessageSend{
Content: fmt.Sprintf("<@%s> Color `%s`", msg.Author.ID, color.Hex()),
Files: []*discordgo.File{
{
Name: color.Hex() + ".png",
Reader: bytes.NewReader(pngBytes),
},
},
})
helpers.RelaxEmbed(err, msg.ChannelID, msg.ID)
}