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
.data
.options
.get(0)
.expect("Expected role option")
.resolved
.as_ref()
.expect("Expected role object")
.data
.options
.get(0)
.expect("Expected role option")
.resolved
.as_ref()
.expect("Expected role object")
{
role.id.to_owned()
} else {
@ -34,13 +34,13 @@ pub mod edit {
};
let role_b = if let CommandDataOptionValue::Role(role) = command
.data
.options
.get(1)
.expect("Expected role option")
.resolved
.as_ref()
.expect("Expected role object")
.data
.options
.get(1)
.expect("Expected role option")
.resolved
.as_ref()
.expect("Expected role object")
{
role.id.to_owned()
} else {
@ -48,13 +48,13 @@ pub mod edit {
};
let role_c = if let CommandDataOptionValue::Role(role) = command
.data
.options
.get(2)
.expect("Expected role option")
.resolved
.as_ref()
.expect("Expected role object")
.data
.options
.get(2)
.expect("Expected role option")
.resolved
.as_ref()
.expect("Expected role object")
{
role.id.to_owned()
} else {
@ -65,17 +65,13 @@ pub mod edit {
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();
}
let mut delete = false;
if let Some(x) = command
.data
.options
.get(3) {
if let Some(x) = command.data.options.get(3) {
let tmp = x.to_owned();
if let Some(y) = tmp.resolved {
match y {
@ -109,63 +105,55 @@ pub mod edit {
}
}
let mut role_a_name = String::new();
let mut role_b_name = String::new();
let mut role_c_name = String::new();
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;
}
if let Some(y) = x.get(&role_b){
if let Some(y) = x.get(&role_b) {
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;
}
}
if delete {
format!("Removed {} + {} = {}", role_a_name, role_b_name, role_c_name)
} 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 {
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)
})
.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> {
@ -177,12 +165,12 @@ 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)
.fetch_optional(db)
.await;
.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>(
"
@ -190,32 +178,28 @@ 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)
.fetch_optional(db)
.await;
.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 {}
pub mod tools {
use serenity::client::Context;
use serenity::model::guild::Member;
use sqlx::{Pool, Sqlite};
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
if let Some(role_adders) = sqlx::query_as::<_, RoleAdder>(
r#"
@ -224,47 +208,40 @@ pub mod tools {
WHERE server = ?
"#,
)
.bind(*new_data.guild_id.as_u64() as i64)
.fetch_all(db)
.await
.ok() {
.bind(*new_data.guild_id.as_u64() as i64)
.fetch_all(db)
.await
.ok()
{
let mut roles_add = vec![];
let mut roles_remove = vec![];
for role_adder in role_adders {
// if the user has both A dnd B give them C
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) {
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)
{
roles_add.push(role_adder.role_c);
}
// If the suer has C but not A or B remove 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)
) {
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))
{
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 {
println!("{:?}", e);
}
}
if !roles_remove.is_empty(){
if let Err(e) = new_data.remove_roles(&ctx, &roles_remove).await{
if !roles_remove.is_empty() {
if let Err(e) = new_data.remove_roles(&ctx, &roles_remove).await {
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) {
Ok(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> {
let database = format!("{}/{}", &config.home, &config.database);

View file

@ -1,5 +1,7 @@
pub mod commands;
use crate::commands::role_adder::tools::on_role_change;
use serenity::model::guild::Member;
use serenity::{
async_trait,
client::{Context, EventHandler},
@ -11,11 +13,9 @@ use serenity::{
},
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 std::sync::Arc;
use tokio::sync::RwLock;
use crate::commands::role_adder::tools::on_role_change;
struct Handler;
@ -125,8 +125,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
}
// 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
let db_lock = {
let data_read = ctx.data.read().await;