Refactors for less nesting, slight performance and memory improvement for count command

This commit is contained in:
Roman Moisieiev 2025-09-11 13:39:10 +01:00
parent 6353d77360
commit f3f08962a4
9 changed files with 161 additions and 287 deletions

View file

@ -54,15 +54,14 @@ impl EventHandler for Handler {
// committee server takes priority
let committee_server = config_global.committee_server;
if new_member.guild_id.get() == committee_server.get() {
if new_member.guild_id == committee_server {
let mut member = vec![new_member.clone()];
update_committees(&db, &ctx, &config_global, &mut member).await;
return;
}
let config_server = match get_server_config(&db, &new_member.guild_id).await {
None => return,
Some(x) => x,
let Some(config_server) = get_server_config(&db, &new_member.guild_id).await else {
return;
};
if get_server_member(&db, &new_member.guild_id, &new_member).await.is_ok() {
@ -81,26 +80,26 @@ impl EventHandler for Handler {
if let Err(e) = new_member.add_roles(&ctx, &roles).await {
println!("{e:?}");
}
} else {
let tmp = get_committee(&db, config_server.wolves_id).await;
if !tmp.is_empty() {
let committee = &tmp[0];
let msg = format!(
r#"
return;
}
let tmp = get_committee(&db, config_server.wolves_id).await;
let Some(committee) = tmp.first() else {
return;
};
let msg = format!(
r#"
Welcome {} to the {} server!
Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use ``/wolves link email_here`` with the email associated with your wolves account, to get full access.
"#,
new_member.display_name(),
committee.name_full,
committee.link,
&config_server.server,
&config_server.bot_channel_id
);
new_member.display_name(),
committee.name_full,
committee.link,
&config_server.server,
&config_server.bot_channel_id
);
if let Err(err) = new_member.user.direct_message(&ctx, CreateMessage::new().content(&msg)).await {
dbg!(err);
}
}
if let Err(err) = new_member.user.direct_message(&ctx, CreateMessage::new().content(&msg)).await {
dbg!(err);
}
}
@ -113,9 +112,8 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
};
// check if the role changed is part of the ones for this server
if let Some(x) = new_data {
on_role_change(&db, &ctx, x).await;
}
let Some(x) = new_data else { return };
on_role_change(&db, &ctx, x).await;
}
async fn ready(&self, ctx: Context, ready: Ready) {
@ -128,7 +126,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
};
let config = config_lock.read().await;
match Command::set_global_commands(
if let Err(e) = Command::set_global_commands(
&ctx.http,
vec![
commands::wolves::register(),
@ -140,22 +138,16 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
)
.await
{
Ok(_) => {}
Err(e) => {
println!("{e:?}")
}
println!("{e:?}")
}
// Inter-Committee server
match config.committee_server.set_commands(&ctx.http, vec![commands::count::committee::register()]).await {
Ok(_) => {}
Err(e) => {
println!("{e:?}")
}
if let Err(e) = config.committee_server.set_commands(&ctx.http, vec![commands::count::committee::register()]).await {
println!("{e:?}")
}
// CompSoc Server
match config
if let Err(e) = config
.compsoc_server
.set_commands(
&ctx.http,
@ -167,16 +159,13 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
)
.await
{
Ok(_) => {}
Err(e) => {
println!("{e:?}")
}
println!("{e:?}")
}
}
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
if let Interaction::Command(command) = interaction {
let _ = command.defer_ephemeral(&ctx.http).await;
_ = command.defer_ephemeral(&ctx.http).await;
// println!("Received command interaction: {:#?}", command);
let content = match command.data.name.as_str() {
@ -190,7 +179,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
"link_minecraft" => commands::minecraft::user::add::run(&command, &ctx).await,
"docs" => commands::wolves::link_docs::users::run(&command, &ctx).await,
// "link" => commands::count::servers::run(&command, &ctx).await,
&_ => format!("not implemented :( wolves {}", x.name.as_str()),
_ => format!("not implemented :( wolves {}", x.name.as_str()),
},
},
@ -205,7 +194,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
None => "error".to_string(),
Some(z) => match z.name.as_str() {
"change" => commands::server_icon::admin::change::run(&command, &ctx).await,
&_ => format!("not implemented :( count {}", x.name.as_str()),
_ => format!("not implemented :( count {}", x.name.as_str()),
},
},
_ => {
@ -214,7 +203,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
},
// TODO: move teh minecraft commands in here as a subgroup
// "link" => commands::count::servers::run(&command, &ctx).await,
&_ => format!("not implemented :( committee {}", x.name.as_str()),
_ => format!("not implemented :( committee {}", x.name.as_str()),
},
},
"minecraft_add" => commands::minecraft::server::add::run(&command, &ctx).await,
@ -226,7 +215,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
Some(x) => match x.name.as_str() {
"committee" => commands::count::committee::run(&command, &ctx).await,
"servers" => commands::count::servers::run(&command, &ctx).await,
&_ => format!("not implemented :( count {}", x.name.as_str()),
_ => format!("not implemented :( count {}", x.name.as_str()),
},
},
@ -240,16 +229,16 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
Some(z) => match z.name.as_str() {
"icon" => commands::server_icon::user::current::icon::run(&command, &ctx).await,
"festival" => commands::server_icon::user::current::festival::run(&command, &ctx).await,
&_ => format!("not implemented :( count {}", x.name.as_str()),
_ => format!("not implemented :( count {}", x.name.as_str()),
},
},
&_ => format!("not implemented :( {}", command.data.name.as_str()),
_ => format!("not implemented :( {}", command.data.name.as_str()),
};
result
}
"stats" => commands::server_icon::user::stats::run(&command, &ctx).await,
&_ => format!("not implemented :( count {}", x.name.as_str()),
_ => format!("not implemented :( count {}", x.name.as_str()),
},
},
_ => format!("not implemented :( {}", command.data.name.as_str()),