From 32af107699e9ded9cc8db908c226a2294335d797 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Thu, 13 Jan 2022 22:34:47 +0000 Subject: [PATCH] discord: add role to user on signup This requires an extra permission, so you'll have to modify your bot settings, kick it from your server and re-add it for this to work. The role you select must also be lower in hierarchy than the bot's group. --- api.go | 23 +++++++++++++++++++++++ config/config-base.json | 12 ++++++++++++ discord.go | 31 +++++++++++++++++++++++++++++++ html/form.html | 8 ++++---- 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/api.go b/api.go index 7aca446..24703ad 100644 --- a/api.go +++ b/api.go @@ -368,6 +368,15 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc success = false return } + err := app.discord.ApplyRole(discordUser.ID) + if err != nil { + f = func(gc *gin.Context) { + app.err.Printf("%s: New user failed: Failed to set member role: %v", req.Code, err) + respond(401, "error", gc) + } + success = false + return + } } } var matrixUser MatrixUser @@ -2025,6 +2034,20 @@ func (app *appContext) GetConfig(gc *gin.Context) { resp.Sections[sectName].Settings[settingName] = s } } + if discordEnabled { + r, err := app.discord.ListRoles() + if err == nil { + roles := make([][2]string, len(r)+1) + roles[0] = [2]string{"", "None"} + for i, role := range r { + roles[i+1] = role + } + s := resp.Sections["discord"].Settings["apply_role"] + s.Options = roles + resp.Sections["discord"].Settings["apply_role"] = s + } + } + resp.Sections["ui"].Settings["language-form"] = fl resp.Sections["ui"].Settings["language-admin"] = al resp.Sections["email"].Settings["language"] = el diff --git a/config/config-base.json b/config/config-base.json index 46b73f7..5a09a4a 100644 --- a/config/config-base.json +++ b/config/config-base.json @@ -686,6 +686,18 @@ "value": "", "description": "Channel to invite new users to." }, + "apply_role": { + "name": "Apply Role on connection", + "required": false, + "requires_restart": true, + "depends_true": "enabled", + "type": "select", + "options": [ + ["", "None"] + ], + "value": "", + "description": "Add the selected role to a user when they sign up." + }, "language": { "name": "Language", "required": false, diff --git a/discord.go b/discord.go index 4027a9e..4c55d00 100644 --- a/discord.go +++ b/discord.go @@ -18,6 +18,7 @@ type DiscordDaemon struct { guildID string serverChannelName, serverName string users map[string]DiscordUser // Map of user IDs to users. Added to on first interaction, and loaded from app.storage.discord on start. + roleID string app *appContext } @@ -38,6 +39,7 @@ func newDiscordDaemon(app *appContext) (*DiscordDaemon, error) { verifiedTokens: map[string]DiscordUser{}, users: map[string]DiscordUser{}, app: app, + roleID: app.config.Section("discord").Key("apply_role").String(), } for _, user := range app.storage.discord { dd.users[user.ID] = user @@ -111,6 +113,35 @@ func (d *DiscordDaemon) run() { return } +// ListRoles returns a list of available (excluding bot and @everyone) roles in a guild as a list of containing an array of the guild ID and its name. +func (d *DiscordDaemon) ListRoles() (roles [][2]string, err error) { + var r []*dg.Role + r, err = d.bot.GuildRoles(d.guildID) + if err != nil { + d.app.err.Printf("Discord: Failed to get roles: %v", err) + return + } + for _, role := range r { + fmt.Println(d.username) + if role.Name != d.username && role.Name != "@everyone" { + roles = append(roles, [2]string{role.ID, role.Name}) + } + } + // roles = make([][2]string, len(r)) + // for i, role := range r { + // roles[i] = [2]string{role.ID, role.Name} + // } + return +} + +// ApplyRole applies the member role to the given user if set. +func (d *DiscordDaemon) ApplyRole(userID string) error { + if d.roleID == "" { + return nil + } + return d.bot.GuildMemberRoleAdd(d.guildID, userID, d.roleID) +} + // NewTempInvite creates an invite link, and returns the invite URL, as well as the URL for the server icon. func (d *DiscordDaemon) NewTempInvite(ageSeconds, maxUses int) (inviteURL, iconURL string) { var inv *dg.Invite diff --git a/html/form.html b/html/form.html index 771b1bc..429e9ee 100644 --- a/html/form.html +++ b/html/form.html @@ -127,21 +127,21 @@ {{ if or (.telegramEnabled) (or .discordEnabled .matrixEnabled) }}
{{ if .telegramEnabled }} {{ end }} {{ if .discordEnabled }} {{ end }} {{ if .matrixEnabled }} {{ end }}