From 84150cd82ede73a65f70dc62821e803ec8a7a30d Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Wed, 5 Feb 2020 17:51:14 -0500 Subject: [PATCH] Split youmubot-core --- Cargo.lock | 15 ++++++++++++--- Cargo.toml | 1 + youmubot-core/Cargo.toml | 17 +++++++++++++++++ .../src}/admin/mod.rs | 0 .../src}/admin/soft_ban.rs | 7 ++----- .../src}/community/mod.rs | 0 .../src}/community/votes.rs | 3 +-- {youmubot => youmubot-core}/src/db.rs | 10 ---------- .../src}/fun/images.rs | 2 +- .../commands => youmubot-core/src}/fun/mod.rs | 0 .../src}/fun/names.rs | 0 .../mod.rs => youmubot-core/src/lib.rs | 15 +++++++++++++++ youmubot/Cargo.toml | 6 +----- youmubot/src/main.rs | 18 ++++++------------ 14 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 youmubot-core/Cargo.toml rename {youmubot/src/commands => youmubot-core/src}/admin/mod.rs (100%) rename {youmubot/src/commands => youmubot-core/src}/admin/soft_ban.rs (97%) rename {youmubot/src/commands => youmubot-core/src}/community/mod.rs (100%) rename {youmubot/src/commands => youmubot-core/src}/community/votes.rs (99%) rename {youmubot => youmubot-core}/src/db.rs (78%) rename {youmubot/src/commands => youmubot-core/src}/fun/images.rs (98%) rename {youmubot/src/commands => youmubot-core/src}/fun/mod.rs (100%) rename {youmubot/src/commands => youmubot-core/src}/fun/names.rs (100%) rename youmubot/src/commands/mod.rs => youmubot-core/src/lib.rs (63%) diff --git a/Cargo.lock b/Cargo.lock index f3fabf8..3210d13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1677,15 +1677,24 @@ dependencies = [ name = "youmubot" version = "0.1.0" dependencies = [ - "chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serenity 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "youmubot-core 0.1.0", + "youmubot-db 0.1.0", + "youmubot-osu 0.1.0", + "youmubot-prelude 0.1.0", +] + +[[package]] +name = "youmubot-core" +version = "0.1.0" +dependencies = [ + "chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", - "reqwest 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "serenity 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "static_assertions 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "youmubot-db 0.1.0", - "youmubot-osu 0.1.0", "youmubot-prelude 0.1.0", ] diff --git a/Cargo.toml b/Cargo.toml index 3bc9602..828c358 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ members = [ "youmubot-prelude", "youmubot-db", + "youmubot-core", "youmubot-osu", "youmubot", ] diff --git a/youmubot-core/Cargo.toml b/youmubot-core/Cargo.toml new file mode 100644 index 0000000..407431c --- /dev/null +++ b/youmubot-core/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "youmubot-core" +version = "0.1.0" +authors = ["Natsu Kagami "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serenity = "0.8" +rand = "0.7" +serde = { version = "1", features = ["derive"] } +chrono = "0.4" +static_assertions = "1.1" + +youmubot-db = { path = "../youmubot-db" } +youmubot-prelude = { path = "../youmubot-prelude" } diff --git a/youmubot/src/commands/admin/mod.rs b/youmubot-core/src/admin/mod.rs similarity index 100% rename from youmubot/src/commands/admin/mod.rs rename to youmubot-core/src/admin/mod.rs diff --git a/youmubot/src/commands/admin/soft_ban.rs b/youmubot-core/src/admin/soft_ban.rs similarity index 97% rename from youmubot/src/commands/admin/soft_ban.rs rename to youmubot-core/src/admin/soft_ban.rs index 371272f..b8d6d25 100644 --- a/youmubot/src/commands/admin/soft_ban.rs +++ b/youmubot-core/src/admin/soft_ban.rs @@ -1,7 +1,4 @@ -use crate::{ - commands::args, - db::{ServerSoftBans, SoftBans}, -}; +use crate::db::{ServerSoftBans, SoftBans}; use chrono::offset::Utc; use serenity::{ framework::standard::{macros::command, Args, CommandError as Error, CommandResult}, @@ -114,7 +111,7 @@ pub fn soft_ban_init(ctx: &mut Context, msg: &Message, mut args: Args) -> Comman } // Watch the soft bans. -pub fn watch_soft_bans(client: &mut serenity::Client) -> impl FnOnce() -> () + 'static { +pub fn watch_soft_bans(client: &serenity::Client) -> impl FnOnce() -> () + 'static { let cache_http = { let cache_http = client.cache_and_http.clone(); let cache: serenity::cache::CacheRwLock = cache_http.cache.clone().into(); diff --git a/youmubot/src/commands/community/mod.rs b/youmubot-core/src/community/mod.rs similarity index 100% rename from youmubot/src/commands/community/mod.rs rename to youmubot-core/src/community/mod.rs diff --git a/youmubot/src/commands/community/votes.rs b/youmubot-core/src/community/votes.rs similarity index 99% rename from youmubot/src/commands/community/votes.rs rename to youmubot-core/src/community/votes.rs index 072c81c..b78f7fd 100644 --- a/youmubot/src/commands/community/votes.rs +++ b/youmubot-core/src/community/votes.rs @@ -1,4 +1,3 @@ -use crate::commands::args::Duration as ParseDuration; use serenity::framework::standard::CommandError as Error; use serenity::{ framework::standard::{macros::command, Args, CommandResult}, @@ -11,7 +10,7 @@ use serenity::{ use std::collections::HashMap as Map; use std::thread; use std::time::Duration; -use youmubot_prelude::*; +use youmubot_prelude::{Duration as ParseDuration, *}; #[command] #[description = "🎌 Cast a poll upon everyone and ask them for opinions!"] diff --git a/youmubot/src/db.rs b/youmubot-core/src/db.rs similarity index 78% rename from youmubot/src/db.rs rename to youmubot-core/src/db.rs index cb2250d..f7926a6 100644 --- a/youmubot/src/db.rs +++ b/youmubot-core/src/db.rs @@ -2,24 +2,14 @@ use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use serenity::{ - framework::standard::CommandError as Error, model::id::{RoleId, UserId}, }; use std::collections::HashMap; -use std::path::Path; use youmubot_db::{GuildMap, DB}; -use youmubot_prelude::*; /// A list of SoftBans for all servers. pub type SoftBans = DB>; -/// Sets up all databases in the client. -pub fn setup_db(path: &Path, data: &mut ShareMap) -> Result<(), Error> { - SoftBans::insert_into(&mut *data, &path.join("soft_bans.yaml"))?; - - Ok(()) -} - /// For the admin commands: /// - Each server might have a `soft ban` role implemented. /// - We allow periodical `soft ban` applications. diff --git a/youmubot/src/commands/fun/images.rs b/youmubot-core/src/fun/images.rs similarity index 98% rename from youmubot/src/commands/fun/images.rs rename to youmubot-core/src/fun/images.rs index ae19783..8c5c32e 100644 --- a/youmubot/src/commands/fun/images.rs +++ b/youmubot-core/src/fun/images.rs @@ -57,7 +57,7 @@ fn message_command(ctx: &mut Context, msg: &Message, args: Args, rating: Rating) // Gets an image URL. fn get_image( - client: &reqwest::blocking::Client, + client: &::Value, rating: Rating, tags: &str, ) -> Result, Error> { diff --git a/youmubot/src/commands/fun/mod.rs b/youmubot-core/src/fun/mod.rs similarity index 100% rename from youmubot/src/commands/fun/mod.rs rename to youmubot-core/src/fun/mod.rs diff --git a/youmubot/src/commands/fun/names.rs b/youmubot-core/src/fun/names.rs similarity index 100% rename from youmubot/src/commands/fun/names.rs rename to youmubot-core/src/fun/names.rs diff --git a/youmubot/src/commands/mod.rs b/youmubot-core/src/lib.rs similarity index 63% rename from youmubot/src/commands/mod.rs rename to youmubot-core/src/lib.rs index 4562bc5..b039c75 100644 --- a/youmubot/src/commands/mod.rs +++ b/youmubot-core/src/lib.rs @@ -9,12 +9,27 @@ use youmubot_prelude::*; pub mod admin; pub mod community; +mod db; pub mod fun; pub use admin::ADMIN_GROUP; pub use community::COMMUNITY_GROUP; pub use fun::FUN_GROUP; +/// Sets up all databases in the client. +pub fn setup( + path: &std::path::Path, + client: &serenity::client::Client, + data: &mut youmubot_prelude::ShareMap, +) -> serenity::framework::standard::CommandResult { + db::SoftBans::insert_into(&mut *data, &path.join("soft_bans.yaml"))?; + + // Create handler threads + std::thread::spawn(admin::watch_soft_bans(client)); + + Ok(()) +} + // A help command #[help] pub fn help( diff --git a/youmubot/Cargo.toml b/youmubot/Cargo.toml index f263819..da96075 100644 --- a/youmubot/Cargo.toml +++ b/youmubot/Cargo.toml @@ -9,12 +9,8 @@ edition = "2018" [dependencies] serenity = "0.8" dotenv = "0.15" -serde = { version = "1.0", features = ["derive"] } -chrono = "0.4.9" -rand = "0.7.2" -static_assertions = "1.1.0" -reqwest = "0.10.1" youmubot-osu = { path = "../youmubot-osu" } youmubot-db = { path = "../youmubot-db" } youmubot-prelude = { path = "../youmubot-prelude" } +youmubot-core = { path = "../youmubot-core" } diff --git a/youmubot/src/main.rs b/youmubot/src/main.rs index e156192..dadc921 100644 --- a/youmubot/src/main.rs +++ b/youmubot/src/main.rs @@ -7,9 +7,6 @@ use serenity::{ use youmubot_osu::discord::{setup as setup_osu, OSU_GROUP}; use youmubot_prelude::*; -mod commands; -mod db; - const MESSAGE_HOOKS: [fn(&mut Context, &Message) -> (); 1] = [youmubot_osu::discord::hook]; struct Handler; @@ -51,15 +48,12 @@ fn main() { std::path::PathBuf::from("data") }); youmubot_prelude::setup::setup_prelude(&db_path, &mut data, &mut fw); - // Setup initial data - db::setup_db(&db_path, &mut data).expect("Setup db should succeed"); + // Setup 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"); } - // Create handler threads - std::thread::spawn(commands::admin::watch_soft_bans(&mut client)); - println!("Starting..."); if let Err(v) = client.start() { panic!(v) @@ -85,7 +79,7 @@ fn setup_framework(client: &Client) -> StandardFramework { .delimiters(vec![" / ", "/ ", " /", "/"]) .owners([owner.id].iter().cloned().collect()) }) - .help(&commands::HELP) + .help(&youmubot_core::HELP) .before(|_, msg, command_name| { println!( "Got command '{}' by user '{}'", @@ -134,8 +128,8 @@ fn setup_framework(client: &Client) -> StandardFramework { c.delay(30).time_span(30).limit(1) }) // groups here - .group(&commands::ADMIN_GROUP) - .group(&commands::FUN_GROUP) - .group(&commands::COMMUNITY_GROUP) + .group(&youmubot_core::ADMIN_GROUP) + .group(&youmubot_core::FUN_GROUP) + .group(&youmubot_core::COMMUNITY_GROUP) .group(&OSU_GROUP) }