1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 09:00:10 +00:00

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).
This commit is contained in:
Harvey Tindall 2024-08-04 15:37:01 +01:00
parent 44311162a6
commit ffd46ff190
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
5 changed files with 42 additions and 3 deletions

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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)