diff --git a/src/main.rs b/src/main.rs index f416eb9..099b5c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use config::{Config, File}; use glob::glob; use lazy_static::lazy_static; -use log::LevelFilter; +use log::{info, LevelFilter}; use poise::serenity_prelude as serenity; use poise::serenity_prelude::UserId; use regex::Regex; @@ -18,7 +18,7 @@ mod commands; // Custom user data passed to all command functions pub struct Data { - database: sqlx::SqlitePool, + // database: sqlx::SqlitePool, } lazy_static! { @@ -34,17 +34,29 @@ lazy_static! { // required_permissions = "MANAGE_MESSAGES | ADMINISTRATOR", owners_only=true, )] -async fn register(ctx: Context<'_>, #[flag] global: bool) -> Result<(), Error> { +async fn register( + ctx: Context<'_>, + #[flag] global: bool, + #[flag] purge: bool, + #[rest] rest: String, +) -> Result<(), Error> { // poise::builtins::register_application_commands(ctx, global).await?; let mut commands_builder = serenity::CreateApplicationCommands::default(); let commands = &ctx.framework().options().commands; + ctx.say(format!("Commands: {}", commands.len())).await?; + let mut command_count: u8 = 0; for command in commands { - if let Some(slash_command) = command.create_as_slash_command() { - commands_builder.add_application_command(slash_command); - } - if let Some(context_menu_command) = command.create_as_context_menu_command() { - commands_builder.add_application_command(context_menu_command); + command_count += 1; + if rest.eq_ignore_ascii_case("all") || rest.contains(&command.name) { + if let Some(slash_command) = command.create_as_slash_command() { + commands_builder.add_application_command(slash_command); + } + if let Some(context_menu_command) = command.create_as_context_menu_command() { + commands_builder.add_application_command(context_menu_command); + } + } else { + info!("Not adding command {}", command.name); } } let commands_builder = serenity::json::Value::Array(commands_builder.0); @@ -55,13 +67,27 @@ async fn register(ctx: Context<'_>, #[flag] global: bool) -> Result<(), Error> { ctx.say("Can only be used by bot owner").await?; return Ok(()); } - - ctx.say(format!("Registering {} commands...", commands.len())) + if purge { + ctx.say(format!( + "Purging global commands, must have been a mistake eh?" + )) .await?; - ctx.discord() - .http - .create_global_application_commands(&commands_builder) + ctx.discord() + .http + .create_global_application_commands(&serenity::json::Value::Null) + .await?; + } else { + ctx.say(format!( + "Registering {} commands out of {}...", + command_count, + commands.len() + )) .await?; + ctx.discord() + .http + .create_global_application_commands(&commands_builder) + .await?; + } } else { let guild = match ctx.guild() { Some(x) => x, @@ -77,8 +103,12 @@ async fn register(ctx: Context<'_>, #[flag] global: bool) -> Result<(), Error> { return Ok(()); } - ctx.say(format!("Registering {} commands...", commands.len())) - .await?; + ctx.say(format!( + "Registering {} commands out of {}...", + command_count, + commands.len() + )) + .await?; ctx.discord() .http .create_guild_application_commands(guild.id.0, &commands_builder) @@ -130,22 +160,32 @@ This bot is very much a work in progress. Commands will change and may break.", } fn setup_config() -> Result<(), Error> { - SETTINGS - .write() - .unwrap() - .merge( + let mut setting = SETTINGS.write().unwrap(); + *setting = Config::builder() + .add_source( glob("data/config/*") .unwrap() .map(|path| File::from(path.unwrap())) .collect::>(), ) + .build() .unwrap(); - println!("{:#?}", SETTINGS.read()); Ok(()) } async fn app() -> Result<(), Error> { + let commands = vec![ + help(), + register(), + commands::invites::invites(), + commands::schedule::schedule(), + commands::boop(), + commands::fix(), + // commands::planning::get_command(), + commands::get_links(), + ]; + let discord = SETTINGS.read().unwrap().get_table("discord").unwrap(); let mut owners: HashSet = HashSet::new(); @@ -155,19 +195,10 @@ async fn app() -> Result<(), Error> { .get("prefix") .expect("Config error, please set the discord[token] value") .to_owned() - .try_into() + .into_string() .expect("Config error, please make sure discord[token] is a string"); let options = poise::FrameworkOptions { - commands: vec![ - help(), - register(), - commands::invites::invites(), - commands::schedule::schedule(), - commands::boop(), - commands::fix(), - commands::planning::get_command(), - commands::get_links(), - ], + commands: commands, prefix_options: poise::PrefixFrameworkOptions { prefix: Some(prefix.into()), edit_tracker: Some(poise::EditTracker::for_timespan( @@ -199,28 +230,33 @@ async fn app() -> Result<(), Error> { .get("token") .expect("Config error, please set the discord[token] value") .clone() - .into_str() + .into_string() .expect("Config error, please make sure discord[token] is a string"); - let database = sqlx::sqlite::SqlitePoolOptions::new() - .max_connections(5) - .connect_with( - "sqlite:data/database.db" - .parse::()? - .create_if_missing(true), - ) - .await?; - sqlx::migrate!("./migrations").run(&database).await?; + // let database = sqlx::sqlite::SqlitePoolOptions::new() + // .max_connections(5) + // .connect_with( + // "sqlite:data/database.db" + // .parse::()? + // .create_if_missing(true), + // ) + // .await?; + // sqlx::migrate!("./migrations").run(&database).await?; - poise::Framework::build() + // let framework = poise::Framework::builder().options(options).token(token) + + let framework = poise::Framework::builder() .token(token) .user_data_setup(move |_ctx, _ready, _framework| { - Box::pin(async move { Ok(Data { database: database }) }) + // Box::pin(async move { Ok(Data { database: database }) }) + Box::pin(async move { Ok(Data {}) }) }) .options(options) - .run() - .await - .unwrap(); + .intents( + serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT, + ); + + framework.run().await.unwrap(); Ok(()) } @@ -239,7 +275,7 @@ async fn main() { .unwrap_or_default() .get("level") { - Some(val) => match val.clone().into_str() { + Some(val) => match val.clone().into_string() { Ok(level) => match level.to_lowercase().as_str() { "trace" => LevelFilter::Trace, "debug" => LevelFilter::Debug,