Move the prelude into a seperate package

This commit is contained in:
Natsu Kagami 2020-02-05 16:21:11 -05:00
parent d5f7a17a2c
commit 03be1a4acc
20 changed files with 88 additions and 44 deletions

10
Cargo.lock generated
View file

@ -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"

View file

@ -1,6 +1,7 @@
[workspace]
members = [
"youmubot-prelude",
"youmubot-db",
"youmubot-osu",
"youmubot",

View file

@ -0,0 +1,12 @@
[package]
name = "youmubot-prelude"
version = "0.1.0"
authors = ["Natsu Kagami <natsukagami@gmail.com>"]
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"

View file

@ -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<HashMap<String, HashMap<GuildId, ChannelId>>>;
pub trait Announcer {
fn announcer_key() -> &'static str;

View file

@ -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<RwLock<ShareMap>>;
/// 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 {

View file

@ -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::<crate::HTTPClient>(reqwest::blocking::Client::new())
.expect("Should be able to insert");
}

View file

@ -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" }

View file

@ -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;

View file

@ -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)]

View file

@ -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;

View file

@ -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!"]

View file

@ -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)]

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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};

View file

@ -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;

View file

@ -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<HashMap<String, GuildMap<ChannelId>>>;
/// A list of SoftBans for all servers.
pub type SoftBans = DB<GuildMap<ServerSoftBans>>;
@ -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(())
}

View file

@ -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::<HTTPClient>(http_client.clone());
let http_client = data.get_cloned::<HTTPClient>();
data.insert::<OsuClient>(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
}