Unverified Commit 8cec4c40 authored by tec's avatar tec
Browse files

Add study field, and nice help messages + clearning fields

parent 2d2f3e3d
...@@ -18,6 +18,7 @@ pub struct Member { ...@@ -18,6 +18,7 @@ pub struct Member {
pub github: Option<String>, pub github: Option<String>,
pub photo: Option<String>, pub photo: Option<String>,
pub website: Option<String>, pub website: Option<String>,
pub study: Option<String>,
} }
table! { table! {
...@@ -31,6 +32,7 @@ table! { ...@@ -31,6 +32,7 @@ table! {
github -> Nullable<Text>, github -> Nullable<Text>,
photo -> Nullable<Text>, photo -> Nullable<Text>,
website -> Nullable<Text>, website -> Nullable<Text>,
study -> Nullable<Text>,
} }
} }
...@@ -94,26 +96,32 @@ pub fn get_member_info_from_tla(tla: &str) -> Result<Member, Error> { ...@@ -94,26 +96,32 @@ pub fn get_member_info_from_tla(tla: &str) -> Result<Member, Error> {
.first(&db_connection()) .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)) diesel::update(members::table.find(*discord_id as i64))
.set(members::biography.eq(bio)) .set(members::biography.eq(bio))
.execute(&db_connection()) .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)) diesel::update(members::table.find(*discord_id as i64))
.set(members::github.eq(git)) .set(members::github.eq(git))
.execute(&db_connection()) .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)) diesel::update(members::table.find(*discord_id as i64))
.set(members::photo.eq(url)) .set(members::photo.eq(url))
.execute(&db_connection()) .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)) diesel::update(members::table.find(*discord_id as i64))
.set(members::website.eq(url)) .set(members::website.eq(url))
.execute(&db_connection()) .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())
}
...@@ -80,9 +80,7 @@ impl Commands { ...@@ -80,9 +80,7 @@ impl Commands {
); );
return; return;
} }
if RESERVED_NAMES.contains(&account_name) if RESERVED_NAMES.contains(&account_name) || database::username_exists(account_name) {
|| database::username_exists(account_name)
{
send_message!( send_message!(
msg.channel_id, msg.channel_id,
&ctx.http, &ctx.http,
...@@ -233,6 +231,9 @@ impl Commands { ...@@ -233,6 +231,9 @@ impl Commands {
if let Some(bio) = member.biography.clone() { if let Some(bio) = member.biography.clone() {
embed.field("Bio", bio, false); 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() { if let Some(git) = member.github.clone() {
embed.field("Git", git, false); embed.field("Git", git, false);
} }
...@@ -249,14 +250,28 @@ impl Commands { ...@@ -249,14 +250,28 @@ impl Commands {
} }
pub fn set_info(ctx: Context, msg: Message, info: &str) { pub fn set_info(ctx: Context, msg: Message, info: &str) {
if info.trim().is_empty() { if info.trim().is_empty() {
send_message!( msg.channel_id
msg.channel_id, .send_message(&ctx.http, |m| {
&ctx.http, m.embed(|embed| {
embed.colour(serenity::utils::Colour::LIGHT_GREY);
embed.title("Usage");
embed.description(
format!( format!(
"Usage: {}set <bio|git|web|photo> <value>", "`{}set <field> <info>` or `{}clear <field>`",
CONFIG.command_prefix 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; return;
} }
let info_content: Vec<_> = info.splitn(2, ' ').collect(); let info_content: Vec<_> = info.splitn(2, ' ').collect();
...@@ -265,22 +280,42 @@ impl Commands { ...@@ -265,22 +280,42 @@ impl Commands {
if info_content.len() == 1 if info_content.len() == 1
|| !vec!["bio", "git", "web", "photo"].contains(&property.as_str()) || !vec!["bio", "git", "web", "photo"].contains(&property.as_str())
{ {
send_message!( msg.channel_id
msg.channel_id, .send_message(&ctx.http, |m| {
&ctx.http, 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!( format!(
"Usage: {}set {} {}", "`{}set {} <info>` or `{}clear {}`\n{}",
CONFIG.command_prefix,
property,
CONFIG.command_prefix, CONFIG.command_prefix,
property, property,
match property.as_str() { match property.as_str() {
"bio" => "some information about yourself :)", "bio" => "Some information about yourself :)",
"git" => "a url to your git{hub,lab} account", "git" => "A url to your git{hub,lab} account",
"photo" => "a url to a profile photo online", "photo" => "A url to a profile photo online",
"web" => "a url to your website/webpage", "web" => "A url to your website/webpage",
_ => "whatever you want, because this does absolutely nothing. Try !set to see what you can do" "web" => "Your degree title",
_ => "Whatever you want, because this does absolutely nothing.",
} }
) ),
false,
); );
embed
});
m
})
.expect("Failed to send usage embed");
return; return;
} }
let mut value = info_content[1].to_string(); let mut value = info_content[1].to_string();
...@@ -314,25 +349,29 @@ impl Commands { ...@@ -314,25 +349,29 @@ impl Commands {
return return
}); });
let set_property = match property.as_str() { let set_property = match property.as_str() {
"bio" => database::set_member_bio(&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, &value), "git" => database::set_member_git(&msg.author.id.0, Some(&value)),
"photo" => database::set_member_photo(&msg.author.id.0, &value), "photo" => database::set_member_photo(&msg.author.id.0, Some(&value)),
"web" => database::set_member_website(&msg.author.id.0, &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), _ => Err(diesel::result::Error::NotFound),
}; };
match set_property { match set_property {
Ok(_) => if property == "git" && member.photo == None { Ok(_) => {
if property == "git" && member.photo == None {
let git_url = Url::parse(&value).unwrap(); // we parsed this earlier and it was fine let git_url = Url::parse(&value).unwrap(); // we parsed this earlier and it was fine
match git_url.host_str() { match git_url.host_str() {
Some("github.com") => { Some("github.com") => {
if let Some(mut path_segments) = git_url.path_segments() { if let Some(mut path_segments) = git_url.path_segments() {
database::set_member_photo( database::set_member_photo(
&msg.author.id.0, &msg.author.id.0,
Some(
format!( format!(
"https://github.com/{}.png", "https://github.com/{}.png",
path_segments.next().expect("URL doesn't have a path") path_segments.next().expect("URL doesn't have a path")
) )
.as_str(), .as_str(),
),
) )
.expect("Attempt to set member photo failed"); .expect("Attempt to set member photo failed");
} else { } else {
...@@ -342,6 +381,7 @@ impl Commands { ...@@ -342,6 +381,7 @@ impl Commands {
_ => info!("Git path added, {}", git_url.path()), _ => info!("Git path added, {}", git_url.path()),
} }
} }
}
Err(why) => { Err(why) => {
error!( error!(
"Umable to set property {} to {} in DB {:?}", "Umable to set property {} to {} in DB {:?}",
...@@ -354,4 +394,19 @@ impl Commands { ...@@ -354,4 +394,19 @@ impl Commands {
error!("Error deleting set profile property: {:?}", why); 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),
};
}
} }
No preview for this file type
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment