Commit 9b26653c authored by spookydonut's avatar spookydonut

add expired member support

parent bc3a4f18
Pipeline #208 failed with stages
......@@ -24,6 +24,7 @@ pub struct UccbotConfig {
pub tiebreaker_role: u64,
pub unregistered_member_role: u64,
pub registered_member_role: u64,
pub expired_member_role: u64,
pub command_prefix: String,
pub for_vote: String,
pub against_vote: String,
......
......@@ -13,6 +13,7 @@ vote_role: 607478818038480937
tiebreaker_role: 607509283483025409
unregistered_member_role: 608282247350714408
registered_member_role: 608282133118582815
expired_member_role: 0
command_prefix: "!"
for_vote: "👍"
......
......@@ -6,6 +6,7 @@ pub struct LDAPUser {
pub username: String,
pub name: String,
pub when_created: String,
pub login_shell: String,
}
pub fn ldap_search(username: &str) -> Option<LDAPUser> {
......@@ -24,7 +25,7 @@ pub fn ldap_search(username: &str) -> Option<LDAPUser> {
"cn=Users,dc=ad,dc=ucc,dc=gu,dc=uwa,dc=edu,dc=au",
Scope::Subtree,
&format!("(cn={})", username),
vec!["when_created", "displayName", "name"],
vec!["when_created", "displayName", "name", "loginShell"],
)
.expect("LDAP error")
.success()
......@@ -42,10 +43,14 @@ pub fn ldap_search(username: &str) -> Option<LDAPUser> {
.get("displayName")
.expect("LDAP failed to get 'displayName' field")
.join(""),
when_created: "".to_string() // result
when_created: "".to_string(), // result
// .get("whenCreated")
// .expect("LDAP failed to get 'whenCreated' field")
// .join(""),
login_shell: result
.get("loginShell")
.expect("LDAP failed to get 'loginShell' field")
.join(""),
})
}
......
......@@ -95,7 +95,8 @@ impl EventHandler for Handler {
`!poll <text>` to get people's opinions on something", false);
embed.field("Account", "`!register <ucc username>` to link your Discord and UCC account\n\
`!profile <user>` to get the profile of a user\n\
`!set <bio|git|web|photo>` to set that property of _your_ profile", false);
`!set <bio|git|web|photo>` to set that property of _your_ profile\n\
`!updateroles` to update your registered roles", false);
embed.field("Fun", "`!cowsay <text>` to have a cow say your words\n\
with no `<text>` it'll give you a fortune 😉", false);
embed
......@@ -117,6 +118,7 @@ impl EventHandler for Handler {
&ctx.http,
format!("{:?}", ldap::tla_search(message_content[1]))
),
"updateroles" => user_management::Commands::update_registered_role(ctx, msg),
_ => send_message!(
msg.channel_id,
&ctx.http,
......
use rand::seq::SliceRandom;
use regex::Regex;
use serenity::{
model::{channel::Message, guild::Member},
model::{channel::Message, guild::Member, id::RoleId},
prelude::*,
utils::MessageBuilder,
};
......@@ -10,7 +10,7 @@ use url::Url;
use crate::config::CONFIG;
use crate::database;
use crate::ldap::ldap_exists;
use crate::ldap::{ldap_exists, ldap_search};
use crate::token_management::*;
pub fn new_member(ctx: &Context, mut new_member: Member) {
......@@ -132,6 +132,55 @@ impl Commands {
Err(why) => error!("Unable to send message with mutt {:?}", why),
};
}
pub fn get_registered_role(name: String) -> Option<u64> {
guard!(let Some(result) = ldap_search(&name) else {
return None
});
if result.login_shell.contains("locked")
&& CONFIG.expired_member_role > 0 {
return Some(CONFIG.expired_member_role)
}
Some(CONFIG.registered_member_role)
}
// TODO: make this return a result
// NOTE: don't make this directly send messages, so it can be used for mass updates
pub fn update_registered_role(ctx: Context, msg: Message) {
guard!(let Ok(member_info) = database::get_member_info(&msg.author.id.0) else {
return // Err()
});
guard!(let Some(registered_role) = Commands::get_registered_role(member_info.username) else {
return // Err()
});
guard!(let Ok(mut discord_member) = serenity::model::id::GuildId(CONFIG.server_id)
.member(ctx.http.clone(), msg.author.id) else {
return // Err()
});
let roles_to_remove = vec![
CONFIG.registered_member_role,
CONFIG.unregistered_member_role,
CONFIG.expired_member_role];
for role in roles_to_remove {
if role == registered_role { // remove when vec.remove_item is stable
continue
}
if discord_member.roles.contains(&RoleId::from(role))
&& discord_member.remove_role(&ctx.http, role).is_err() {
return // Err()
}
}
if !discord_member.roles.contains(&RoleId::from(registered_role))
&& discord_member.add_role(&ctx.http, registered_role).is_err() {
return // Err()
}
// Ok()
}
pub fn verify(ctx: Context, msg: Message, token: &str) {
match parse_token(&msg.author, token) {
Ok(name) => {
......@@ -145,9 +194,13 @@ impl Commands {
"Unable to remove role: {:?}",
member.remove_role(&ctx.http, CONFIG.unregistered_member_role)
);
guard!(let Some(member_role) = Commands::get_registered_role(name) else {
send_message!(msg.channel_id, ctx.http.clone(), "Couldn't find you in LDAP!");
return
});
e!(
"Unable to add role: {:?}",
member.add_role(&ctx.http, CONFIG.registered_member_role)
member.add_role(&ctx.http, member_role)
);
e!(
"Unable to edit nickname: {:?}",
......
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