fmt: fmt and clippy
This commit is contained in:
parent
5e7964ae26
commit
0d9ce2de7f
3 changed files with 84 additions and 109 deletions
|
@ -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,24 +105,22 @@ 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 {
|
||||
|
@ -152,13 +146,7 @@ pub mod edit {
|
|||
.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("role_c").description("Sum of A and B").kind(CommandOptionType::Role).required(true))
|
||||
.create_option(|option| {
|
||||
option
|
||||
.name("delete")
|
||||
|
@ -200,22 +188,18 @@ pub mod edit {
|
|||
|
||||
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#"
|
||||
|
@ -227,44 +211,37 @@ pub mod tools {
|
|||
.bind(*new_data.guild_id.as_u64() as i64)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
.ok() {
|
||||
|
||||
|
||||
.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue