2025-02-19 00:17:02 +00:00
|
|
|
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommandOption};
|
|
|
|
use serenity::{builder::CreateCommand, client::Context};
|
2024-10-28 21:53:04 +00:00
|
|
|
use skynet_discord_bot::common::database::{get_server_config, DataBase, Servers};
|
|
|
|
use skynet_discord_bot::common::set_roles::normal::update_server;
|
2024-10-28 01:29:01 +00:00
|
|
|
use skynet_discord_bot::common::wolves::cns::get_wolves;
|
2023-09-17 19:30:59 +01:00
|
|
|
use sqlx::{Error, Pool, Sqlite};
|
|
|
|
|
2025-02-19 00:17:02 +00:00
|
|
|
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
|
|
|
let wolves_api = if let Some(CommandDataOption {
|
|
|
|
value: CommandDataOptionValue::String(key),
|
|
|
|
..
|
|
|
|
}) = command.data.options.first()
|
2023-09-17 19:30:59 +01:00
|
|
|
{
|
2025-02-19 00:17:02 +00:00
|
|
|
key.to_string()
|
2023-09-17 19:30:59 +01:00
|
|
|
} else {
|
|
|
|
return "Please provide a wolves API key".to_string();
|
|
|
|
};
|
|
|
|
|
2025-02-19 00:17:02 +00:00
|
|
|
let role_current = if let Some(CommandDataOption {
|
|
|
|
value: CommandDataOptionValue::Role(role),
|
|
|
|
..
|
|
|
|
}) = command.data.options.get(1)
|
2023-09-17 19:30:59 +01:00
|
|
|
{
|
2025-02-19 00:17:02 +00:00
|
|
|
role.to_owned()
|
2023-09-17 19:30:59 +01:00
|
|
|
} else {
|
|
|
|
return "Please provide a valid role for ``Role Current``".to_string();
|
|
|
|
};
|
|
|
|
|
2025-02-19 00:17:02 +00:00
|
|
|
let role_past = if let Some(CommandDataOption {
|
|
|
|
value: CommandDataOptionValue::Role(role),
|
|
|
|
..
|
|
|
|
}) = command.data.options.get(5)
|
|
|
|
{
|
|
|
|
Some(role.to_owned())
|
|
|
|
} else {
|
|
|
|
None
|
2023-09-17 19:30:59 +01:00
|
|
|
};
|
|
|
|
|
2025-02-19 00:17:02 +00:00
|
|
|
let bot_channel_id = if let Some(CommandDataOption {
|
|
|
|
value: CommandDataOptionValue::Channel(channel),
|
|
|
|
..
|
|
|
|
}) = command.data.options.get(2)
|
2024-09-17 21:19:44 +01:00
|
|
|
{
|
2025-02-19 00:17:02 +00:00
|
|
|
channel.to_owned()
|
2024-09-17 21:19:44 +01:00
|
|
|
} else {
|
|
|
|
return "Please provide a valid channel for ``Bot Channel``".to_string();
|
|
|
|
};
|
|
|
|
|
2023-09-17 19:30:59 +01:00
|
|
|
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(),
|
2025-02-19 00:17:02 +00:00
|
|
|
wolves_api,
|
2025-02-24 17:07:26 +00:00
|
|
|
wolves_id: 0,
|
2023-09-17 19:30:59 +01:00
|
|
|
role_past,
|
|
|
|
role_current,
|
|
|
|
member_past: 0,
|
2024-06-03 04:06:47 +01:00
|
|
|
member_current: 0,
|
2024-09-17 21:19:44 +01:00
|
|
|
bot_channel_id,
|
2023-09-17 19:30:59 +01:00
|
|
|
};
|
|
|
|
|
2023-11-23 16:12:28 +00:00
|
|
|
match add_server(&db, ctx, &server_data).await {
|
2023-09-17 19:30:59 +01:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => {
|
|
|
|
println!("{:?}", e);
|
|
|
|
return format!("Failure to insert into Servers {:?}", server_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
"Added/Updated server info".to_string()
|
|
|
|
}
|
|
|
|
|
2025-02-19 00:17:02 +00:00
|
|
|
pub fn register() -> CreateCommand {
|
|
|
|
CreateCommand::new("add")
|
2023-09-17 19:30:59 +01:00
|
|
|
.description("Enable the bot for this discord")
|
2025-02-26 15:55:42 +00:00
|
|
|
.default_member_permissions(serenity::model::Permissions::MANAGE_GUILD)
|
2025-02-19 00:17:02 +00:00
|
|
|
.add_option(CreateCommandOption::new(CommandOptionType::String, "api_key", "UL Wolves API Key").required(true))
|
|
|
|
.add_option(CreateCommandOption::new(CommandOptionType::Role, "role_current", "Role for Current members").required(true))
|
|
|
|
.add_option(CreateCommandOption::new(CommandOptionType::Channel, "bot_channel", "Safe space for folks to use the bot commands.").required(true))
|
|
|
|
.add_option(CreateCommandOption::new(CommandOptionType::Role, "role_past", "Role for Past members").required(false))
|
2023-09-17 19:30:59 +01:00
|
|
|
}
|
|
|
|
|
2023-11-23 16:12:28 +00:00
|
|
|
async fn add_server(db: &Pool<Sqlite>, ctx: &Context, server: &Servers) -> Result<Option<Servers>, Error> {
|
|
|
|
let existing = get_server_config(db, &server.server).await;
|
2025-02-19 00:17:02 +00:00
|
|
|
let role_past = server.role_past.map(|x| x.get() as i64);
|
2023-09-17 19:30:59 +01:00
|
|
|
|
2023-11-23 16:12:28 +00:00
|
|
|
let insert = sqlx::query_as::<_, Servers>(
|
2023-09-17 19:30:59 +01:00
|
|
|
"
|
2025-02-18 21:14:05 +00:00
|
|
|
INSERT OR REPLACE INTO servers (server, wolves_api, role_past, role_current, bot_channel_id)
|
|
|
|
VALUES (?1, ?2, ?3, ?4, ?5)
|
2023-09-17 19:30:59 +01:00
|
|
|
",
|
|
|
|
)
|
2025-02-19 00:17:02 +00:00
|
|
|
.bind(server.server.get() as i64)
|
2023-09-17 19:30:59 +01:00
|
|
|
.bind(&server.wolves_api)
|
|
|
|
.bind(role_past)
|
2025-02-19 00:17:02 +00:00
|
|
|
.bind(server.role_current.get() as i64)
|
|
|
|
.bind(server.bot_channel_id.get() as i64)
|
2023-09-17 19:30:59 +01:00
|
|
|
.fetch_optional(db)
|
2023-11-23 16:12:28 +00:00
|
|
|
.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;
|
2024-09-17 22:08:20 +01:00
|
|
|
result.2 = Some(x.role_current);
|
2023-11-23 16:12:28 +00:00
|
|
|
}
|
|
|
|
if x.role_past != server.role_past {
|
|
|
|
result.0 = true;
|
|
|
|
result.3 = true;
|
|
|
|
result.4 = x.role_past;
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// update all users
|
|
|
|
if update {
|
2023-11-23 21:53:27 +00:00
|
|
|
// handle wolves api here
|
2024-10-28 01:29:01 +00:00
|
|
|
get_wolves(ctx).await;
|
2023-11-23 21:53:27 +00:00
|
|
|
|
2023-11-23 16:12:28 +00:00
|
|
|
let mut roles_remove = vec![];
|
|
|
|
if current_remove {
|
|
|
|
roles_remove.push(current_role)
|
|
|
|
}
|
|
|
|
if past_remove {
|
|
|
|
roles_remove.push(past_role)
|
|
|
|
}
|
2024-05-06 02:12:26 +01:00
|
|
|
update_server(ctx, server, &roles_remove, &[]).await;
|
2023-11-23 16:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
insert
|
2023-09-17 19:30:59 +01:00
|
|
|
}
|