-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
130 lines (120 loc) · 3.04 KB
/
main.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
package main
import (
"fmt"
"image/color"
"os"
"os/signal"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
"golang.org/x/image/colornames"
)
func parseHexOrRGBToColor(s string) (color.RGBA, bool) {
formats := []string{
"%d %d %d",
"%d,%d,%d",
"#%02x%02x%02x",
"%02x%02x%02x",
"#%1x%1x%1x",
"%1x%1x%1x",
}
var c color.RGBA
for _, format := range formats {
if _, err := fmt.Sscanf(s, format, &c.R, &c.G, &c.B); err == nil {
return c, true
}
}
return c, false
}
func parseColornameToColor(s string) (color.RGBA, bool) {
c, ok := colornames.Map[s]
return c, ok
}
func parseMessageToColor(s string) (color.RGBA, bool) {
if s != "" {
parseFunctions := []func(s string) (c color.RGBA, ok bool){
parseHexOrRGBToColor,
parseColornameToColor,
}
for _, parseFunction := range parseFunctions {
if c, ok := parseFunction(s); ok {
return c, ok
}
}
}
return color.RGBA{}, false
}
func createUserRole(dc *discordgo.Session, guild string, user *discordgo.User) (role *discordgo.Role, err error) {
role, err = dc.GuildRoleCreate(guild)
if err != nil {
return
}
role, err = dc.GuildRoleEdit(guild, role.ID, user.ID, role.Color, role.Hoist, role.Permissions, role.Mentionable)
return
}
func RGBToRoleColor(c color.RGBA) int {
return ((int(c.R) & 0x0ff) << 16) | ((int(c.G) & 0x0ff) << 8) | (int(c.B) & 0x0ff)
}
func updateUserRoleColor(dc *discordgo.Session, guild string, user *discordgo.User, role *discordgo.Role, c color.RGBA) (err error) {
_, err = dc.GuildRoleEdit(guild, role.ID, role.Name, RGBToRoleColor(c), role.Hoist, role.Permissions, role.Mentionable)
err = dc.GuildMemberRoleAdd(guild, user.ID, role.ID)
return err
}
func upsertUserRoleColor(dc *discordgo.Session, guild string, user *discordgo.User, c color.RGBA) error {
roles, err := dc.GuildRoles(guild)
if err != nil {
return err
}
var foundRole *discordgo.Role
var roleFound bool
for _, role := range roles {
if role.Name == user.ID {
foundRole = role
roleFound = true
}
}
if !roleFound {
role, err := createUserRole(dc, guild, user)
if err != nil {
return err
}
foundRole = role
roleFound = true
}
err = updateUserRoleColor(dc, guild, user, foundRole, c)
return err
}
func main() {
token := os.Getenv("TOKEN")
channel := os.Getenv("CHANNEL")
dc, err := discordgo.New("Bot " + token)
if err != nil {
panic(err)
}
dc.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.ChannelID != channel || m.Author.Bot {
return
}
c, ok := parseMessageToColor(strings.ToLower(m.Content))
if !ok {
_ = dc.MessageReactionAdd(m.ChannelID, m.ID, "❌")
return
}
err := upsertUserRoleColor(dc, m.GuildID, m.Author, c)
if err != nil {
_ = dc.MessageReactionAdd(m.ChannelID, m.ID, "❌")
return
}
_ = dc.MessageReactionAdd(m.ChannelID, m.ID, "✅")
return
})
dc.Identify.Intents = discordgo.IntentsGuildMessages
err = dc.Open()
if err != nil {
panic(err)
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
_ = dc.Close()
}