// handler for /generate role commands
handler for /generate role commands
handler for / generate role commands
async fn handle_generate_role(
    ctx: &Context,
    interaction: &ApplicationCommandInteraction,
) -> Option<SerenityResult<()>> {
    let rcos_api_response_project = project_info::CurrProjects::get(0, None)
        .await
        .map_err(|err| {
            error!("Could not query the RCOS API: {}", err);
            err
        });
    if let Err(err) = rcos_api_response_project {
        return Some(
            interaction_error(
                "RCOS API Error",
                "We could not get data about projects because the \
        RCOS API responded with an error. Please contact a coordinator and \
        report this error on Telescope's GitHub.",
                &err,
                &ctx,
                &interaction,
            )
            .await,
        );
    }
    let projects_associate_info = rcos_api_response_project.unwrap().projects;
    // Create role for project if is not previously set.
    for project in projects_associate_info {
        if project.project_role.is_none() {
            let role = GuildId(global_config().discord_config.rcos_guild_id())
                .create_role(&ctx.http, |r| r.name(&project.title).mentionable(true))
                .await
                .map_err(|err| {
                    error!("Could not create the channel: {}", err);
                    err
                });
            if let Err(err) = role {
                return Some(
                    interaction_error(
                        "Discord Error",
                        "We could not create role for projects",
                        &err,
                        &ctx,
                        &interaction,
                    )
                    .await,
                );
            }
            // Insert project role data into database.
            let insert_role = create_project_role::CreateOneProjectRole::execute(
                project.project_id,
                role.unwrap().id.to_string(),
            )
            .await
            .map_err(|err| {
                error!(
                    "Could not insert project role data to into database: {}",
                    err
                );
                err
            });
            if let Err(err) = insert_role {
                return Some(
                    interaction_error(
                        "Database Error",
                        "We could not insert role for projects into database",
                        &err,
                        &ctx,
                        &interaction,
                    )
                    .await,
                );
            }
        }
    }
    let rcos_api_response_small_group = small_group_info::CurrSmallGroups::get(0, None)
        .await
        .map_err(|err| {
            error!("Could not query the RCOS API: {}", err);
            err
        });
    if let Err(err) = rcos_api_response_small_group {
        return Some(
            interaction_error(
                "RCOS API Error",
                "We could not get data about small groups because the \
        RCOS API responded with an error. Please contact a coordinator and \
        report this error on Telescope's GitHub.",
                &err,
                &ctx,
                &interaction,
            )
            .await,
        );
    }
    let small_groups_associate_info = rcos_api_response_small_group.unwrap().small_groups;
    // Create role for project if is not previously set.
    for small_group in small_groups_associate_info {
        let small_group_projects = &small_group.small_group_projects;
        if small_group.small_group_role.is_none() {
            let role = GuildId(global_config().discord_config.rcos_guild_id())
                .create_role(&ctx.http, |r| {
                    r.name(small_group.title.clone()).mentionable(true)
                })
                .await
                .map_err(|err| {
                    error!("Unable to  create the role: {}", err);
                    err
                });
            if let Err(err) = role {
                return Some(
                    interaction_error(
                        "Discord Error",
                        "We could not create role for small groups",
                        &err,
                        &ctx,
                        &interaction,
                    )
                    .await,
                );
            }
            // Insert small group role data into database.
            let insert_role = create_small_group_role::CreateOneSmallGroupRole::execute(
                small_group.small_group_id,
                role.unwrap().id.to_string(),
            )
            .await
            .map_err(|err| {
                error!(
                    "Could not insert small group role data to into database: {}",
                    err
                );
                err
            });
            if let Err(err) = insert_role {
                return Some(
                    interaction_error(
                        "Database Error",
                        "We could not insert role for small groups into database",
                        &err,
                        &ctx,
                        &interaction,
                    )
                    .await,
                );
            }
        }
        // Create role for small group projects if is not previously set.
        for small_group_project in small_group_projects {
            if small_group_project.project.project_role.is_none() {
                let role = GuildId(global_config().discord_config.rcos_guild_id())
                    .create_role(&ctx.http, |r| {
                        r.name(small_group_project.project.title.clone())
                            .mentionable(true)
                    })
                    .await
                    .map_err(|err| {
                        error!("Unable to  create the role: {}", err);
                        err
                    });
                if let Err(err) = role {
                    return Some(
                        interaction_error(
                            "Discord Error",
                            "We could not create role for small group projects",
                            &err,
                            &ctx,
                            &interaction,
                        )
                        .await,
                    );
                }
                let insert_role = create_project_role::CreateOneProjectRole::execute(
                    small_group_project.project.project_id,
                    role.unwrap().id.to_string(),
                )
                .await
                .map_err(|err| {
                    error!(
                        "Could not insert small group role data to into database: {}",
                        err
                    );
                    err
                });
                if let Err(err) = insert_role {
                    return Some(
                        interaction_error(
                            "Database Error",
                            "We could not insert role for small groups into database",
                            &err,
                            &ctx,
                            &interaction,
                        )
                        .await,
                    );
                }
            }
        }
    }
    return None;
}
async fn handle_generate_role ( ctx : & Context , interaction : & ApplicationCommandInteraction , ) -> Option < SerenityResult < ( ) > > { let rcos_api_response_project = project_info :: CurrProjects :: get ( 0 , None ) . await . map_err ( | err | { error ! ( " " err ) ; err } ) ; if let Err ( err ) = rcos_api_response_project { return Some ( interaction_error ( " " , " \
 \
 " , & err , & ctx , & interaction , ) . await , ) ; } let projects_associate_info = rcos_api_response_project . unwrap ( ) . projects ; // Create role for project if is not previously set. for project in projects_associate_info { if project . project_role . is_none ( ) { let role = GuildId ( global_config ( ) . discord_config . rcos_guild_id ( ) ) . create_role ( & ctx . http , | r | r . name ( & project . title ) . mentionable ( true ) ) . await . map_err ( | err | { error ! ( " " err ) ; err } ) ; if let Err ( err ) = role { return Some ( interaction_error ( " " , " " , & err , & ctx , & interaction , ) . await , ) ; } // Insert project role data into database. let insert_role = create_project_role :: CreateOneProjectRole :: execute ( project . project_id , role . unwrap ( ) . id . to_string ( ) , ) . await . map_err ( | err | { error ! ( " " err ) ; err } ) ; if let Err ( err ) = insert_role { return Some ( interaction_error ( " " , " " , & err , & ctx , & interaction , ) . await , ) ; } } } let rcos_api_response_small_group = small_group_info :: CurrSmallGroups :: get ( 0 , None ) . await . map_err ( | err | { error ! ( " " err ) ; err } ) ; if let Err ( err ) = rcos_api_response_small_group { return Some ( interaction_error ( " " , " \
 \
 " , & err , & ctx , & interaction , ) . await , ) ; } let small_groups_associate_info = rcos_api_response_small_group . unwrap ( ) . small_groups ; // Create role for project if is not previously set. for small_group in small_groups_associate_info { let small_group_projects = & small_group . small_group_projects ; if small_group . small_group_role . is_none ( ) { let role = GuildId ( global_config ( ) . discord_config . rcos_guild_id ( ) ) . create_role ( & ctx . http , | r | { r . name ( small_group . title . clone ( ) ) . mentionable ( true ) } ) . await . map_err ( | err | { error ! ( " " err ) ; err } ) ; if let Err ( err ) = role { return Some ( interaction_error ( " " , " " , & err , & ctx , & interaction , ) . await , ) ; } // Insert small group role data into database. let insert_role = create_small_group_role :: CreateOneSmallGroupRole :: execute ( small_group . small_group_id , role . unwrap ( ) . id . to_string ( ) , ) . await . map_err ( | err | { error ! ( " " err ) ; err } ) ; if let Err ( err ) = insert_role { return Some ( interaction_error ( " " , " " , & err , & ctx , & interaction , ) . await , ) ; } } // Create role for small group projects if is not previously set. for small_group_project in small_group_projects { if small_group_project . project . project_role . is_none ( ) { let role = GuildId ( global_config ( ) . discord_config . rcos_guild_id ( ) ) . create_role ( & ctx . http , | r | { r . name ( small_group_project . project . title . clone ( ) ) . mentionable ( true ) } ) . await . map_err ( | err | { error ! ( " " err ) ; err } ) ; if let Err ( err ) = role { return Some ( interaction_error ( " " , " " , & err , & ctx , & interaction , ) . await , ) ; } let insert_role = create_project_role :: CreateOneProjectRole :: execute ( small_group_project . project . project_id , role . unwrap ( ) . id . to_string ( ) , ) . await . map_err ( | err | { error ! ( " " err ) ; err } ) ; if let Err ( err ) = insert_role { return Some ( interaction_error ( " " , " " , & err , & ctx , & interaction , ) . await , ) ; } } } } return None ; }
handler for /generate role commands
handler for / generate role commands
ctx
&Context
interaction
&ApplicationCommandInteraction