Refactors for less nesting, slight performance and memory improvement for count command
This commit is contained in:
parent
6353d77360
commit
f3f08962a4
9 changed files with 161 additions and 287 deletions
|
@ -10,31 +10,19 @@ use skynet_discord_bot::common::{
|
|||
use sqlx::{Error, Pool, Sqlite};
|
||||
|
||||
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
||||
let sub_options = if let Some(CommandDataOption {
|
||||
value: CommandDataOptionValue::SubCommand(options),
|
||||
let Some(CommandDataOption {
|
||||
value: CommandDataOptionValue::SubCommand(sub_options),
|
||||
..
|
||||
}) = command.data.options.first()
|
||||
{
|
||||
options
|
||||
} else {
|
||||
else {
|
||||
return "Please provide sub options".to_string();
|
||||
};
|
||||
|
||||
let wolves_api = if let Some(x) = sub_options.first() {
|
||||
match &x.value {
|
||||
CommandDataOptionValue::String(key) => key.to_string(),
|
||||
_ => return "Please provide a wolves API key".to_string(),
|
||||
}
|
||||
} else {
|
||||
let Some(CommandDataOptionValue::String(wolves_api)) = sub_options.first().map(|opt| &opt.value) else {
|
||||
return "Please provide a wolves API key".to_string();
|
||||
};
|
||||
|
||||
let role_current = if let Some(x) = sub_options.get(1) {
|
||||
match &x.value {
|
||||
CommandDataOptionValue::Role(role) => role.to_owned(),
|
||||
_ => return "Please provide a valid role for ``Role Current``".to_string(),
|
||||
}
|
||||
} else {
|
||||
let Some(&CommandDataOptionValue::Role(role_current)) = sub_options.get(1).map(|opt| &opt.value) else {
|
||||
return "Please provide a valid role for ``Role Current``".to_string();
|
||||
};
|
||||
|
||||
|
@ -47,12 +35,7 @@ pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
|||
None
|
||||
};
|
||||
|
||||
let bot_channel_id = if let Some(x) = sub_options.get(2) {
|
||||
match &x.value {
|
||||
CommandDataOptionValue::Channel(channel) => channel.to_owned(),
|
||||
_ => return "Please provide a valid channel for ``Bot Channel``".to_string(),
|
||||
}
|
||||
} else {
|
||||
let Some(&CommandDataOptionValue::Channel(bot_channel_id)) = sub_options.get(2).map(|opt| &opt.value) else {
|
||||
return "Please provide a valid channel for ``Bot Channel``".to_string();
|
||||
};
|
||||
|
||||
|
@ -63,7 +46,7 @@ pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
|||
|
||||
let server_data = Servers {
|
||||
server: command.guild_id.unwrap_or_default(),
|
||||
wolves_api,
|
||||
wolves_api: wolves_api.to_string(),
|
||||
wolves_id: 0,
|
||||
role_past,
|
||||
role_current,
|
||||
|
@ -72,12 +55,9 @@ pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
|||
bot_channel_id,
|
||||
};
|
||||
|
||||
match add_server(&db, ctx, &server_data).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("{e:?}");
|
||||
return format!("Failure to insert into Servers {server_data:?}");
|
||||
}
|
||||
if let Err(e) = add_server(&db, ctx, &server_data).await {
|
||||
println!("{e:?}");
|
||||
return format!("Failure to insert into Servers {server_data:?}");
|
||||
}
|
||||
|
||||
"Added/Updated server info".to_string()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue