From c4ce365555d11e83688bebee9030957be8f4ef2d Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Wed, 5 Feb 2020 18:39:38 -0500 Subject: [PATCH 1/4] Enable feature flags --- youmubot/Cargo.toml | 6 +++++- youmubot/src/main.rs | 50 ++++++++++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/youmubot/Cargo.toml b/youmubot/Cargo.toml index da96075..5d5469a 100644 --- a/youmubot/Cargo.toml +++ b/youmubot/Cargo.toml @@ -5,12 +5,16 @@ authors = ["Natsu Kagami "] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = ["core", "osu"] +core = [] +osu = ["youmubot-osu"] [dependencies] serenity = "0.8" dotenv = "0.15" -youmubot-osu = { path = "../youmubot-osu" } youmubot-db = { path = "../youmubot-db" } youmubot-prelude = { path = "../youmubot-prelude" } youmubot-core = { path = "../youmubot-core" } +youmubot-osu = { path = "../youmubot-osu", optional = true } diff --git a/youmubot/src/main.rs b/youmubot/src/main.rs index dadc921..fb26363 100644 --- a/youmubot/src/main.rs +++ b/youmubot/src/main.rs @@ -4,12 +4,17 @@ use serenity::{ framework::standard::{DispatchError, StandardFramework}, model::{channel::Message, gateway}, }; -use youmubot_osu::discord::{setup as setup_osu, OSU_GROUP}; use youmubot_prelude::*; -const MESSAGE_HOOKS: [fn(&mut Context, &Message) -> (); 1] = [youmubot_osu::discord::hook]; +struct Handler { + hooks: Vec ()>, +} -struct Handler; +impl Handler { + fn new() -> Handler { + Handler { hooks: vec![] } + } +} impl EventHandler for Handler { fn ready(&self, _: Context, ready: gateway::Ready) { @@ -17,7 +22,8 @@ impl EventHandler for Handler { } fn message(&self, mut ctx: Context, message: Message) { - MESSAGE_HOOKS.iter().for_each(|f| f(&mut ctx, &message)); + println!("{:?}", message); + self.hooks.iter().for_each(|f| f(&mut ctx, &message)); } } @@ -27,12 +33,17 @@ fn main() { println!("Loaded dotenv from {:?}", path); } + let mut handler = Handler::new(); + // Set up hooks + #[cfg(feature = "osu")] + handler.hooks.push(youmubot_osu::discord::hook); + // Sets up a client let mut client = { // Collect the token let token = var("TOKEN").expect("Please set TOKEN as the Discord Bot's token to be used."); // Attempt to connect and set up a framework - Client::new(token, Handler).expect("Cannot connect") + Client::new(token, handler).expect("Cannot connect") }; // Set up base framework @@ -49,11 +60,20 @@ fn main() { }); youmubot_prelude::setup::setup_prelude(&db_path, &mut data, &mut fw); // Setup core + #[cfg(feature = "core")] youmubot_core::setup(&db_path, &client, &mut data).expect("Setup db should succeed"); // osu! - setup_osu(&db_path, &client, &mut data).expect("osu! is initialized"); + #[cfg(feature = "osu")] + youmubot_osu::discord::setup(&db_path, &client, &mut data).expect("osu! is initialized"); } + #[cfg(feature = "core")] + println!("Core enabled."); + #[cfg(feature = "osu")] + println!("osu! enabled."); + + client.with_framework(fw); + println!("Starting..."); if let Err(v) = client.start() { panic!(v) @@ -72,7 +92,7 @@ fn setup_framework(client: &Client) -> StandardFramework { .expect("Should be able to get app info") .owner; - StandardFramework::new() + let fw = StandardFramework::new() .configure(|c| { c.with_whitespace(false) .prefix("y!") @@ -126,10 +146,14 @@ fn setup_framework(client: &Client) -> StandardFramework { .bucket("images", |c| c.time_span(60).limit(2)) .bucket("community", |c| { c.delay(30).time_span(30).limit(1) - }) - // groups here - .group(&youmubot_core::ADMIN_GROUP) - .group(&youmubot_core::FUN_GROUP) - .group(&youmubot_core::COMMUNITY_GROUP) - .group(&OSU_GROUP) + }); + // groups here + #[cfg(feature = "core")] + let fw = fw + .group(&youmubot_core::ADMIN_GROUP) + .group(&youmubot_core::FUN_GROUP) + .group(&youmubot_core::COMMUNITY_GROUP); + #[cfg(feature = "osu")] + let fw = fw.group(&youmubot_osu::discord::OSU_GROUP); + fw } From ed8346abb909e044981ae8ef33a96aa907df820f Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Wed, 5 Feb 2020 18:49:28 -0500 Subject: [PATCH 2/4] Optional prefix --- youmubot/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/youmubot/src/main.rs b/youmubot/src/main.rs index fb26363..54e765b 100644 --- a/youmubot/src/main.rs +++ b/youmubot/src/main.rs @@ -95,7 +95,7 @@ fn setup_framework(client: &Client) -> StandardFramework { let fw = StandardFramework::new() .configure(|c| { c.with_whitespace(false) - .prefix("y!") + .prefix(&var("PREFIX").unwrap_or("y!".to_owned())) .delimiters(vec![" / ", "/ ", " /", "/"]) .owners([owner.id].iter().cloned().collect()) }) From 581b0390325cd52cafbc2f0a29068089b6e7d553 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Thu, 6 Feb 2020 11:55:04 -0500 Subject: [PATCH 3/4] Write db on WriteGuard drop --- youmubot-db/src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/youmubot-db/src/lib.rs b/youmubot-db/src/lib.rs index ccf8576..974fc60 100644 --- a/youmubot-db/src/lib.rs +++ b/youmubot-db/src/lib.rs @@ -64,3 +64,14 @@ where (*self).0.borrow_data_mut() } } + +impl Drop for DBWriteGuard +where + T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned, +{ + fn drop(&mut self) { + if let Err(e) = self.0.save() { + dbg!(e); + } + } +} From ac4e60c5962459144bbd5a9a8eb146b17cc6d3f8 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Thu, 6 Feb 2020 11:55:39 -0500 Subject: [PATCH 4/4] Show beatmapset link as a bracketed anchor --- youmubot-osu/src/discord/embeds.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/youmubot-osu/src/discord/embeds.rs b/youmubot-osu/src/discord/embeds.rs index d2318a7..4e1b39f 100644 --- a/youmubot-osu/src/discord/embeds.rs +++ b/youmubot-osu/src/discord/embeds.rs @@ -79,7 +79,7 @@ pub fn beatmap_embed<'a>(b: &'_ Beatmap, m: Mode, c: &'a mut CreateEmbed) -> &'a link, link ) }) - .push_line(b.beatmapset_link()) + .push_line(format!(" [[Beatmapset]]({})", b.beatmapset_link())) .push_line(&b.approval) .push("Language: ") .push_bold(&b.language)