feat: now using proper types for the guild and user ID's
This commit is contained in:
parent
ca6ae993c5
commit
6b08f82e2c
4 changed files with 85 additions and 45 deletions
|
@ -1,16 +1,17 @@
|
||||||
use skynet_discord_bot::{db_init, get_config, Accounts, Config, DataBase};
|
use skynet_discord_bot::{db_init, get_config, Accounts, Config, DataBase};
|
||||||
use std::process;
|
use std::{process, sync::Arc};
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serenity::model::id::GuildId;
|
|
||||||
use serenity::{
|
use serenity::{
|
||||||
async_trait,
|
async_trait,
|
||||||
client::{Context, EventHandler},
|
client::{Context, EventHandler},
|
||||||
model::gateway::{GatewayIntents, Ready},
|
model::{
|
||||||
|
gateway::{GatewayIntents, Ready},
|
||||||
|
id::GuildId,
|
||||||
|
},
|
||||||
Client,
|
Client,
|
||||||
};
|
};
|
||||||
use sqlx::{Pool, Sqlite};
|
use sqlx::{Pool, Sqlite};
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,15 @@
|
||||||
use serenity::model::id::GuildId;
|
|
||||||
use serenity::{
|
use serenity::{
|
||||||
async_trait,
|
async_trait,
|
||||||
client::{Context, EventHandler},
|
client::{Context, EventHandler},
|
||||||
model::{
|
model::{
|
||||||
gateway::{GatewayIntents, Ready},
|
gateway::{GatewayIntents, Ready},
|
||||||
prelude::RoleId,
|
id::GuildId,
|
||||||
},
|
},
|
||||||
Client,
|
Client,
|
||||||
};
|
};
|
||||||
use skynet_discord_bot::{db_init, get_config, Accounts, Config, DataBase, Servers};
|
use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, Accounts, Config, DataBase, Servers};
|
||||||
use sqlx::{Pool, Sqlite};
|
use sqlx::{Pool, Sqlite};
|
||||||
use std::process;
|
use std::{process, sync::Arc};
|
||||||
use std::sync::Arc;
|
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
@ -75,7 +73,6 @@ async fn bulk_check(ctx: Arc<Context>) {
|
||||||
let mut roles_set = [0, 0, 0];
|
let mut roles_set = [0, 0, 0];
|
||||||
let mut members = vec![];
|
let mut members = vec![];
|
||||||
|
|
||||||
let server = GuildId::from(server as u64);
|
|
||||||
for member in get_server_member_bulk(&db, &server).await {
|
for member in get_server_member_bulk(&db, &server).await {
|
||||||
if let Some(x) = member.discord {
|
if let Some(x) = member.discord {
|
||||||
members.push(x);
|
members.push(x);
|
||||||
|
@ -89,16 +86,14 @@ async fn bulk_check(ctx: Arc<Context>) {
|
||||||
let mut roles = vec![];
|
let mut roles = vec![];
|
||||||
|
|
||||||
if let Some(role) = &role_past {
|
if let Some(role) = &role_past {
|
||||||
let role = RoleId::from(*role as u64);
|
if !member.roles.contains(role) {
|
||||||
if !member.roles.contains(&role) {
|
|
||||||
roles_set[0] += 1;
|
roles_set[0] += 1;
|
||||||
roles.push(role.to_owned());
|
roles.push(role.to_owned());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(role) = &role_current {
|
if let Some(role) = &role_current {
|
||||||
let role = RoleId::from(*role as u64);
|
if !member.roles.contains(role) {
|
||||||
if !member.roles.contains(&role) {
|
|
||||||
roles_set[1] += 1;
|
roles_set[1] += 1;
|
||||||
roles.push(role.to_owned());
|
roles.push(role.to_owned());
|
||||||
}
|
}
|
||||||
|
@ -111,15 +106,13 @@ async fn bulk_check(ctx: Arc<Context>) {
|
||||||
// old and never
|
// old and never
|
||||||
|
|
||||||
if let Some(role) = &role_past {
|
if let Some(role) = &role_past {
|
||||||
let role = RoleId::from(*role as u64);
|
if member.roles.contains(role) {
|
||||||
if member.roles.contains(&role) {
|
|
||||||
members_all += 1;
|
members_all += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(role) = &role_current {
|
if let Some(role) = &role_current {
|
||||||
let role = RoleId::from(*role as u64);
|
if member.roles.contains(role) {
|
||||||
if member.roles.contains(&role) {
|
|
||||||
roles_set[2] += 1;
|
roles_set[2] += 1;
|
||||||
// if theya re not a current member and have the role then remove it
|
// if theya re not a current member and have the role then remove it
|
||||||
if let Err(e) = member.remove_role(&ctx, role).await {
|
if let Err(e) = member.remove_role(&ctx, role).await {
|
||||||
|
@ -137,17 +130,6 @@ async fn bulk_check(ctx: Arc<Context>) {
|
||||||
println!("{:?} Changes: New: +{}, Current: +{}/-{}", server.as_u64(), roles_set[0], roles_set[1], roles_set[2]);
|
println!("{:?} Changes: New: +{}, Current: +{}/-{}", server.as_u64(), roles_set[0], roles_set[1], roles_set[2]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async fn get_server_config_bulk(db: &Pool<Sqlite>) -> Vec<Servers> {
|
|
||||||
sqlx::query_as::<_, Servers>(
|
|
||||||
r#"
|
|
||||||
SELECT *
|
|
||||||
FROM servers
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.fetch_all(db)
|
|
||||||
.await
|
|
||||||
.unwrap_or_default()
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_server_member_bulk(db: &Pool<Sqlite>, server: &GuildId) -> Vec<Accounts> {
|
async fn get_server_member_bulk(db: &Pool<Sqlite>, server: &GuildId) -> Vec<Accounts> {
|
||||||
sqlx::query_as::<_, Accounts>(
|
sqlx::query_as::<_, Accounts>(
|
||||||
|
|
82
src/lib.rs
82
src/lib.rs
|
@ -1,15 +1,18 @@
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serenity::{
|
use serenity::{
|
||||||
model::{guild, id::GuildId},
|
model::{
|
||||||
|
guild,
|
||||||
|
id::{GuildId, RoleId},
|
||||||
|
},
|
||||||
prelude::TypeMapKey,
|
prelude::TypeMapKey,
|
||||||
};
|
};
|
||||||
|
|
||||||
use sqlx::{
|
use sqlx::{
|
||||||
sqlite::{SqliteConnectOptions, SqlitePoolOptions},
|
sqlite::{SqliteConnectOptions, SqlitePoolOptions, SqliteRow},
|
||||||
Error, Pool, Sqlite,
|
Error, FromRow, Pool, Row, Sqlite,
|
||||||
};
|
};
|
||||||
use std::str::FromStr;
|
use std::{env, str::FromStr, sync::Arc};
|
||||||
use std::{env, sync::Arc};
|
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
@ -99,9 +102,9 @@ fn str_to_num<T: FromStr + Default>(x: &str) -> T {
|
||||||
x.trim().parse::<T>().unwrap_or_default()
|
x.trim().parse::<T>().unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct Accounts {
|
pub struct Accounts {
|
||||||
pub server: i64,
|
pub server: GuildId,
|
||||||
pub wolves_id: String,
|
pub wolves_id: String,
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub expiry: String,
|
pub expiry: String,
|
||||||
|
@ -109,15 +112,60 @@ pub struct Accounts {
|
||||||
pub minecraft: Option<String>,
|
pub minecraft: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)]
|
impl<'r> FromRow<'r, SqliteRow> for Accounts {
|
||||||
|
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
|
||||||
|
let server_tmp: i64 = row.try_get("server")?;
|
||||||
|
let server = GuildId::from(server_tmp as u64);
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
server,
|
||||||
|
wolves_id: row.try_get("wolves_api")?,
|
||||||
|
email: row.try_get("email")?,
|
||||||
|
expiry: row.try_get("expiry")?,
|
||||||
|
discord: row.try_get("discord")?,
|
||||||
|
minecraft: row.try_get("minecraft")?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct Servers {
|
pub struct Servers {
|
||||||
pub server: i64,
|
pub server: GuildId,
|
||||||
pub wolves_api: String,
|
pub wolves_api: String,
|
||||||
pub role_past: Option<i64>,
|
pub role_past: Option<RoleId>,
|
||||||
pub role_current: Option<i64>,
|
pub role_current: Option<RoleId>,
|
||||||
pub member_past: i64,
|
pub member_past: i64,
|
||||||
pub member_current: i64,
|
pub member_current: i64,
|
||||||
}
|
}
|
||||||
|
impl<'r> FromRow<'r, SqliteRow> for Servers {
|
||||||
|
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
|
||||||
|
let server_tmp: i64 = row.try_get("server")?;
|
||||||
|
let server = GuildId::from(server_tmp as u64);
|
||||||
|
let role_past = match row.try_get("role_past") {
|
||||||
|
Ok(x) => {
|
||||||
|
let tmp: i64 = x;
|
||||||
|
Some(RoleId::from(tmp as u64))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
let role_current = match row.try_get("role_current") {
|
||||||
|
Ok(x) => {
|
||||||
|
let tmp: i64 = x;
|
||||||
|
Some(RoleId::from(tmp as u64))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
server,
|
||||||
|
wolves_api: row.try_get("wolves_api")?,
|
||||||
|
role_past,
|
||||||
|
role_current,
|
||||||
|
member_past: row.try_get("member_past")?,
|
||||||
|
member_current: row.try_get("member_current")?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
@ -190,3 +238,15 @@ pub async fn get_server_member(db: &Pool<Sqlite>, server: &GuildId, member: &gui
|
||||||
.await
|
.await
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_server_config_bulk(db: &Pool<Sqlite>) -> Vec<Servers> {
|
||||||
|
sqlx::query_as::<_, Servers>(
|
||||||
|
r#"
|
||||||
|
SELECT *
|
||||||
|
FROM servers
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.fetch_all(db)
|
||||||
|
.await
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ use serenity::{
|
||||||
model::{
|
model::{
|
||||||
gateway::{GatewayIntents, Ready},
|
gateway::{GatewayIntents, Ready},
|
||||||
guild,
|
guild,
|
||||||
prelude::RoleId,
|
|
||||||
},
|
},
|
||||||
Client,
|
Client,
|
||||||
};
|
};
|
||||||
|
@ -33,15 +32,13 @@ impl EventHandler for Handler {
|
||||||
let mut roles = vec![];
|
let mut roles = vec![];
|
||||||
|
|
||||||
if let Some(role) = &config.role_past {
|
if let Some(role) = &config.role_past {
|
||||||
let role = RoleId::from(*role as u64);
|
if !new_member.roles.contains(role) {
|
||||||
if !new_member.roles.contains(&role) {
|
|
||||||
roles.push(role.to_owned());
|
roles.push(role.to_owned());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(role) = &config.role_current {
|
if let Some(role) = &config.role_current {
|
||||||
let role = RoleId::from(*role as u64);
|
if !new_member.roles.contains(role) {
|
||||||
if !new_member.roles.contains(&role) {
|
|
||||||
roles.push(role.to_owned());
|
roles.push(role.to_owned());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue