2024-10-28 00:51:39 +00:00
|
|
|
pub mod common;
|
|
|
|
|
2023-09-16 20:05:50 +01:00
|
|
|
use chrono::{Datelike, SecondsFormat, Utc};
|
2024-10-28 21:53:04 +00:00
|
|
|
use dotenvy::dotenv;
|
2025-02-19 00:26:54 +00:00
|
|
|
use rand::{distr::Alphanumeric, rng, Rng};
|
2025-02-19 00:17:02 +00:00
|
|
|
use serenity::all::CommandInteraction;
|
2023-11-23 16:12:28 +00:00
|
|
|
use serenity::client::Context;
|
2025-02-18 21:41:28 +00:00
|
|
|
use serenity::model::id::{ChannelId, GuildId, RoleId};
|
2024-10-28 21:53:04 +00:00
|
|
|
use serenity::prelude::TypeMapKey;
|
2024-10-28 00:59:04 +00:00
|
|
|
use std::{env, sync::Arc};
|
2023-09-11 02:25:07 +01:00
|
|
|
use tokio::sync::RwLock;
|
|
|
|
|
2024-11-23 23:50:25 +00:00
|
|
|
#[derive(Debug)]
|
2023-09-11 02:25:07 +01:00
|
|
|
pub struct Config {
|
2024-09-17 00:21:07 +01:00
|
|
|
// manages where teh database is stored
|
2023-09-11 02:25:07 +01:00
|
|
|
pub home: String,
|
|
|
|
pub database: String,
|
|
|
|
|
2024-09-17 00:21:07 +01:00
|
|
|
// tokens for discord and other API's
|
2023-09-17 21:02:41 +01:00
|
|
|
pub discord_token: String,
|
2024-09-17 00:21:07 +01:00
|
|
|
pub discord_token_minecraft: String,
|
2024-11-30 00:44:36 +00:00
|
|
|
pub minecraft_mcprofile: String,
|
2023-09-17 21:02:41 +01:00
|
|
|
|
2024-09-17 00:21:07 +01:00
|
|
|
// email settings
|
2023-09-11 02:25:07 +01:00
|
|
|
pub mail_smtp: String,
|
|
|
|
pub mail_user: String,
|
|
|
|
pub mail_pass: String,
|
2023-10-27 01:39:03 +01:00
|
|
|
|
2024-09-17 00:21:07 +01:00
|
|
|
// wolves API base for clubs/socs
|
2023-10-27 01:39:03 +01:00
|
|
|
pub wolves_url: String,
|
2024-11-09 01:17:43 +00:00
|
|
|
// API key for accessing more general resources
|
|
|
|
pub wolves_api: String,
|
2024-11-09 14:55:26 +00:00
|
|
|
|
|
|
|
// discord server for committee
|
|
|
|
pub committee_server: GuildId,
|
|
|
|
pub committee_role: RoleId,
|
2024-11-09 16:17:43 +00:00
|
|
|
pub committee_category: ChannelId,
|
2023-09-11 02:25:07 +01:00
|
|
|
}
|
|
|
|
impl TypeMapKey for Config {
|
|
|
|
type Value = Arc<RwLock<Config>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_config() -> Config {
|
|
|
|
dotenv().ok();
|
|
|
|
|
|
|
|
// reasonable defaults
|
|
|
|
let mut config = Config {
|
|
|
|
discord_token: "".to_string(),
|
2024-09-17 00:21:07 +01:00
|
|
|
discord_token_minecraft: "".to_string(),
|
2024-11-30 00:44:36 +00:00
|
|
|
minecraft_mcprofile: "".to_string(),
|
2023-09-11 02:25:07 +01:00
|
|
|
|
|
|
|
home: ".".to_string(),
|
|
|
|
database: "database.db".to_string(),
|
|
|
|
|
|
|
|
mail_smtp: "".to_string(),
|
|
|
|
mail_user: "".to_string(),
|
|
|
|
mail_pass: "".to_string(),
|
2023-10-27 01:39:03 +01:00
|
|
|
wolves_url: "".to_string(),
|
2025-02-18 13:36:08 +00:00
|
|
|
wolves_api: "".to_string(),
|
2025-02-19 00:42:26 +00:00
|
|
|
committee_server: GuildId::new(1),
|
|
|
|
committee_role: RoleId::new(1),
|
|
|
|
committee_category: ChannelId::new(1),
|
2023-09-11 02:25:07 +01:00
|
|
|
};
|
|
|
|
|
2024-09-17 23:20:31 +01:00
|
|
|
if let Ok(x) = env::var("DATABASE_HOME") {
|
2023-09-11 02:25:07 +01:00
|
|
|
config.home = x.trim().to_string();
|
|
|
|
}
|
|
|
|
if let Ok(x) = env::var("DATABASE") {
|
|
|
|
config.database = x.trim().to_string();
|
|
|
|
}
|
|
|
|
|
2023-09-17 21:02:41 +01:00
|
|
|
if let Ok(x) = env::var("DISCORD_TOKEN") {
|
|
|
|
config.discord_token = x.trim().to_string();
|
|
|
|
}
|
2024-09-17 00:21:07 +01:00
|
|
|
if let Ok(x) = env::var("DISCORD_TOKEN_MINECRAFT") {
|
|
|
|
config.discord_token_minecraft = x.trim().to_string();
|
2024-03-03 12:49:55 +00:00
|
|
|
}
|
2024-11-30 00:44:36 +00:00
|
|
|
if let Ok(x) = env::var("MINECRAFT_MCPROFILE_KEY") {
|
|
|
|
config.minecraft_mcprofile = x.trim().to_string();
|
|
|
|
}
|
2023-09-17 21:02:41 +01:00
|
|
|
|
2023-09-11 02:25:07 +01:00
|
|
|
if let Ok(x) = env::var("EMAIL_SMTP") {
|
|
|
|
config.mail_smtp = x.trim().to_string();
|
|
|
|
}
|
|
|
|
if let Ok(x) = env::var("EMAIL_USER") {
|
|
|
|
config.mail_user = x.trim().to_string();
|
|
|
|
}
|
|
|
|
if let Ok(x) = env::var("EMAIL_PASS") {
|
|
|
|
config.mail_pass = x.trim().to_string();
|
|
|
|
}
|
|
|
|
|
2024-11-09 16:53:26 +00:00
|
|
|
if let Ok(x) = env::var("WOLVES_URL_BASE") {
|
2023-10-27 01:39:03 +01:00
|
|
|
config.wolves_url = x.trim().to_string();
|
|
|
|
}
|
2024-11-09 01:17:43 +00:00
|
|
|
if let Ok(x) = env::var("WOLVES_API") {
|
|
|
|
config.wolves_api = x.trim().to_string();
|
|
|
|
}
|
|
|
|
|
2024-11-09 14:55:26 +00:00
|
|
|
if let Ok(x) = env::var("COMMITTEE_DISCORD") {
|
|
|
|
if let Ok(x) = x.trim().parse::<u64>() {
|
2025-02-19 00:17:02 +00:00
|
|
|
config.committee_server = GuildId::new(x);
|
2024-11-09 14:55:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Ok(x) = env::var("COMMITTEE_DISCORD") {
|
|
|
|
if let Ok(x) = x.trim().parse::<u64>() {
|
2025-02-19 00:17:02 +00:00
|
|
|
config.committee_role = RoleId::new(x);
|
2024-11-09 14:55:26 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-09 16:17:43 +00:00
|
|
|
if let Ok(x) = env::var("COMMITTEE_CATEGORY") {
|
|
|
|
if let Ok(x) = x.trim().parse::<u64>() {
|
2025-02-19 00:17:02 +00:00
|
|
|
config.committee_category = ChannelId::new(x);
|
2024-11-09 16:17:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-09 14:55:26 +00:00
|
|
|
|
2023-09-11 02:25:07 +01:00
|
|
|
config
|
|
|
|
}
|
|
|
|
|
2023-09-16 20:03:58 +01:00
|
|
|
pub fn get_now_iso(short: bool) -> String {
|
|
|
|
let now = Utc::now();
|
|
|
|
if short {
|
|
|
|
format!("{}-{:02}-{:02}", now.year(), now.month(), now.day())
|
|
|
|
} else {
|
|
|
|
now.to_rfc3339_opts(SecondsFormat::Millis, true)
|
|
|
|
}
|
2023-09-16 20:05:50 +01:00
|
|
|
}
|
2023-09-17 15:25:17 +01:00
|
|
|
|
|
|
|
pub fn random_string(len: usize) -> String {
|
2025-02-19 00:26:54 +00:00
|
|
|
rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
|
2023-09-17 15:25:17 +01:00
|
|
|
}
|
2023-11-23 16:12:28 +00:00
|
|
|
|
2024-03-03 00:53:31 +00:00
|
|
|
/**
|
2024-03-03 12:49:55 +00:00
|
|
|
For any time ye need to check if a user who calls a command has admin privlages
|
|
|
|
*/
|
2025-02-19 00:17:02 +00:00
|
|
|
pub async fn is_admin(command: &CommandInteraction, ctx: &Context) -> Option<String> {
|
2024-03-03 00:53:31 +00:00
|
|
|
let mut admin = false;
|
|
|
|
|
|
|
|
let g_id = match command.guild_id {
|
|
|
|
None => return Some("Not in a server".to_string()),
|
|
|
|
Some(x) => x,
|
|
|
|
};
|
|
|
|
|
|
|
|
let roles_server = g_id.roles(&ctx.http).await.unwrap_or_default();
|
|
|
|
|
|
|
|
if let Ok(member) = g_id.member(&ctx.http, command.user.id).await {
|
|
|
|
if let Some(permissions) = member.permissions {
|
|
|
|
if permissions.administrator() {
|
|
|
|
admin = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for role_id in member.roles {
|
|
|
|
if admin {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if let Some(role) = roles_server.get(&role_id) {
|
|
|
|
if role.permissions.administrator() {
|
|
|
|
admin = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !admin {
|
2024-03-03 12:49:55 +00:00
|
|
|
Some("Administrator permission required".to_string())
|
2024-03-03 00:53:31 +00:00
|
|
|
} else {
|
2024-03-03 12:49:55 +00:00
|
|
|
None
|
2024-03-03 00:53:31 +00:00
|
|
|
}
|
2024-03-03 12:49:55 +00:00
|
|
|
}
|