forked from Adam-Clrk/rolebot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
247 lines (221 loc) · 6.95 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"errors"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/jessevdk/go-flags"
"log"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
)
var (
runtimeOptions struct {
DiscordToken string `short:"t" long:"token" description:"Discord bot token" required:"true" env:"DISCORD_TOKEN"`
DiscordGuildID string `long:"guildID" description:"Discord guild ID to monitor" default:"" env:"DISCORD_GUILD"`
Verbose bool `short:"v" long:"verbose" description:"Show verbose debug information"`
MaxUserRoles int `long:"maxUserRoles" default:"5" description:"Maximum number of teams a User can join"`
RemoveCommands bool `long:"removeCommands" description:"Remove commands on shutdown"`
}
dSession *discordgo.Session
)
func init() {
_, err := flags.Parse(&runtimeOptions)
if err != nil {
log.Fatal(err)
}
}
func main() {
RunBot()
}
func RunBot() {
var err error
if runtimeOptions.DiscordToken == "" {
os.Exit(1)
}
dSession, err = discordgo.New("Bot " + runtimeOptions.DiscordToken)
if err != nil {
log.Fatal("error creating Discord session,", err)
}
dSession.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
dSession.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
})
err = dSession.Open()
if err != nil {
log.Fatal("error opening connection,", err)
}
defer dSession.Close()
log.Println("adding commands...")
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, v := range commands {
cmd, err := dSession.ApplicationCommandCreate(dSession.State.User.ID, runtimeOptions.DiscordGuildID, v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
registeredCommands[i] = cmd
}
guilds, err := dSession.UserGuilds(100, "", "", false)
log.Print("Running on servers:")
if len(guilds) == 0 {
log.Print("\t(none)")
}
for index := range guilds {
guild := guilds[index]
log.Print("\t", guild.Name, " (", guild.ID, ")")
}
log.Print("Join URL:")
log.Print("https://discordapp.com/api/oauth2/authorize?scope=bot&permissions=268446720&client_id=", dSession.State.User.ID)
log.Print("Bot running. CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
}
func debug(v ...interface{}) {
if runtimeOptions.Verbose {
fa := "Debug: "
v = append([]interface{}{fa}, v...)
log.Print(v...)
}
}
var commands = []*discordgo.ApplicationCommand{
{
Name: "team",
Description: "Manage your membership of LAN teams",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "join",
Description: "Join a team",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "team-name",
Description: "Name of the team you wish to join.",
Required: true,
},
},
},
},
},
}
var commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"team": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options
content := ""
// As you can see, names of subcommands (nested, top-level)
// and subcommand groups are provided through the arguments.
switch options[0].Name {
case "join":
// OOB check
if len(options[0].Options) < 0 {
content = "🤔 Please provide a team name."
break
}
if i.Interaction.GuildID == "" {
content = "😡 This command can only be used in a server."
break
}
roleID := options[0].Options[0].StringValue()
err := validateUserCanJoinRole(s, i.Interaction.Member.User, i.GuildID, roleID)
if err != nil {
content = err.Error()
break
}
role, err := createOrReturnRole(s, i.GuildID, roleID)
if err != nil {
content = err.Error()
break
}
err = s.GuildMemberRoleAdd(i.GuildID, i.Interaction.Member.User.ID, role.ID)
if err != nil {
content = err.Error()
break
}
content = fmt.Sprintln("🙌 Joined", role.Name)
default:
content = "😶 Please use a sub-command."
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: content,
Flags: discordgo.MessageFlagsEphemeral,
},
})
},
}
func validateUserCanJoinRole(s *discordgo.Session, u *discordgo.User, guild string, targetRole string) (err error) {
// This function validates that the given GuildMember satisfies the following rules:
// - is not already assigned to more than 5 Team Roles
// - is not already assigned to the given targetRole
var roleCount int
member, err := s.GuildMember(guild, u.ID)
if err != nil {
return err
}
for _, v := range member.Roles {
role, err := s.State.Role(guild, v)
if err != nil {
return err
}
getRoleName := regexp.MustCompile(`(?i)^(?:Team):* ?(.*)`)
roleName := getRoleName.FindAllStringSubmatch(role.Name, -1)
// role names get normalized to lower case during the lookup only
if roleName != nil && strings.ToLower(roleName[0][1]) == strings.ToLower(targetRole) {
// The Member is already part of the given GuildRole!
return errors.New("⚠️ You are already a member of that team")
}
// Check if it's a team role, and if it is, add to the counter
if strings.HasPrefix(role.Name, "Team:") {
roleCount += 1
}
}
if roleCount >= runtimeOptions.MaxUserRoles {
// Joining this Role would take the user over their limit
return errors.New(fmt.Sprintf("⚠️ You are already a member of %d or more teams! Please contact an administrator if you need more", runtimeOptions.MaxUserRoles))
}
// Succ(ess)
return nil
}
func createOrReturnRole(s *discordgo.Session, guild string, rname string) (v *discordgo.Role, err error) {
roles, err := s.GuildRoles(guild)
getRole := regexp.MustCompile(`(?i)^(?:Team):*`)
if !getRole.MatchString(rname) {
rname = fmt.Sprintln("Team:", rname)
}
rname = strings.Replace(rname, "\n", "", -1)
if err == nil {
for _, v := range roles {
// role names get normalized to lower case during the lookup only
if strings.ToLower(v.Name) == strings.ToLower(rname) {
log.Print("tying", rname, "to old role", v.Name)
return v, nil
}
}
// couldn't find the role in our list, create it
log.Print("creating new role ", rname)
var rColour = 8290694
var rHoist = true
var rMentionable = true
var rPerms int64 = 0
rParams := discordgo.RoleParams{
Name: rname,
Color: &rColour,
Hoist: &rHoist,
Permissions: &rPerms,
Mentionable: &rMentionable,
UnicodeEmoji: nil,
Icon: nil,
}
role, err := s.GuildRoleCreate(guild, &rParams)
return role, err
}
return nil, errors.New("problem creating the target role")
}