fmt: formatting and clippy

This commit is contained in:
silver 2024-10-28 21:53:04 +00:00
parent b7161e2614
commit 344d6d3585
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
14 changed files with 130 additions and 138 deletions

View file

@ -1,13 +1,13 @@
use serenity::prelude::TypeMapKey;
use std::sync::Arc;
use tokio::sync::RwLock;
use sqlx::{Error, FromRow, Pool, Row, Sqlite};
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions, SqliteRow};
use serenity::model::id::{ChannelId, GuildId, RoleId, UserId};
use crate::Config;
use serde::{Deserialize, Serialize};
use serenity::model::guild;
use serenity::model::id::{ChannelId, GuildId, RoleId, UserId};
use serenity::prelude::TypeMapKey;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions, SqliteRow};
use sqlx::{Error, FromRow, Pool, Row, Sqlite};
use std::str::FromStr;
use crate::Config;
use std::sync::Arc;
use tokio::sync::RwLock;
pub struct DataBase;
impl TypeMapKey for DataBase {
@ -267,4 +267,4 @@ pub async fn get_server_config_bulk(db: &Pool<Sqlite>) -> Vec<Servers> {
.fetch_all(db)
.await
.unwrap_or_default()
}
}

View file

@ -1,11 +1,10 @@
use serde::{Deserialize, Serialize};
use crate::common::set_roles::normal::get_server_member_bulk;
use crate::Config;
use serde::de::DeserializeOwned;
use sqlx::{Error, FromRow, Pool, Row, Sqlite};
use serde::{Deserialize, Serialize};
use serenity::model::id::GuildId;
use sqlx::sqlite::SqliteRow;
use crate::Config;
use crate::common::set_roles::normal::get_server_member_bulk;
use sqlx::{Error, FromRow, Pool, Row, Sqlite};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Minecraft {
@ -25,7 +24,6 @@ impl<'r> FromRow<'r, SqliteRow> for Minecraft {
}
}
/**
loop through all members of server
get a list of folks with mc accounts that are members
@ -164,4 +162,3 @@ pub async fn get_minecraft_config_server(db: &Pool<Sqlite>, g_id: GuildId) -> Ve
.await
.unwrap_or_default()
}

View file

@ -1,4 +1,4 @@
pub mod wolves;
pub mod database;
pub mod minecraft;
pub mod set_roles;
pub mod set_roles;
pub mod wolves;

View file

@ -1,11 +1,11 @@
pub mod normal {
use serenity::client::Context;
use serenity::model::id::{GuildId, RoleId, UserId};
use sqlx::{Pool, Sqlite};
use crate::common::database::{DataBase, ServerMembersWolves, Servers, Wolves};
use crate::get_now_iso;
use crate::common::database::{DataBase, ServerMembersWolves, Servers, Wolves};
use crate::get_now_iso;
use serenity::client::Context;
use serenity::model::id::{GuildId, RoleId, UserId};
use sqlx::{Pool, Sqlite};
pub async fn update_server(ctx: &Context, server: &Servers, remove_roles: &[Option<RoleId>], members_changed: &[UserId]) {
pub async fn update_server(ctx: &Context, server: &Servers, remove_roles: &[Option<RoleId>], members_changed: &[UserId]) {
let db_lock = {
let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Database in TypeMap.").clone()
@ -131,14 +131,14 @@ pub mod normal {
// for updating committee members
pub mod committee {
use std::collections::HashMap;
use std::sync::Arc;
use crate::common::database::{DataBase, Wolves};
use crate::common::wolves::committees::Committees;
use serenity::client::Context;
use serenity::model::guild::Member;
use serenity::model::id::{GuildId, RoleId};
use sqlx::{Pool, Sqlite};
use crate::common::database::{DataBase, Wolves};
use crate::common::wolves::committees::Committees;
use std::collections::HashMap;
use std::sync::Arc;
pub async fn check_committee(ctx: Arc<Context>) {
let db_lock = {
@ -154,7 +154,7 @@ pub mod committee {
update_committees(&db, &ctx, &mut members).await;
}
pub async fn update_committees(db: &Pool<Sqlite>, ctx: &Context, members: &mut Vec<Member>){
pub async fn update_committees(db: &Pool<Sqlite>, ctx: &Context, members: &mut Vec<Member>) {
let server = GuildId(1220150752656363520);
let committee_member = RoleId(1226602779968274573);
let committees = get_committees(db).await;
@ -168,24 +168,21 @@ pub mod committee {
roles_name.insert(role.name.to_owned(), role.to_owned());
}
// a map of users and the roles they are goign to be getting
let mut users_roles = HashMap::new();
// a list of all the roles that can be removed from folks who should have them
let mut committee_roles = vec![
committee_member
];
let mut committee_roles = vec![committee_member];
for committee in &committees {
// get the role for this committee/club/soc
let role = match roles_name.get(&committee.name) {
Some(x) => {Some(x.to_owned())}
Some(x) => Some(x.to_owned()),
None => {
// create teh role if it does not exist
match server.create_role(&ctx, |r| r.hoist(false).mentionable(true).name(&committee.name)).await{
Ok(x) => { Some(x) }
Err(_) => {None}
match server.create_role(&ctx, |r| r.hoist(false).mentionable(true).name(&committee.name)).await {
Ok(x) => Some(x),
Err(_) => None,
}
}
};
@ -196,7 +193,7 @@ pub mod committee {
for id_wolves in &committee.committee {
// ID in this is the wolves ID, so we need to get a matching discord ID (if one exists)
if let Some(x) = get_server_member_discord(&db, id_wolves).await {
if let Some(x) = get_server_member_discord(db, id_wolves).await {
if let Some(member_tmp) = x.discord {
let values = users_roles.entry(member_tmp).or_insert(vec![]);
values.push(r.id);
@ -245,9 +242,9 @@ pub mod committee {
FROM committees
"#,
)
.fetch_all(db)
.await
.unwrap_or_default()
.fetch_all(db)
.await
.unwrap_or_default()
}
async fn get_server_member_discord(db: &Pool<Sqlite>, user: &i64) -> Option<Wolves> {
@ -258,9 +255,9 @@ pub mod committee {
WHERE id_wolves = ?
"#,
)
.bind(user)
.fetch_one(db)
.await
.ok()
.bind(user)
.fetch_one(db)
.await
.ok()
}
}
}

View file

@ -1,12 +1,10 @@
use crate::common::database::Wolves;
use serde::{Deserialize, Serialize};
use sqlx::{Pool, Sqlite};
use crate::common::database::Wolves;
/**
This file relates to anything that directly interacts with teh wolves API
*/
This file relates to anything that directly interacts with teh wolves API
*/
#[derive(Deserialize, Serialize, Debug)]
struct WolvesResultUserMin {
@ -33,10 +31,10 @@ async fn add_users_wolves(db: &Pool<Sqlite>, user: &WolvesResultUserMin) {
ON CONFLICT(id_wolves) DO UPDATE SET email = $2
",
)
.bind(&user.member_id)
.bind(&user.contact_email)
.fetch_optional(db)
.await
.bind(&user.member_id)
.bind(&user.contact_email)
.fetch_optional(db)
.await
{
Ok(_) => {}
Err(e) => {
@ -46,20 +44,19 @@ async fn add_users_wolves(db: &Pool<Sqlite>, user: &WolvesResultUserMin) {
}
}
/**
This is getting data for Clubs and Socs
*/
pub mod cns {
use crate::common::database::{get_server_config_bulk, DataBase, ServerMembers, ServerMembersWolves, Servers};
use crate::common::set_roles::normal::update_server;
use std::collections::BTreeMap;
use crate::common::wolves::{add_users_wolves, WolvesResultUserMin};
use crate::Config;
use serde::{Deserialize, Serialize};
use serenity::client::Context;
use serenity::model::id::GuildId;
use sqlx::{Pool, Sqlite};
use crate::Config;
use crate::common::database::{get_server_config_bulk, DataBase, ServerMembers, ServerMembersWolves, Servers, Wolves};
use crate::common::wolves::{add_users_wolves, WolvesResultUserMin};
use std::collections::BTreeMap;
#[derive(Deserialize, Serialize, Debug)]
struct WolvesResultUser {
@ -222,11 +219,11 @@ pub mod cns {
Get and store the data on C&S committees
*/
pub mod committees {
use crate::common::database::DataBase;
use crate::Config;
use serde::{Deserialize, Serialize};
use serenity::client::Context;
use sqlx::{ Pool, Sqlite};
use crate::common::database::{DataBase};
use crate::Config;
use sqlx::{Pool, Sqlite};
// This is what Wolves returns to us
#[derive(Deserialize, Serialize, Debug)]
@ -243,22 +240,22 @@ pub mod committees {
// Link to their page such as https://ulwolves.ie/society/computer
link: String,
// array of Committee members member_id's
committee: Vec<String>
committee: Vec<String>,
}
// Database entry for it
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct Committees {
pub id: i64,
pub name: String,
pub link: String,
#[sqlx(json)]
pub committee: Vec<i64>,
pub id: i64,
pub name: String,
pub link: String,
#[sqlx(json)]
pub committee: Vec<i64>,
}
impl From<WolvesResultCNS> for Committees {
fn from(value: WolvesResultCNS) -> Self {
Self{
Self {
id: value.id.parse().unwrap_or(0),
name: value.name,
link: value.link,
@ -267,7 +264,7 @@ pub mod committees {
}
}
pub async fn get_cns(ctx: &Context){
pub async fn get_cns(ctx: &Context) {
let db_lock = {
let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Database in TypeMap.").clone()
@ -283,7 +280,7 @@ pub mod committees {
// TODO: proper api key management
let api_key = "";
// request data from wolves
for committee in get_committees(&config, api_key).await {
for committee in get_committees(&config, api_key).await {
let tmp = Committees::from(committee);
add_committee(&db, &tmp).await;
}
@ -299,7 +296,11 @@ pub mod committees {
// get wolves data
if let Ok(mut res) = surf::post(&url).header("X-AM-Identity", wolves_api).await {
if let Ok(WolvesResult { success, result, }) = res.body_json().await {
if let Ok(WolvesResult {
success,
result,
}) = res.body_json().await
{
if success != 1 {
return vec![];
}
@ -319,12 +320,12 @@ pub mod committees {
ON CONFLICT(id) DO UPDATE SET committee = $4
",
)
.bind(committee.id)
.bind(&committee.name)
.bind(&committee.link)
.bind(serde_json::to_string(&committee.committee).unwrap_or_default())
.fetch_optional(db)
.await
.bind(committee.id)
.bind(&committee.name)
.bind(&committee.link)
.bind(serde_json::to_string(&committee.committee).unwrap_or_default())
.fetch_optional(db)
.await
{
Ok(_) => {}
Err(e) => {
@ -339,12 +340,11 @@ pub mod committees {
get the data for an individual user
*/
pub mod individual {
use serde::{Deserialize, Serialize};
use serenity::client::Context;
use crate::common::database::DataBase;
use crate::common::wolves::{add_users_wolves, WolvesResultUserMin};
use crate::Config;
use serde::{Deserialize, Serialize};
use serenity::client::Context;
#[derive(Deserialize, Serialize, Debug)]
struct WolvesResultUser {
@ -378,8 +378,7 @@ pub mod individual {
result: WolvesResultUser,
}
pub async fn get_user(ctx: &Context, email: &str) -> bool{
pub async fn get_user(ctx: &Context, email: &str) -> bool {
let db_lock = {
let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Database in TypeMap.").clone()
@ -396,9 +395,7 @@ pub mod individual {
let api_key = "";
// request data from wolves
match get_user_sub(&config, api_key, email).await {
None => {
false
}
None => false,
// if exists save it and return true
Some(user) => {
// add to db
@ -409,7 +406,7 @@ pub mod individual {
}
}
async fn get_user_sub(config: &Config, wolves_api: &str, email: &str) -> Option<WolvesResultUser> {
async fn get_user_sub(config: &Config, wolves_api: &str, _email: &str) -> Option<WolvesResultUser> {
if config.wolves_url.is_empty() {
return None;
}
@ -419,7 +416,11 @@ pub mod individual {
// get wolves data
if let Ok(mut res) = surf::post(&url).header("X-AM-Identity", wolves_api).await {
if let Ok(WolvesResult { success, result, }) = res.body_json().await {
if let Ok(WolvesResult {
success,
result,
}) = res.body_json().await
{
if success != 1 {
return None;
}
@ -430,4 +431,4 @@ pub mod individual {
None
}
}
}