/
color.go
103 lines (80 loc) · 2.54 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
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
package color
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/pajbot/basecommand"
"github.com/pajbot/pajbot2-discord/pkg"
"github.com/pajbot/pajbot2-discord/pkg/commands"
"github.com/pajbot/pajbot2-discord/pkg/utils"
)
func init() {
commands.Register([]string{"$color", "$colour"}, New())
}
var _ pkg.Command = &Command{}
type Command struct {
basecommand.Command
}
func New() *Command {
return &Command{
Command: basecommand.New(),
}
}
func getColorRole(c string, roles []*discordgo.Role) *discordgo.Role {
for _, role := range roles {
if role.Name == c {
return role
}
}
return nil
}
func (c *Command) Run(s *discordgo.Session, m *discordgo.MessageCreate, parts []string) (res pkg.CommandResult) {
res = pkg.CommandResultUserCooldown
const usage = `$color <colorname>`
var err error
hasAccess, err := utils.MemberInRoles(s, m.GuildID, m.Author.ID, "nitrobooster")
if err != nil {
fmt.Println("Error:", err)
return pkg.CommandResultUserCooldown
}
if !hasAccess {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s, you don't have permission to use $color. Please consider boosting the server to get access to it.", m.Author.Mention()))
return pkg.CommandResultUserCooldown
}
if len(parts) < 2 {
s.ChannelMessageSend(m.ChannelID, "You need to provide a color. Usage: "+usage)
return
}
color := parts[1]
if color == "reset" {
colorRoles := utils.ColorRoles(s, m.GuildID)
err := utils.RemoveNitroColors(s, m.GuildID, m.Author.ID, colorRoles)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Error removing nitro roles: "+err.Error())
}
s.ChannelMessageSend(m.ChannelID, "Removed colors from you")
return
}
colorRoles := utils.ColorRoles(s, m.GuildID)
// Ensure the color exists
colorRole := getColorRole(color, colorRoles)
if colorRole == nil {
s.ChannelMessageSend(m.ChannelID, "The specified color doesn't exist. Use $colors to see what colors are available")
return
}
err = utils.RemoveNitroColors(s, m.GuildID, m.Author.ID, colorRoles)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Error removing previous nitro roles: "+err.Error())
}
// Assign role
err = s.GuildMemberRoleAdd(m.GuildID, m.Author.ID, colorRole.ID)
if err != nil {
fmt.Println("Error assigning role:", err)
s.ChannelMessageSend(m.ChannelID, "Couldn't apply color correctly. Ping @pajlada for more info.")
}
// Announce color success
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Color %s assigned correctly.", color))
return
}
func (c *Command) Description() string {
return c.Command.Description
}