From 03be1a4acc20c31f9eae4d2d67749cd1ad75e8cf Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Wed, 5 Feb 2020 16:21:11 -0500 Subject: [PATCH] Move the prelude into a seperate package --- Cargo.lock | 10 ++++++ Cargo.toml | 1 + youmubot-prelude/Cargo.toml | 12 +++++++ .../src}/announcer.rs | 8 +++-- .../commands => youmubot-prelude/src}/args.rs | 0 .../prelude.rs => youmubot-prelude/src/lib.rs | 21 +++++++----- youmubot-prelude/src/setup.rs | 14 ++++++++ youmubot/Cargo.toml | 1 + youmubot/src/commands/admin/mod.rs | 2 +- youmubot/src/commands/admin/soft_ban.rs | 2 +- youmubot/src/commands/community/mod.rs | 2 +- youmubot/src/commands/community/votes.rs | 2 +- youmubot/src/commands/fun/images.rs | 2 +- youmubot/src/commands/fun/mod.rs | 2 +- youmubot/src/commands/mod.rs | 5 +-- youmubot/src/commands/osu/announcer.rs | 7 ++-- youmubot/src/commands/osu/hook.rs | 2 +- youmubot/src/commands/osu/mod.rs | 2 +- youmubot/src/db.rs | 5 +-- youmubot/src/main.rs | 32 ++++++++++++------- 20 files changed, 88 insertions(+), 44 deletions(-) create mode 100644 youmubot-prelude/Cargo.toml rename {youmubot/src/commands => youmubot-prelude/src}/announcer.rs (93%) rename {youmubot/src/commands => youmubot-prelude/src}/args.rs (100%) rename youmubot/src/prelude.rs => youmubot-prelude/src/lib.rs (80%) create mode 100644 youmubot-prelude/src/setup.rs diff --git a/Cargo.lock b/Cargo.lock index 5295786..8828ed8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1689,6 +1689,7 @@ dependencies = [ "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", ] [[package]] @@ -1715,6 +1716,15 @@ dependencies = [ "serenity 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "youmubot-prelude" +version = "0.1.0" +dependencies = [ + "reqwest 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serenity 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "youmubot-db 0.1.0", +] + [metadata] "checksum adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2" "checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d" diff --git a/Cargo.toml b/Cargo.toml index 1be4f54..3bc9602 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ + "youmubot-prelude", "youmubot-db", "youmubot-osu", "youmubot", diff --git a/youmubot-prelude/Cargo.toml b/youmubot-prelude/Cargo.toml new file mode 100644 index 0000000..5e8e18b --- /dev/null +++ b/youmubot-prelude/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "youmubot-prelude" +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" +youmubot-db = { path = "../youmubot-db" } +reqwest = "0.10" diff --git a/youmubot/src/commands/announcer.rs b/youmubot-prelude/src/announcer.rs similarity index 93% rename from youmubot/src/commands/announcer.rs rename to youmubot-prelude/src/announcer.rs index d36d940..47e7400 100644 --- a/youmubot/src/commands/announcer.rs +++ b/youmubot-prelude/src/announcer.rs @@ -1,14 +1,16 @@ -use crate::db::AnnouncerChannels; -use crate::prelude::*; +use crate::AppData; use serenity::{ framework::standard::{CommandError as Error, CommandResult}, http::{CacheHttp, Http}, model::id::{ChannelId, GuildId, UserId}, }; use std::{ - collections::HashSet, + collections::{HashMap, HashSet}, thread::{spawn, JoinHandle}, }; +use youmubot_db::DB; + +pub(crate) type AnnouncerChannels = DB>>; pub trait Announcer { fn announcer_key() -> &'static str; diff --git a/youmubot/src/commands/args.rs b/youmubot-prelude/src/args.rs similarity index 100% rename from youmubot/src/commands/args.rs rename to youmubot-prelude/src/args.rs diff --git a/youmubot/src/prelude.rs b/youmubot-prelude/src/lib.rs similarity index 80% rename from youmubot/src/prelude.rs rename to youmubot-prelude/src/lib.rs index dc98a94..c391553 100644 --- a/youmubot/src/prelude.rs +++ b/youmubot-prelude/src/lib.rs @@ -1,24 +1,29 @@ -use std::sync::Arc; -use youmubot_osu::Client as OsuHttpClient; - pub use serenity::prelude::*; +use std::sync::Arc; + +pub mod announcer; +pub mod args; +pub mod setup; + +pub use announcer::Announcer; +pub use args::Duration; /// The global app data. pub type AppData = Arc>; /// The HTTP client. -pub(crate) struct HTTPClient; +pub struct HTTPClient; impl TypeMapKey for HTTPClient { type Value = reqwest::blocking::Client; } /// The osu! client. -pub(crate) struct OsuClient; +// pub(crate) struct OsuClient; -impl TypeMapKey for OsuClient { - type Value = OsuHttpClient; -} +// impl TypeMapKey for OsuClient { +// type Value = OsuHttpClient; +// } /// The TypeMap trait that allows TypeMaps to quickly get a clonable item. pub trait GetCloned { diff --git a/youmubot-prelude/src/setup.rs b/youmubot-prelude/src/setup.rs new file mode 100644 index 0000000..4aadd37 --- /dev/null +++ b/youmubot-prelude/src/setup.rs @@ -0,0 +1,14 @@ +use serenity::{framework::standard::StandardFramework, prelude::*}; +use std::path::Path; + +/// Set up the prelude libraries. +/// +/// Panics on failure: Youmubot should *NOT* attempt to continue when this function fails. +pub fn setup_prelude(db_path: &Path, data: &mut ShareMap, _: &mut StandardFramework) { + // Setup the announcer DB. + crate::announcer::AnnouncerChannels::insert_into(data, db_path.join("announcers.yaml")) + .expect("Announcers DB set up"); + + data.insert::(reqwest::blocking::Client::new()) + .expect("Should be able to insert"); +} diff --git a/youmubot/Cargo.toml b/youmubot/Cargo.toml index 3114212..40b31d1 100644 --- a/youmubot/Cargo.toml +++ b/youmubot/Cargo.toml @@ -19,4 +19,5 @@ lazy_static = "1" youmubot-osu = { path = "../youmubot-osu" } rayon = "1.1" youmubot-db = { path = "../youmubot-db" } +youmubot-prelude = { path = "../youmubot-prelude" } diff --git a/youmubot/src/commands/admin/mod.rs b/youmubot/src/commands/admin/mod.rs index c63a979..12849da 100644 --- a/youmubot/src/commands/admin/mod.rs +++ b/youmubot/src/commands/admin/mod.rs @@ -1,4 +1,3 @@ -use serenity::prelude::*; use serenity::{ framework::standard::{ macros::{command, group}, @@ -11,6 +10,7 @@ use serenity::{ }; use soft_ban::{SOFT_BAN_COMMAND, SOFT_BAN_INIT_COMMAND}; use std::{thread::sleep, time::Duration}; +use youmubot_prelude::*; mod soft_ban; pub use soft_ban::watch_soft_bans; diff --git a/youmubot/src/commands/admin/soft_ban.rs b/youmubot/src/commands/admin/soft_ban.rs index 24f48ad..371272f 100644 --- a/youmubot/src/commands/admin/soft_ban.rs +++ b/youmubot/src/commands/admin/soft_ban.rs @@ -3,7 +3,6 @@ use crate::{ db::{ServerSoftBans, SoftBans}, }; use chrono::offset::Utc; -use serenity::prelude::*; use serenity::{ framework::standard::{macros::command, Args, CommandError as Error, CommandResult}, model::{ @@ -12,6 +11,7 @@ use serenity::{ }, }; use std::cmp::max; +use youmubot_prelude::*; #[command] #[required_permissions(ADMINISTRATOR)] diff --git a/youmubot/src/commands/community/mod.rs b/youmubot/src/commands/community/mod.rs index 0886bcf..4d90da4 100644 --- a/youmubot/src/commands/community/mod.rs +++ b/youmubot/src/commands/community/mod.rs @@ -2,7 +2,6 @@ use rand::{ distributions::{Distribution, Uniform}, thread_rng, }; -use serenity::prelude::*; use serenity::{ framework::standard::{ macros::{command, group}, @@ -14,6 +13,7 @@ use serenity::{ }, utils::MessageBuilder, }; +use youmubot_prelude::*; mod votes; diff --git a/youmubot/src/commands/community/votes.rs b/youmubot/src/commands/community/votes.rs index 67de2e8..072c81c 100644 --- a/youmubot/src/commands/community/votes.rs +++ b/youmubot/src/commands/community/votes.rs @@ -1,6 +1,5 @@ use crate::commands::args::Duration as ParseDuration; use serenity::framework::standard::CommandError as Error; -use serenity::prelude::*; use serenity::{ framework::standard::{macros::command, Args, CommandResult}, model::{ @@ -12,6 +11,7 @@ use serenity::{ use std::collections::HashMap as Map; use std::thread; use std::time::Duration; +use youmubot_prelude::*; #[command] #[description = "🎌 Cast a poll upon everyone and ask them for opinions!"] diff --git a/youmubot/src/commands/fun/images.rs b/youmubot/src/commands/fun/images.rs index 0898a29..ae19783 100644 --- a/youmubot/src/commands/fun/images.rs +++ b/youmubot/src/commands/fun/images.rs @@ -1,4 +1,3 @@ -use crate::prelude::*; use serde::Deserialize; use serenity::framework::standard::CommandError as Error; use serenity::{ @@ -9,6 +8,7 @@ use serenity::{ model::channel::{Channel, Message}, }; use std::string::ToString; +use youmubot_prelude::*; #[command] #[checks(nsfw)] diff --git a/youmubot/src/commands/fun/mod.rs b/youmubot/src/commands/fun/mod.rs index a61fb02..57640fc 100644 --- a/youmubot/src/commands/fun/mod.rs +++ b/youmubot/src/commands/fun/mod.rs @@ -2,7 +2,6 @@ use rand::{ distributions::{Distribution, Uniform}, thread_rng, }; -use serenity::prelude::*; use serenity::{ framework::standard::{ macros::{command, group}, @@ -11,6 +10,7 @@ use serenity::{ model::{channel::Message, id::UserId}, utils::MessageBuilder, }; +use youmubot_prelude::*; mod images; mod names; diff --git a/youmubot/src/commands/mod.rs b/youmubot/src/commands/mod.rs index 0160e99..3506467 100644 --- a/youmubot/src/commands/mod.rs +++ b/youmubot/src/commands/mod.rs @@ -1,4 +1,3 @@ -use serenity::prelude::*; use serenity::{ framework::standard::{ help_commands, macros::help, Args, CommandGroup, CommandResult, HelpOptions, @@ -6,9 +5,7 @@ use serenity::{ model::{channel::Message, id::UserId}, }; use std::collections::HashSet; - -mod announcer; -mod args; +use youmubot_prelude::*; pub mod admin; pub mod community; diff --git a/youmubot/src/commands/osu/announcer.rs b/youmubot/src/commands/osu/announcer.rs index a0c80d7..781a105 100644 --- a/youmubot/src/commands/osu/announcer.rs +++ b/youmubot/src/commands/osu/announcer.rs @@ -1,9 +1,5 @@ use super::{embeds::score_embed, BeatmapWithMode}; -use crate::{ - commands::announcer::Announcer, - db::{OsuSavedUsers, OsuUser}, - prelude::*, -}; +use crate::db::{OsuSavedUsers, OsuUser}; use rayon::prelude::*; use serenity::{ framework::standard::{CommandError as Error, CommandResult}, @@ -15,6 +11,7 @@ use youmubot_osu::{ request::{BeatmapRequestKind, UserID}, Client as Osu, }; +use youmubot_prelude::*; /// Announce osu! top scores. pub struct OsuAnnouncer; diff --git a/youmubot/src/commands/osu/hook.rs b/youmubot/src/commands/osu/hook.rs index 2b3bf03..0f1b8f0 100644 --- a/youmubot/src/commands/osu/hook.rs +++ b/youmubot/src/commands/osu/hook.rs @@ -1,4 +1,3 @@ -use crate::prelude::*; use lazy_static::lazy_static; use regex::Regex; use serenity::{ @@ -11,6 +10,7 @@ use youmubot_osu::{ models::{Beatmap, Mode}, request::BeatmapRequestKind, }; +use youmubot_prelude::*; use super::embeds::{beatmap_embed, beatmapset_embed}; diff --git a/youmubot/src/commands/osu/mod.rs b/youmubot/src/commands/osu/mod.rs index 4f63d2d..d33892a 100644 --- a/youmubot/src/commands/osu/mod.rs +++ b/youmubot/src/commands/osu/mod.rs @@ -1,5 +1,4 @@ use crate::db::{OsuSavedUsers, OsuUser}; -use crate::prelude::*; use serenity::{ framework::standard::{ macros::{command, group}, @@ -13,6 +12,7 @@ use youmubot_osu::{ models::{Beatmap, Mode, User}, request::{BeatmapRequestKind, UserID}, }; +use youmubot_prelude::*; mod announcer; mod cache; diff --git a/youmubot/src/db.rs b/youmubot/src/db.rs index c6b7a1d..21ec47a 100644 --- a/youmubot/src/db.rs +++ b/youmubot/src/db.rs @@ -12,9 +12,6 @@ use std::path::PathBuf; use youmubot_db::{GuildMap, DB}; use youmubot_osu::models::{Beatmap, Mode}; -/// A map from announcer keys to guild IDs and to channels. -pub type AnnouncerChannels = DB>>; - /// A list of SoftBans for all servers. pub type SoftBans = DB>; @@ -34,7 +31,7 @@ pub fn setup_db(client: &mut Client) -> Result<(), Error> { SoftBans::insert_into(&mut *data, &path.join("soft_bans.yaml"))?; OsuSavedUsers::insert_into(&mut *data, &path.join("osu_saved_users.yaml"))?; OsuLastBeatmap::insert_into(&mut *data, &path.join("last_beatmaps.yaml"))?; - AnnouncerChannels::insert_into(&mut *data, &path.join("announcers.yaml"))?; + // AnnouncerChannels::insert_into(&mut *data, &path.join("announcers.yaml"))?; Ok(()) } diff --git a/youmubot/src/main.rs b/youmubot/src/main.rs index 5227392..c8c3f9a 100644 --- a/youmubot/src/main.rs +++ b/youmubot/src/main.rs @@ -1,19 +1,16 @@ use dotenv; use dotenv::var; -use reqwest; use serenity::{ framework::standard::{DispatchError, StandardFramework}, model::{channel::Message, gateway}, }; use youmubot_osu::Client as OsuApiClient; +use youmubot_prelude::*; mod commands; mod db; -mod prelude; use commands::osu::OsuAnnouncer; -use commands::Announcer; -use prelude::*; const MESSAGE_HOOKS: [fn(&mut Context, &Message) -> (); 1] = [commands::osu::hook]; @@ -40,16 +37,30 @@ fn main() { // 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 - setup_framework(Client::new(token, Handler).expect("Cannot connect...")) + Client::new(token, Handler).expect("Cannot connect") }; + // Set up base framework + let mut fw = setup_framework(&client); + + // Setup each package starting from the prelude. + { + let mut data = client.data.write(); + let db_path = var("DBPATH") + .map(|v| std::path::PathBuf::from(v)) + .unwrap_or_else(|e| { + println!("No DBPATH set up ({:?}), using `/data`", e); + std::path::PathBuf::from("data") + }); + youmubot_prelude::setup::setup_prelude(&db_path, &mut data, &mut fw); + } + // Setup initial data db::setup_db(&mut client).expect("Setup db should succeed"); // Setup shared instances of things { let mut data = client.data.write(); - let http_client = reqwest::blocking::Client::new(); - data.insert::(http_client.clone()); + let http_client = data.get_cloned::(); data.insert::(OsuApiClient::new( http_client.clone(), var("OSU_API_KEY").expect("Please set OSU_API_KEY as osu! api key."), @@ -71,7 +82,7 @@ fn main() { } // Sets up a framework for a client -fn setup_framework(mut client: Client) -> Client { +fn setup_framework(client: &Client) -> StandardFramework { // Collect owners let owner = client .cache_and_http @@ -80,8 +91,7 @@ fn setup_framework(mut client: Client) -> Client { .expect("Should be able to get app info") .owner; - client.with_framework( - StandardFramework::new() + StandardFramework::new() .configure(|c| { c.with_whitespace(false) .prefix("y!") @@ -141,6 +151,4 @@ fn setup_framework(mut client: Client) -> Client { .group(&commands::FUN_GROUP) .group(&commands::COMMUNITY_GROUP) .group(&commands::OSU_GROUP) - ); - client }