discord-bot/src/commands/add_server.rs
Brendan Golden d0726169ee
All checks were successful
/ check_lfs (push) Successful in 16s
/ check_lfs (pull_request) Successful in 12s
clippy: changes from nightly clippy
all of this is embeding teh var into teh format macro
2025-07-21 00:50:20 +01:00

143 lines
3.9 KiB
Rust

use serenity::{
all::{CommandDataOption, CommandDataOptionValue, CommandInteraction},
client::Context,
};
use skynet_discord_bot::common::{
database::{get_server_config, DataBase, Servers},
set_roles::normal::update_server,
wolves::cns::get_wolves,
};
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),
..
}) = command.data.options.first()
{
options
} 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 {
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 {
return "Please provide a valid role for ``Role Current``".to_string();
};
let role_past = if let Some(x) = sub_options.get(5) {
match &x.value {
CommandDataOptionValue::Role(role) => Some(role.to_owned()),
_ => None,
}
} else {
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 {
return "Please provide a valid channel for ``Bot Channel``".to_string();
};
let db_lock = {
let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
};
let db = db_lock.read().await;
let server_data = Servers {
server: command.guild_id.unwrap_or_default(),
wolves_api,
wolves_id: 0,
role_past,
role_current,
member_past: 0,
member_current: 0,
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:?}");
}
}
"Added/Updated server info".to_string()
}
async fn add_server(db: &Pool<Sqlite>, ctx: &Context, server: &Servers) -> Result<Option<Servers>, Error> {
let existing = get_server_config(db, &server.server).await;
let role_past = server.role_past.map(|x| x.get() as i64);
let insert = sqlx::query_as::<_, Servers>(
"
INSERT OR REPLACE INTO servers (server, wolves_api, role_past, role_current, bot_channel_id)
VALUES (?1, ?2, ?3, ?4, ?5)
",
)
.bind(server.server.get() as i64)
.bind(&server.wolves_api)
.bind(role_past)
.bind(server.role_current.get() as i64)
.bind(server.bot_channel_id.get() as i64)
.fetch_optional(db)
.await;
// if the entry does not exist already tehn do a user update
let (update, current_remove, current_role, past_remove, past_role) = match &existing {
None => (true, false, None, false, None),
Some(x) => {
let mut result = (false, false, None, false, None);
if x.wolves_api != server.wolves_api {
result.0 = true;
}
if x.role_current != server.role_current {
result.0 = true;
result.1 = true;
result.2 = Some(x.role_current);
}
if x.role_past != server.role_past {
result.0 = true;
result.3 = true;
result.4 = x.role_past;
}
result
}
};
// update all users
if update {
// handle wolves api here
get_wolves(ctx).await;
let mut roles_remove = vec![];
if current_remove {
roles_remove.push(current_role)
}
if past_remove {
roles_remove.push(past_role)
}
update_server(ctx, server, &roles_remove, &[]).await;
}
insert
}