-
Notifications
You must be signed in to change notification settings - Fork 921
/
simpleembed.go
131 lines (108 loc) · 3.86 KB
/
simpleembed.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package simpleembed
import (
"strconv"
"strings"
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/common"
"golang.org/x/image/colornames"
)
var Command = &commands.YAGCommand{
CmdCategory: commands.CategoryFun,
Name: "SimpleEmbed",
Aliases: []string{"se"},
Description: "A more simpler version of CustomEmbed, controlled completely using switches.",
ArgSwitches: []*dcmd.ArgDef{
&dcmd.ArgDef{Switch: "channel", Help: "Optional channel to send in", Type: dcmd.Channel},
&dcmd.ArgDef{Switch: "title", Type: dcmd.String, Default: ""},
&dcmd.ArgDef{Switch: "desc", Type: dcmd.String, Help: "Text in the 'description' field", Default: ""},
&dcmd.ArgDef{Switch: "color", Help: "Either hex code or name", Type: dcmd.String, Default: ""},
&dcmd.ArgDef{Switch: "url", Help: "Url of this embed", Type: dcmd.String, Default: ""},
&dcmd.ArgDef{Switch: "thumbnail", Help: "Url to a thumbnail", Type: dcmd.String, Default: ""},
&dcmd.ArgDef{Switch: "image", Help: "Url to an image", Type: dcmd.String, Default: ""},
&dcmd.ArgDef{Switch: "author", Help: "The text in the 'author' field", Type: dcmd.String, Default: ""},
&dcmd.ArgDef{Switch: "authoricon", Help: "Url to a icon for the 'author' field", Type: dcmd.String, Default: ""},
&dcmd.ArgDef{Switch: "footer", Help: "Text content for the footer", Type: dcmd.String, Default: ""},
&dcmd.ArgDef{Switch: "footericon", Help: "Url to a icon for the 'footer' field", Type: dcmd.String, Default: ""},
},
RunFunc: func(data *dcmd.Data) (interface{}, error) {
embed := &discordgo.MessageEmbed{
Title: data.Switch("title").Str(),
Description: data.Switch("desc").Str(),
URL: data.Switch("url").Str(),
}
if color := data.Switch("color").Str(); color != "" {
parsedColor, ok := ParseColor(color)
if !ok {
return "Unknown color: " + color + ", can be either hex color code or name for a known color", nil
}
embed.Color = parsedColor
}
if author := data.Switch("author").Str(); author != "" {
embed.Author = &discordgo.MessageEmbedAuthor{
Name: author,
IconURL: data.Switch("authoricon").Str(),
}
}
if thumbnail := data.Switch("thumbnail").Str(); thumbnail != "" {
embed.Thumbnail = &discordgo.MessageEmbedThumbnail{
URL: thumbnail,
}
}
if image := data.Switch("image").Str(); image != "" {
embed.Image = &discordgo.MessageEmbedImage{
URL: image,
}
}
footer := data.Switch("footer").Str()
footerIcon := data.Switch("footericon").Str()
if footer != "" || footerIcon != "" {
embed.Footer = &discordgo.MessageEmbedFooter{
Text: footer,
IconURL: footerIcon,
}
}
cID := data.Msg.ChannelID
c := data.Switch("channel")
if c.Value != nil {
cID = c.Value.(*dstate.ChannelState).ID
hasPerms, err := bot.AdminOrPermMS(cID, data.MS, discordgo.PermissionSendMessages|discordgo.PermissionReadMessages)
if err != nil {
return "Failed checking permissions, please try again or join the support server.", err
}
if !hasPerms {
return "You do not have permissions to send messages there", nil
}
}
_, err := common.BotSession.ChannelMessageSendEmbed(cID, embed)
if err != nil {
return err, err
}
if cID != data.Msg.ChannelID {
return "Done", nil
}
return nil, nil
},
}
func ParseColor(raw string) (int, bool) {
if strings.HasPrefix(raw, "#") {
raw = raw[1:]
}
// try to parse as hex color code first
parsed, err := strconv.ParseInt(raw, 16, 32)
if err == nil {
return int(parsed), true
}
// look up the color code table
for _, v := range colornames.Names {
if strings.EqualFold(v, raw) {
cStruct := colornames.Map[v]
color := (int(cStruct.R) << 16) | (int(cStruct.G) << 8) | int(cStruct.B)
return color, true
}
}
return 0, false
}