From ffd46ff190546198ae968051256c9ecd3a95df95 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Sun, 4 Aug 2024 15:37:01 +0100 Subject: [PATCH] discord: option to add/remove role on enable/disable/deletion in Settings > Discord, shown when a role is selected in "Apply Role". The discord housekeeping daemon should pick up users deleted outide of jfa-go too, so users who delete their own accounts should have their roles removed (periodically). --- config/config-base.json | 9 +++++++++ discord.go | 18 ++++++++++++++++++ housekeeping-d.go | 2 ++ logmessages/logmessages.go | 2 +- users.go | 14 ++++++++++++-- 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/config/config-base.json b/config/config-base.json index 1d12d88..08b0aca 100644 --- a/config/config-base.json +++ b/config/config-base.json @@ -1120,6 +1120,15 @@ "value": "", "description": "Add the selected role to a user when they sign up." }, + "disable_enable_role": { + "name": "Remove/add role on user enable/disable/deletion", + "required": false, + "requires_restart": true, + "depends_true": "apply_role", + "type": "bool", + "value": false, + "description": "When a user is disabled or deleted, remove the Discord role, and when re-enabled, add it back." + }, "language": { "name": "Language", "required": false, diff --git a/discord.go b/discord.go index 23169e6..7859caf 100644 --- a/discord.go +++ b/discord.go @@ -167,6 +167,24 @@ func (d *DiscordDaemon) ApplyRole(userID string) error { return d.bot.GuildMemberRoleAdd(d.guildID, userID, d.roleID) } +// RemoveRole removes the member role to the given user if set. +func (d *DiscordDaemon) RemoveRole(userID string) error { + if d.roleID == "" { + return nil + } + return d.bot.GuildMemberRoleRemove(d.guildID, userID, d.roleID) +} + +// SetRoleDisabled removes the role if "disabled", and applies if "!disabled". +func (d *DiscordDaemon) SetRoleDisabled(userID string, disabled bool) (err error) { + if disabled { + err = d.RemoveRole(userID) + } else { + err = d.ApplyRole(userID) + } + return +} + // 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/housekeeping-d.go b/housekeeping-d.go index f62c9d9..5d60e79 100644 --- a/housekeeping-d.go +++ b/housekeeping-d.go @@ -36,6 +36,8 @@ func (app *appContext) clearDiscord() { // Make sure the user doesn't exist, and no other error has occured switch err.(type) { case mediabrowser.ErrUserNotFound: + // Remove role in case their account was deleted oustide of jfa-go + app.discord.RemoveRole(discordUser.MethodID().(string)) app.storage.DeleteDiscordKey(discordUser.JellyfinID) default: continue diff --git a/logmessages/logmessages.go b/logmessages/logmessages.go index 277c883..f65626f 100644 --- a/logmessages/logmessages.go +++ b/logmessages/logmessages.go @@ -167,7 +167,7 @@ const ( UserExists = "user already exists" AccountLinked = "account already linked and require_unique enabled" AccountUnverified = "unverified" - FailedSetDiscordMemberRole = "Failed to set " + Discord + " member role: %v" + FailedSetDiscordMemberRole = "Failed to apply/remove " + Discord + " member role: %v" FailedSetEmailAddress = "Failed to set email address for %s user \"%s\": %v" diff --git a/users.go b/users.go index 7febe89..5fafb39 100644 --- a/users.go +++ b/users.go @@ -174,7 +174,12 @@ func (app *appContext) SetUserDisabled(user mediabrowser.User, disabled bool) (e } if app.discord != nil && app.config.Section("discord").Key("disable_enable_role").MustBool(false) { - // FIXME: Un-apply role + cmUser, ok := app.storage.GetDiscordKey(user.ID) + if ok { + if err := app.discord.SetRoleDisabled(cmUser.MethodID().(string), disabled); err != nil { + app.err.Printf(lm.FailedSetDiscordMemberRole, err) + } + } } return } @@ -198,7 +203,12 @@ func (app *appContext) DeleteUser(user mediabrowser.User) (err error, deleted bo } if app.discord != nil && app.config.Section("discord").Key("disable_enable_role").MustBool(false) { - // FIXME: Un-apply role + cmUser, ok := app.storage.GetDiscordKey(user.ID) + if ok { + if err := app.discord.RemoveRole(cmUser.MethodID().(string)); err != nil { + app.err.Printf(lm.FailedSetDiscordMemberRole, err) + } + } } status, err = app.jf.DeleteUser(user.ID)