website/integrations: Update discord integration role check expression error handling. (#5709)

* website/integrations: Update discord integration expression error handling

As per discussion in https://github.com/goauthentik/authentik/pull/5701 after merge, we could do with handling the case where the user is not in the guild being queried!

Signed-off-by: Aterfax <Aterfax@users.noreply.github.com>

* Correct lowercase f in False.

Signed-off-by: Aterfax <Aterfax@users.noreply.github.com>

* Update website/integrations/sources/discord/index.md

Co-authored-by: Jens L. <jens@beryju.org>
Signed-off-by: Aterfax <Aterfax@users.noreply.github.com>

---------

Signed-off-by: Aterfax <Aterfax@users.noreply.github.com>
Co-authored-by: Jens L. <jens@beryju.org>
This commit is contained in:
Aterfax 2023-05-21 23:02:40 +01:00 committed by GitHub
parent ba0e64d304
commit 444deae637
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 0 deletions

View File

@ -135,6 +135,18 @@ guild_member_object = requests.get(
} }
).json() ).json()
# The response for JSON errors is held within guild_member_object['code']
# See: https://discord.com/developers/docs/topics/opcodes-and-status-codes#json
# If the user isn't in the queried guild, it gives the somewhat misleading code = 10004.
if "code" in guild_member_object:
if guild_member_object['code'] == 10004:
ak_message(f"User is not a member of {GUILD_NAME_STRING}.")
else:
ak_create_event("discord_error", source=context['source'], code= guild_member_object['code']})
ak_message("Discord API error, try again later.")
# Policy does not match if there is any error.
return False
user_matched = any(ACCEPTED_ROLE_ID == g for g in guild_member_object["roles"]) user_matched = any(ACCEPTED_ROLE_ID == g for g in guild_member_object["roles"])
if not user_matched: if not user_matched:
ak_message(f"User is not a member of the {ROLE_NAME_STRING} role in {GUILD_NAME_STRING}.") ak_message(f"User is not a member of the {ROLE_NAME_STRING} role in {GUILD_NAME_STRING}.")