feat: bumped serenity to the latest version

Lots of changes to how it runs
This commit is contained in:
silver 2025-02-19 00:17:02 +00:00
parent a8c1cc9cf1
commit 6b84f33d2e
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
12 changed files with 352 additions and 485 deletions

View file

@ -1,11 +1,4 @@
use serenity::{
builder::CreateApplicationCommand,
client::Context,
model::{
application::interaction::application_command::ApplicationCommandInteraction,
prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue},
},
};
use serenity::client::Context;
use skynet_discord_bot::common::database::{DataBase, RoleAdder};
use skynet_discord_bot::is_admin;
@ -13,51 +6,40 @@ use sqlx::{Error, Pool, Sqlite};
pub mod edit {
use super::*;
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption};
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
pub async fn run(command: &CommandInteraction, 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
.first()
.expect("Expected role option")
.resolved
.as_ref()
.expect("Expected role object")
let role_a = if let Some(CommandDataOption {
value: CommandDataOptionValue::Role(role),
..
}) = command.data.options.first()
{
role.id.to_owned()
role.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")
let role_b = if let Some(CommandDataOption {
value: CommandDataOptionValue::Role(role),
..
}) = command.data.options.get(1)
{
role.id.to_owned()
role.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")
let role_c = if let Some(CommandDataOption {
value: CommandDataOptionValue::Role(role),
..
}) = command.data.options.get(2)
{
role.id.to_owned()
role.to_owned()
} else {
return "Please provide a valid role for ``Role Current``".to_string();
};
@ -70,14 +52,15 @@ pub mod edit {
return "Role C cannot be same as A or B".to_string();
}
let mut delete = false;
if let Some(x) = command.data.options.get(3) {
let tmp = x.to_owned();
if let Some(CommandDataOptionValue::Boolean(z)) = tmp.resolved {
delete = z;
}
}
let delete = if let Some(CommandDataOption {
value: CommandDataOptionValue::Boolean(z),
..
}) = command.data.options.get(3)
{
*z
} else {
false
};
let db_lock = {
let data_read = ctx.data.read().await;
@ -124,32 +107,13 @@ pub mod edit {
}
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name("roles_adder")
pub fn register() -> CreateCommand {
CreateCommand::new("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)
})
.add_option(CreateCommandOption::new(CommandOptionType::Role, "role_a", "A role you want to add to Role B").required(true))
.add_option(CreateCommandOption::new(CommandOptionType::Role, "role_b", "A role you want to add to Role A").required(true))
.add_option(CreateCommandOption::new(CommandOptionType::Role, "role_c", "Sum of A and B").required(true))
.add_option(CreateCommandOption::new(CommandOptionType::Boolean, "delete", "Delete this entry.").required(false))
}
async fn add_server(db: &Pool<Sqlite>, server: &RoleAdder, delete: bool) -> Result<Option<RoleAdder>, Error> {
@ -160,10 +124,10 @@ pub mod edit {
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)
.bind(server.server.get() as i64)
.bind(server.role_a.get() as i64)
.bind(server.role_b.get() as i64)
.bind(server.role_c.get() as i64)
.fetch_optional(db)
.await
} else {
@ -173,10 +137,10 @@ pub mod edit {
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)
.bind(server.server.get() as i64)
.bind(server.role_a.get() as i64)
.bind(server.role_b.get() as i64)
.bind(server.role_c.get() as i64)
.fetch_optional(db)
.await
}
@ -192,7 +156,7 @@ pub mod tools {
use skynet_discord_bot::common::database::RoleAdder;
use sqlx::{Pool, Sqlite};
pub async fn on_role_change(db: &Pool<Sqlite>, ctx: &Context, mut new_data: Member) {
pub async fn on_role_change(db: &Pool<Sqlite>, ctx: &Context, new_data: Member) {
// check if the role changed is part of the oens for this server
if let Ok(role_adders) = sqlx::query_as::<_, RoleAdder>(
r#"
@ -201,7 +165,7 @@ pub mod tools {
WHERE server = ?
"#,
)
.bind(*new_data.guild_id.as_u64() as i64)
.bind(new_data.guild_id.get() as i64)
.fetch_all(db)
.await
{