/
add_user.go
127 lines (107 loc) · 3.5 KB
/
add_user.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
package game
import (
"fmt"
"strings"
"github.com/itfactory-tm/thomas-bot/pkg/command"
"github.com/bwmarrin/discordgo"
"github.com/itfactory-tm/thomas-bot/pkg/sudo"
)
// adduser contains the bob!adduser and bob!remuser command
type UserCommand struct{}
// NewUserCommand gives a new UserCommand
func NewUserCommand() *UserCommand {
return &UserCommand{}
}
// Register registers the handlers
func (u *UserCommand) Register(registry command.Registry, server command.Server) {
registry.RegisterMessageCreateHandler("adduser", u.addUser)
registry.RegisterMessageCreateHandler("remuser", u.remUser)
}
// InstallSlashCommands registers the slash commands
func (u *UserCommand) InstallSlashCommands(session *discordgo.Session) error {
return nil
}
func (u *UserCommand) addUser(s *discordgo.Session, msg *discordgo.MessageCreate) {
if !sudo.IsItfGameAdmin(msg.Author.ID) {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("%s is not in the sudoers file. This incident will be reported.", msg.Author.ID))
return
}
userId := strings.Trim(msg.Content, "bob!adduser ")
if len(userId) < 1 {
s.ChannelMessageSend(msg.ChannelID, "You need to specify a user by userid")
return
}
userArray := strings.Fields(userId)
roles, err := s.GuildRoles(msg.GuildID)
if err != nil {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("Error getting guild roles: %v", err))
return
}
gameRoleID := ""
for _, role := range roles {
if role.Name == "ITF Gamer" {
gameRoleID = role.ID
}
}
//Adds role of single or multiple users
affectedUsers := ""
for _, user := range userArray {
err = s.GuildMemberRoleAdd(msg.GuildID, user, gameRoleID)
if err != nil {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("Error adding role: %v", err))
return
}
affectedUsers += fmt.Sprintf("<@%s> ", user)
}
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("User added! %s", affectedUsers))
}
func (u *UserCommand) remUser(s *discordgo.Session, msg *discordgo.MessageCreate) {
if !sudo.IsItfGameAdmin(msg.Author.ID) {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("%s is not in the sudoers file. This incident will be reported.", msg.Author.ID))
return
}
userId := strings.Trim(msg.Content, "bob!remuser ")
if len(userId) < 1 {
s.ChannelMessageSend(msg.ChannelID, "You need to specify a user by userid")
return
}
userArray := strings.Fields(userId)
roles, err := s.GuildRoles(msg.GuildID)
if err != nil {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("Error getting guild roles: %v", err))
return
}
gameRoleID := ""
for _, role := range roles {
if role.Name == "ITF Gamer" {
gameRoleID = role.ID
}
}
//Removes role of single or multiple users
affectedUsers := ""
for _, user := range userArray {
err = s.GuildMemberRoleRemove(msg.GuildID, user, gameRoleID)
if err != nil {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("Error removing role: %v", err))
return
}
affectedUsers += fmt.Sprintf("<@%s> ", user)
}
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("User removed! %s", affectedUsers))
}
// Info return the commands in this package
func (u *UserCommand) Info() []command.Command {
return []command.Command{
command.Command{
Name: "adduser",
Category: command.CategoryModeratie,
Description: "Add a user to the ITF Gamer role (ITF Game admin only)",
Hidden: false,
},
command.Command{
Name: "remuser",
Category: command.CategoryModeratie,
Description: "Remove a user from the ITF Gamer role (ITF Game admin only)",
Hidden: false,
}}
}