forked from Skynet/discord-bot
172 lines
No EOL
4.6 KiB
Rust
172 lines
No EOL
4.6 KiB
Rust
use serenity::{
|
|
builder::CreateApplicationCommand,
|
|
client::Context,
|
|
model::{
|
|
application::interaction::application_command::ApplicationCommandInteraction,
|
|
prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue},
|
|
},
|
|
};
|
|
|
|
use skynet_discord_bot::{is_admin, DataBase, RoleAdder};
|
|
use sqlx::{Error, Pool, Sqlite};
|
|
|
|
pub mod edit {
|
|
use super::*;
|
|
|
|
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
|
|
// check if user has high enough permisssions
|
|
if let Some(msg) = is_admin(command, ctx).await {
|
|
return msg;
|
|
}
|
|
|
|
let role_a = if let CommandDataOptionValue::Role(role) = command
|
|
.data
|
|
.options
|
|
.get(0)
|
|
.expect("Expected role option")
|
|
.resolved
|
|
.as_ref()
|
|
.expect("Expected role object")
|
|
{
|
|
role.id.to_owned()
|
|
} else {
|
|
return "Please provide a valid role for ``Role Current``".to_string();
|
|
};
|
|
|
|
let role_b = if let CommandDataOptionValue::Role(role) = command
|
|
.data
|
|
.options
|
|
.get(1)
|
|
.expect("Expected role option")
|
|
.resolved
|
|
.as_ref()
|
|
.expect("Expected role object")
|
|
{
|
|
role.id.to_owned()
|
|
} else {
|
|
return "Please provide a valid role for ``Role Current``".to_string();
|
|
};
|
|
|
|
let role_c = if let CommandDataOptionValue::Role(role) = command
|
|
.data
|
|
.options
|
|
.get(2)
|
|
.expect("Expected role option")
|
|
.resolved
|
|
.as_ref()
|
|
.expect("Expected role object")
|
|
{
|
|
role.id.to_owned()
|
|
} else {
|
|
return "Please provide a valid role for ``Role Current``".to_string();
|
|
};
|
|
|
|
let delete = if let CommandDataOptionValue::Boolean(delete) = command
|
|
.data
|
|
.options
|
|
.get(3)
|
|
.expect("Expected true/false option")
|
|
.resolved
|
|
.as_ref()
|
|
.expect("Expected true/False object")
|
|
{
|
|
delete.to_owned()
|
|
} else {
|
|
false
|
|
};
|
|
|
|
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 = RoleAdder {
|
|
server: command.guild_id.unwrap_or_default(),
|
|
role_a,
|
|
role_b,
|
|
role_c,
|
|
};
|
|
|
|
match add_server(&db, &server_data, delete).await {
|
|
Ok(_) => {}
|
|
Err(e) => {
|
|
println!("{:?}", e);
|
|
return format!("Failure to insert into Servers {:?}", server_data);
|
|
}
|
|
}
|
|
|
|
"Added/Updated server info".to_string()
|
|
}
|
|
|
|
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
|
command
|
|
.name("roles_adder")
|
|
.description("Combine roles together to an new one")
|
|
.create_option(|option| {
|
|
option
|
|
.name("role_a")
|
|
.description("A role you want to add to Role B")
|
|
.kind(CommandOptionType::Role)
|
|
.required(true)
|
|
})
|
|
.create_option(|option| {
|
|
option
|
|
.name("role_b")
|
|
.description("A role you want to add to Role A")
|
|
.kind(CommandOptionType::Role)
|
|
.required(true)
|
|
})
|
|
.create_option(|option| {
|
|
option
|
|
.name("role_c")
|
|
.description("Sum of A and B")
|
|
.kind(CommandOptionType::Role)
|
|
.required(true)
|
|
})
|
|
.create_option(|option| {
|
|
option
|
|
.name("delete")
|
|
.description("Delete this entry.")
|
|
.kind(CommandOptionType::Boolean)
|
|
.required(false)
|
|
})
|
|
}
|
|
|
|
async fn add_server(db: &Pool<Sqlite>, server: &RoleAdder, delete: bool) -> Result<Option<RoleAdder>, Error> {
|
|
let action;
|
|
if delete {
|
|
action = sqlx::query_as::<_, RoleAdder>(
|
|
"
|
|
DELETE FROM roles_adder
|
|
WHERE server = ?1 AND role_a = ?2 AND role_b = ?3 AND role_c = ?4
|
|
",
|
|
)
|
|
.bind(*server.server.as_u64() as i64)
|
|
.bind(*server.role_a.as_u64() as i64)
|
|
.bind(*server.role_b.as_u64() as i64)
|
|
.bind(*server.role_c.as_u64() as i64)
|
|
.fetch_optional(db)
|
|
.await;
|
|
} else {
|
|
action = sqlx::query_as::<_, RoleAdder>(
|
|
"
|
|
INSERT OR REPLACE INTO roles_adder (server, role_a, role_b, role_c)
|
|
VALUES (?1, ?2, ?3, ?4)
|
|
",
|
|
)
|
|
.bind(*server.server.as_u64() as i64)
|
|
.bind(*server.role_a.as_u64() as i64)
|
|
.bind(*server.role_b.as_u64() as i64)
|
|
.bind(*server.role_c.as_u64() as i64)
|
|
.fetch_optional(db)
|
|
.await;
|
|
}
|
|
|
|
action
|
|
}
|
|
|
|
}
|
|
|
|
// TODO
|
|
pub mod list {} |