fmt: fmt and clippy

This commit is contained in:
silver 2024-09-30 00:09:29 +01:00
parent 5e7964ae26
commit 0d9ce2de7f
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
3 changed files with 84 additions and 109 deletions

View file

@ -20,13 +20,13 @@ pub mod edit {
} }
let role_a = if let CommandDataOptionValue::Role(role) = command let role_a = if let CommandDataOptionValue::Role(role) = command
.data .data
.options .options
.get(0) .get(0)
.expect("Expected role option") .expect("Expected role option")
.resolved .resolved
.as_ref() .as_ref()
.expect("Expected role object") .expect("Expected role object")
{ {
role.id.to_owned() role.id.to_owned()
} else { } else {
@ -34,13 +34,13 @@ pub mod edit {
}; };
let role_b = if let CommandDataOptionValue::Role(role) = command let role_b = if let CommandDataOptionValue::Role(role) = command
.data .data
.options .options
.get(1) .get(1)
.expect("Expected role option") .expect("Expected role option")
.resolved .resolved
.as_ref() .as_ref()
.expect("Expected role object") .expect("Expected role object")
{ {
role.id.to_owned() role.id.to_owned()
} else { } else {
@ -48,13 +48,13 @@ pub mod edit {
}; };
let role_c = if let CommandDataOptionValue::Role(role) = command let role_c = if let CommandDataOptionValue::Role(role) = command
.data .data
.options .options
.get(2) .get(2)
.expect("Expected role option") .expect("Expected role option")
.resolved .resolved
.as_ref() .as_ref()
.expect("Expected role object") .expect("Expected role object")
{ {
role.id.to_owned() role.id.to_owned()
} else { } else {
@ -65,17 +65,13 @@ pub mod edit {
return "Roles A and B must be different".to_string(); return "Roles A and B must be different".to_string();
} }
if (role_c == role_a)|| (role_c == role_b) { if (role_c == role_a) || (role_c == role_b) {
return "Role C cannot be same as A or B".to_string(); return "Role C cannot be same as A or B".to_string();
} }
let mut delete = false; let mut delete = false;
if let Some(x) = command if let Some(x) = command.data.options.get(3) {
.data
.options
.get(3) {
let tmp = x.to_owned(); let tmp = x.to_owned();
if let Some(y) = tmp.resolved { if let Some(y) = tmp.resolved {
match y { match y {
@ -109,63 +105,55 @@ pub mod edit {
} }
} }
let mut role_a_name = String::new(); let mut role_a_name = String::new();
let mut role_b_name = String::new(); let mut role_b_name = String::new();
let mut role_c_name = String::new(); let mut role_c_name = String::new();
if let Ok(x) = server.roles(&ctx).await { if let Ok(x) = server.roles(&ctx).await {
if let Some(y) = x.get(&role_a){ if let Some(y) = x.get(&role_a) {
role_a_name = y.to_owned().name; role_a_name = y.to_owned().name;
} }
if let Some(y) = x.get(&role_b){ if let Some(y) = x.get(&role_b) {
role_b_name = y.to_owned().name; role_b_name = y.to_owned().name;
} }
if let Some(y) = x.get(&role_b){ if let Some(y) = x.get(&role_b) {
role_c_name = y.to_owned().name; role_c_name = y.to_owned().name;
} }
} }
if delete { if delete {
format!("Removed {} + {} = {}", role_a_name, role_b_name, role_c_name) format!("Removed {} + {} = {}", role_a_name, role_b_name, role_c_name)
} else { } else {
format!("Added {} + {} = {}", role_a_name, role_b_name, role_c_name) format!("Added {} + {} = {}", role_a_name, role_b_name, role_c_name)
} }
} }
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command command
.name("roles_adder") .name("roles_adder")
.description("Combine roles together to an new one") .description("Combine roles together to an new one")
.create_option(|option| { .create_option(|option| {
option option
.name("role_a") .name("role_a")
.description("A role you want to add to Role B") .description("A role you want to add to Role B")
.kind(CommandOptionType::Role) .kind(CommandOptionType::Role)
.required(true) .required(true)
}) })
.create_option(|option| { .create_option(|option| {
option option
.name("role_b") .name("role_b")
.description("A role you want to add to Role A") .description("A role you want to add to Role A")
.kind(CommandOptionType::Role) .kind(CommandOptionType::Role)
.required(true) .required(true)
}) })
.create_option(|option| { .create_option(|option| option.name("role_c").description("Sum of A and B").kind(CommandOptionType::Role).required(true))
option .create_option(|option| {
.name("role_c") option
.description("Sum of A and B") .name("delete")
.kind(CommandOptionType::Role) .description("Delete this entry.")
.required(true) .kind(CommandOptionType::Boolean)
}) .required(false)
.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> { async fn add_server(db: &Pool<Sqlite>, server: &RoleAdder, delete: bool) -> Result<Option<RoleAdder>, Error> {
@ -177,12 +165,12 @@ pub mod edit {
WHERE server = ?1 AND role_a = ?2 AND role_b = ?3 AND role_c = ?4 WHERE server = ?1 AND role_a = ?2 AND role_b = ?3 AND role_c = ?4
", ",
) )
.bind(*server.server.as_u64() as i64) .bind(*server.server.as_u64() as i64)
.bind(*server.role_a.as_u64() as i64) .bind(*server.role_a.as_u64() as i64)
.bind(*server.role_b.as_u64() as i64) .bind(*server.role_b.as_u64() as i64)
.bind(*server.role_c.as_u64() as i64) .bind(*server.role_c.as_u64() as i64)
.fetch_optional(db) .fetch_optional(db)
.await; .await;
} else { } else {
action = sqlx::query_as::<_, RoleAdder>( action = sqlx::query_as::<_, RoleAdder>(
" "
@ -190,32 +178,28 @@ pub mod edit {
VALUES (?1, ?2, ?3, ?4) VALUES (?1, ?2, ?3, ?4)
", ",
) )
.bind(*server.server.as_u64() as i64) .bind(*server.server.as_u64() as i64)
.bind(*server.role_a.as_u64() as i64) .bind(*server.role_a.as_u64() as i64)
.bind(*server.role_b.as_u64() as i64) .bind(*server.role_b.as_u64() as i64)
.bind(*server.role_c.as_u64() as i64) .bind(*server.role_c.as_u64() as i64)
.fetch_optional(db) .fetch_optional(db)
.await; .await;
} }
action action
} }
} }
// TODO // TODO
pub mod list {} pub mod list {}
pub mod tools { pub mod tools {
use serenity::client::Context; use serenity::client::Context;
use serenity::model::guild::Member; use serenity::model::guild::Member;
use sqlx::{Pool, Sqlite};
use skynet_discord_bot::RoleAdder; use skynet_discord_bot::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, mut new_data: Member) {
// check if the role changed is part of the oens for this server // check if the role changed is part of the oens for this server
if let Some(role_adders) = sqlx::query_as::<_, RoleAdder>( if let Some(role_adders) = sqlx::query_as::<_, RoleAdder>(
r#" r#"
@ -224,47 +208,40 @@ pub mod tools {
WHERE server = ? WHERE server = ?
"#, "#,
) )
.bind(*new_data.guild_id.as_u64() as i64) .bind(*new_data.guild_id.as_u64() as i64)
.fetch_all(db) .fetch_all(db)
.await .await
.ok() { .ok()
{
let mut roles_add = vec![]; let mut roles_add = vec![];
let mut roles_remove = vec![]; let mut roles_remove = vec![];
for role_adder in role_adders { for role_adder in role_adders {
// if the user has both A dnd B give them C // if the user has both A dnd B give them C
if new_data.roles.contains(&role_adder.role_a) && if new_data.roles.contains(&role_adder.role_a) && new_data.roles.contains(&role_adder.role_b) && !new_data.roles.contains(&role_adder.role_c)
new_data.roles.contains(&role_adder.role_b) && {
!new_data.roles.contains(&role_adder.role_c) {
roles_add.push(role_adder.role_c); roles_add.push(role_adder.role_c);
} }
// If the suer has C but not A or B remove C // If the suer has C but not A or B remove C
if new_data.roles.contains(&role_adder.role_c) && if new_data.roles.contains(&role_adder.role_c)
( && (!new_data.roles.contains(&role_adder.role_a) || !new_data.roles.contains(&role_adder.role_b))
!new_data.roles.contains(&role_adder.role_a) || !new_data.roles.contains(&role_adder.role_b) {
) {
roles_remove.push(role_adder.role_c); roles_remove.push(role_adder.role_c);
} }
} }
if !roles_add.is_empty() {
if !roles_add.is_empty(){
if let Err(e) = new_data.add_roles(&ctx, &roles_add).await { if let Err(e) = new_data.add_roles(&ctx, &roles_add).await {
println!("{:?}", e); println!("{:?}", e);
} }
} }
if !roles_remove.is_empty(){ if !roles_remove.is_empty() {
if let Err(e) = new_data.remove_roles(&ctx, &roles_remove).await{ if let Err(e) = new_data.remove_roles(&ctx, &roles_remove).await {
println!("{:?}", e); println!("{:?}", e);
} }
} }
} }
} }
} }

View file

@ -314,7 +314,7 @@ impl<'r> FromRow<'r, SqliteRow> for RoleAdder {
} }
} }
fn get_role_from_row(row: &SqliteRow, col: &str)-> RoleId{ fn get_role_from_row(row: &SqliteRow, col: &str) -> RoleId {
match row.try_get(col) { match row.try_get(col) {
Ok(x) => { Ok(x) => {
let tmp: i64 = x; let tmp: i64 = x;
@ -324,7 +324,6 @@ fn get_role_from_row(row: &SqliteRow, col: &str)-> RoleId{
} }
} }
pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> { pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
let database = format!("{}/{}", &config.home, &config.database); let database = format!("{}/{}", &config.home, &config.database);

View file

@ -1,5 +1,7 @@
pub mod commands; pub mod commands;
use crate::commands::role_adder::tools::on_role_change;
use serenity::model::guild::Member;
use serenity::{ use serenity::{
async_trait, async_trait,
client::{Context, EventHandler}, client::{Context, EventHandler},
@ -11,11 +13,9 @@ use serenity::{
}, },
Client, Client,
}; };
use std::sync::Arc;
use serenity::model::guild::Member;
use skynet_discord_bot::{db_init, get_config, get_server_config, get_server_member, Config, DataBase}; use skynet_discord_bot::{db_init, get_config, get_server_config, get_server_member, Config, DataBase};
use std::sync::Arc;
use tokio::sync::RwLock; use tokio::sync::RwLock;
use crate::commands::role_adder::tools::on_role_change;
struct Handler; struct Handler;
@ -125,8 +125,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
} }
// handles role updates // handles role updates
async fn guild_member_update(&self, ctx: Context, _old_data: Option<Member>, new_data: Member){ async fn guild_member_update(&self, ctx: Context, _old_data: Option<Member>, new_data: Member) {
// get config/db // get config/db
let db_lock = { let db_lock = {
let data_read = ctx.data.read().await; let data_read = ctx.data.read().await;