|
1 | 1 | //! Page and service to let users into RCOS Discord and give them the verified role.
|
2 | 2 |
|
3 |
| -use crate::api::discord::rcos_discord_verified_role_id; |
| 3 | +use crate::api::discord::{global_discord_client, rcos_discord_verified_role_id}; |
4 | 4 | use crate::api::rcos::users::discord_whois::DiscordWhoIs;
|
5 | 5 | use crate::error::TelescopeError;
|
6 | 6 |
|
7 | 7 | use crate::web::services::auth::identity::AuthenticationCookie;
|
8 | 8 | use actix_web::HttpResponse;
|
9 | 9 | use reqwest::header::LOCATION;
|
| 10 | +use serenity::model::prelude::RoleId; |
| 11 | +use crate::env::global_config; |
10 | 12 |
|
11 | 13 | /// Let users into the RCOS discord.
|
12 | 14 | #[get("/join_discord")]
|
@@ -86,13 +88,26 @@ pub async fn handle(auth: AuthenticationCookie) -> Result<HttpResponse, Telescop
|
86 | 88 | );
|
87 | 89 |
|
88 | 90 | // Get RCOS Discord Verified role ID if possible. If not, user empty role list.
|
89 |
| - let roles = rcos_discord_verified_role_id() |
| 91 | + let verified_role: RoleId = rcos_discord_verified_role_id() |
90 | 92 | .await?
|
91 |
| - .map(|role| vec![role]) |
92 | 93 | .ok_or(TelescopeError::ise("Could not get Verified role ID."))?;
|
93 | 94 |
|
94 |
| - // Add user to Discord. |
95 |
| - discord.add_to_rcos_guild(Some(nickname), roles).await?; |
| 95 | + // Add user to Discord with verified role and nickname. |
| 96 | + discord.add_to_rcos_guild(Some(nickname), vec![verified_role]).await?; |
| 97 | + |
| 98 | + // If user was already in the discord, they may not have the verified role, and the |
| 99 | + // previous call will do nothing. Make an additional call here to add the verified role |
| 100 | + // to the user if they don't already have it. |
| 101 | + // See https://github.com/rcos/Telescope/issues/236. |
| 102 | + |
| 103 | + // Get the rcos discord guild ID. |
| 104 | + let rcos_discord_guild = global_config().discord_config.rcos_guild_id(); |
| 105 | + |
| 106 | + // Make the call to add the verified role |
| 107 | + global_discord_client() |
| 108 | + .add_member_role(rcos_discord_guild, discord_user_id, verified_role.0) |
| 109 | + .await |
| 110 | + .map_err(TelescopeError::serenity_error)?; |
96 | 111 |
|
97 | 112 | // On success, redirect user back to their profile.
|
98 | 113 | Ok(HttpResponse::Found()
|
|
0 commit comments