Slight refactoring to reduce indentation
This commit is contained in:
parent
9f713a6463
commit
a4ddf32b73
43
src/main.rs
43
src/main.rs
@ -18,27 +18,28 @@ impl EventHandler for Handler {
|
||||
old: Option<VoiceState>,
|
||||
new: VoiceState,
|
||||
) {
|
||||
if let Some(gid) = guild_id {
|
||||
if let Some(rid) = ROLES_BY_SERVER.get(&gid) {
|
||||
if let Ok(guild) = PartialGuild::get(&ctx.http, gid) {
|
||||
let mut roles = guild
|
||||
.member(&ctx.http, new.user_id)
|
||||
.expect("could not retrieve member")
|
||||
.roles;
|
||||
// user just joined the VC -> add the role
|
||||
if old.is_none() {
|
||||
roles.push(*rid);
|
||||
// user is no longer in VC -> remove the role
|
||||
} else if new.channel_id.is_none() {
|
||||
roles.retain(|r| r != rid);
|
||||
}
|
||||
if let Err(e) = guild.edit_member(&ctx.http, new.user_id, |m| m.roles(roles)) {
|
||||
println!(
|
||||
"Could not edit member {} of guild {}, {}",
|
||||
new.user_id, gid, e
|
||||
);
|
||||
}
|
||||
}
|
||||
if guild_id.is_none() || !ROLES_BY_SERVER.contains_key(&guild_id.unwrap()) {
|
||||
return;
|
||||
}
|
||||
let gid = guild_id.unwrap();
|
||||
let rid = ROLES_BY_SERVER.get(&gid).unwrap();
|
||||
if let Ok(guild) = PartialGuild::get(&ctx.http, gid) {
|
||||
let mut roles = guild
|
||||
.member(&ctx.http, new.user_id)
|
||||
.expect("could not retrieve member")
|
||||
.roles;
|
||||
// user just joined the VC -> add the role
|
||||
if old.is_none() {
|
||||
roles.push(*rid);
|
||||
// user is no longer in VC -> remove the role
|
||||
} else if new.channel_id.is_none() {
|
||||
roles.retain(|r| r != rid);
|
||||
}
|
||||
if let Err(e) = guild.edit_member(&ctx.http, new.user_id, |m| m.roles(roles)) {
|
||||
println!(
|
||||
"Could not edit member {} of guild {}, {}",
|
||||
new.user_id, gid, e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user