diff --git a/src/database.rs b/src/database.rs index 5a0cdc07fc2cbf107740b3ed4ec90d5a5d4e8adb..40c621698c2e95dce7ef4357698e7dd7b2944b48 100644 --- a/src/database.rs +++ b/src/database.rs @@ -18,6 +18,7 @@ pub struct Member { pub github: Option<String>, pub photo: Option<String>, pub website: Option<String>, + pub study: Option<String>, } table! { @@ -31,6 +32,7 @@ table! { github -> Nullable<Text>, photo -> Nullable<Text>, website -> Nullable<Text>, + study -> Nullable<Text>, } } @@ -94,26 +96,32 @@ pub fn get_member_info_from_tla(tla: &str) -> Result<Member, Error> { .first(&db_connection()) } -pub fn set_member_bio(discord_id: &u64, bio: &str) -> Result<usize, Error> { +pub fn set_member_bio(discord_id: &u64, bio: Option<&str>) -> Result<usize, Error> { diesel::update(members::table.find(*discord_id as i64)) .set(members::biography.eq(bio)) .execute(&db_connection()) } -pub fn set_member_git(discord_id: &u64, git: &str) -> Result<usize, Error> { +pub fn set_member_git(discord_id: &u64, git: Option<&str>) -> Result<usize, Error> { diesel::update(members::table.find(*discord_id as i64)) .set(members::github.eq(git)) .execute(&db_connection()) } -pub fn set_member_photo(discord_id: &u64, url: &str) -> Result<usize, Error> { +pub fn set_member_photo(discord_id: &u64, url: Option<&str>) -> Result<usize, Error> { diesel::update(members::table.find(*discord_id as i64)) .set(members::photo.eq(url)) .execute(&db_connection()) } -pub fn set_member_website(discord_id: &u64, url: &str) -> Result<usize, Error> { +pub fn set_member_website(discord_id: &u64, url: Option<&str>) -> Result<usize, Error> { diesel::update(members::table.find(*discord_id as i64)) .set(members::website.eq(url)) .execute(&db_connection()) } + +pub fn set_member_study(discord_id: &u64, study: Option<&str>) -> Result<usize, Error> { + diesel::update(members::table.find(*discord_id as i64)) + .set(members::study.eq(study)) + .execute(&db_connection()) +} diff --git a/src/user_management.rs b/src/user_management.rs index 57470053c3620ca5e611976c24b294c6023e1e66..692376c158a2bc2cc9d706dfe68c73734a6946c9 100644 --- a/src/user_management.rs +++ b/src/user_management.rs @@ -80,9 +80,7 @@ impl Commands { ); return; } - if RESERVED_NAMES.contains(&account_name) - || database::username_exists(account_name) - { + if RESERVED_NAMES.contains(&account_name) || database::username_exists(account_name) { send_message!( msg.channel_id, &ctx.http, @@ -233,6 +231,9 @@ impl Commands { if let Some(bio) = member.biography.clone() { embed.field("Bio", bio, false); } + if let Some(study) = member.study.clone() { + embed.field("Area of study", study, false); + } if let Some(git) = member.github.clone() { embed.field("Git", git, false); } @@ -249,14 +250,28 @@ impl Commands { } pub fn set_info(ctx: Context, msg: Message, info: &str) { if info.trim().is_empty() { - send_message!( - msg.channel_id, - &ctx.http, - format!( - "Usage: {}set <bio|git|web|photo> <value>", - CONFIG.command_prefix - ) - ); + msg.channel_id + .send_message(&ctx.http, |m| { + m.embed(|embed| { + embed.colour(serenity::utils::Colour::LIGHT_GREY); + embed.title("Usage"); + embed.description( + format!( + "`{}set <field> <info>` or `{}clear <field>`", + CONFIG.command_prefix, + CONFIG.command_prefix, + ) + ); + embed.field("Biography", format!("`{}set bio <info>`\nBe friendly! Provide a little introduction to yourself.", CONFIG.command_prefix), false); + embed.field("Git", format!("`{}set git <url>`\nA link to your git forge profile. Also takes a github username for convinience", CONFIG.command_prefix), false); + embed.field("Photo", format!("`{}set photo <url>`\nPut a face to a name! Provide a profile photo.", CONFIG.command_prefix), false); + embed.field("Website", format!("`{}set web <info>`\nGot a personal website? Share it here :)", CONFIG.command_prefix), false); + embed.field("Studying", format!("`{}set study <info>`\nYou're (probably) a Uni student, what's your major?", CONFIG.command_prefix), false); + embed + }); + m + }) + .expect("Failed to send usage help embed"); return; } let info_content: Vec<_> = info.splitn(2, ' ').collect(); @@ -265,22 +280,42 @@ impl Commands { if info_content.len() == 1 || !vec!["bio", "git", "web", "photo"].contains(&property.as_str()) { - send_message!( - msg.channel_id, - &ctx.http, - format!( - "Usage: {}set {} {}", - CONFIG.command_prefix, - property, - match property.as_str() { - "bio" => "some information about yourself :)", - "git" => "a url to your git{hub,lab} account", - "photo" => "a url to a profile photo online", - "web" => "a url to your website/webpage", - _ => "whatever you want, because this does absolutely nothing. Try !set to see what you can do" - } - ) - ); + msg.channel_id + .send_message(&ctx.http, |m| { + m.embed(|embed| { + embed.colour(serenity::utils::Colour::LIGHT_GREY); + embed.title("Usage"); + embed.field( + match property.as_str() { + "bio" => "Biography", + "git" => "Git Forge Profile", + "photo" => "Profile Photo", + "web" => "Personal Website", + "study" => "Area of study", + _ => "???", + }, + format!( + "`{}set {} <info>` or `{}clear {}`\n{}", + CONFIG.command_prefix, + property, + CONFIG.command_prefix, + property, + match property.as_str() { + "bio" => "Some information about yourself :)", + "git" => "A url to your git{hub,lab} account", + "photo" => "A url to a profile photo online", + "web" => "A url to your website/webpage", + "web" => "Your degree title", + _ => "Whatever you want, because this does absolutely nothing.", + } + ), + false, + ); + embed + }); + m + }) + .expect("Failed to send usage embed"); return; } let mut value = info_content[1].to_string(); @@ -314,32 +349,37 @@ impl Commands { return }); let set_property = match property.as_str() { - "bio" => database::set_member_bio(&msg.author.id.0, &value), - "git" => database::set_member_git(&msg.author.id.0, &value), - "photo" => database::set_member_photo(&msg.author.id.0, &value), - "web" => database::set_member_website(&msg.author.id.0, &value), + "bio" => database::set_member_bio(&msg.author.id.0, Some(&value)), + "git" => database::set_member_git(&msg.author.id.0, Some(&value)), + "photo" => database::set_member_photo(&msg.author.id.0, Some(&value)), + "web" => database::set_member_website(&msg.author.id.0, Some(&value)), + "study" => database::set_member_study(&msg.author.id.0, Some(&value)), _ => Err(diesel::result::Error::NotFound), }; match set_property { - Ok(_) => if property == "git" && member.photo == None { - let git_url = Url::parse(&value).unwrap(); // we parsed this earlier and it was fine - match git_url.host_str() { - Some("github.com") => { - if let Some(mut path_segments) = git_url.path_segments() { - database::set_member_photo( - &msg.author.id.0, - format!( - "https://github.com/{}.png", - path_segments.next().expect("URL doesn't have a path") + Ok(_) => { + if property == "git" && member.photo == None { + let git_url = Url::parse(&value).unwrap(); // we parsed this earlier and it was fine + match git_url.host_str() { + Some("github.com") => { + if let Some(mut path_segments) = git_url.path_segments() { + database::set_member_photo( + &msg.author.id.0, + Some( + format!( + "https://github.com/{}.png", + path_segments.next().expect("URL doesn't have a path") + ) + .as_str(), + ), ) - .as_str(), - ) - .expect("Attempt to set member photo failed"); - } else { - info!("Git path added (2), {}", git_url.path()); + .expect("Attempt to set member photo failed"); + } else { + info!("Git path added (2), {}", git_url.path()); + } } + _ => info!("Git path added, {}", git_url.path()), } - _ => info!("Git path added, {}", git_url.path()), } } Err(why) => { @@ -354,4 +394,19 @@ impl Commands { error!("Error deleting set profile property: {:?}", why); } } + pub fn clear_info(ctx: Context, msg: Message, field: &str) { + if field.trim().is_empty() { + // just show the help page from set_info + Commands::set_info(ctx, msg, ""); + return; + } + let clear_property = match field { + "bio" => database::set_member_bio(&msg.author.id.0, None), + "git" => database::set_member_git(&msg.author.id.0, None), + "photo" => database::set_member_photo(&msg.author.id.0, None), + "web" => database::set_member_website(&msg.author.id.0, None), + "study" => database::set_member_study(&msg.author.id.0, None), + _ => Err(diesel::result::Error::NotFound), + }; + } } diff --git a/state.db b/state.db index d8718d3308eb20f66a68774aa979719572eae66a..535092aef9158b9a4c2a50b96bc543d6003adf27 100644 --- a/state.db +++ b/state.db @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e6642eb54f8c63bfd31fa28b105e359ae5d023f04d0e990aa452afc6d163e316 +oid sha256:c2a6d39d270b0e6bb8f801bd6d1840efd962491b51f9b637957089ca1092cde8 size 32768