Merge branch 'master' into codeforces

This commit is contained in:
Natsu Kagami 2020-02-06 12:04:50 -05:00
commit 3ad172a5b6
Signed by: nki
GPG key ID: 73376E117CD20735
4 changed files with 55 additions and 16 deletions

View file

@ -64,3 +64,14 @@ where
(*self).0.borrow_data_mut() (*self).0.borrow_data_mut()
} }
} }
impl<T> Drop for DBWriteGuard<T>
where
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
{
fn drop(&mut self) {
if let Err(e) = self.0.save() {
dbg!(e);
}
}
}

View file

@ -79,7 +79,7 @@ pub fn beatmap_embed<'a>(b: &'_ Beatmap, m: Mode, c: &'a mut CreateEmbed) -> &'a
link, link link, link
) )
}) })
.push_line(b.beatmapset_link()) .push_line(format!(" [[Beatmapset]]({})", b.beatmapset_link()))
.push_line(&b.approval) .push_line(&b.approval)
.push("Language: ") .push("Language: ")
.push_bold(&b.language) .push_bold(&b.language)

View file

@ -5,12 +5,16 @@ authors = ["Natsu Kagami <natsukagami@gmail.com>"]
edition = "2018" edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # 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] [dependencies]
serenity = "0.8" serenity = "0.8"
dotenv = "0.15" dotenv = "0.15"
youmubot-osu = { path = "../youmubot-osu" }
youmubot-db = { path = "../youmubot-db" } youmubot-db = { path = "../youmubot-db" }
youmubot-prelude = { path = "../youmubot-prelude" } youmubot-prelude = { path = "../youmubot-prelude" }
youmubot-core = { path = "../youmubot-core" } youmubot-core = { path = "../youmubot-core" }
youmubot-osu = { path = "../youmubot-osu", optional = true }

View file

@ -4,12 +4,17 @@ use serenity::{
framework::standard::{DispatchError, StandardFramework}, framework::standard::{DispatchError, StandardFramework},
model::{channel::Message, gateway}, model::{channel::Message, gateway},
}; };
use youmubot_osu::discord::{setup as setup_osu, OSU_GROUP};
use youmubot_prelude::*; use youmubot_prelude::*;
const MESSAGE_HOOKS: [fn(&mut Context, &Message) -> (); 1] = [youmubot_osu::discord::hook]; struct Handler {
hooks: Vec<fn(&mut Context, &Message) -> ()>,
}
struct Handler; impl Handler {
fn new() -> Handler {
Handler { hooks: vec![] }
}
}
impl EventHandler for Handler { impl EventHandler for Handler {
fn ready(&self, _: Context, ready: gateway::Ready) { fn ready(&self, _: Context, ready: gateway::Ready) {
@ -17,7 +22,8 @@ impl EventHandler for Handler {
} }
fn message(&self, mut ctx: Context, message: Message) { 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); 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 // Sets up a client
let mut client = { let mut client = {
// Collect the token // Collect the token
let token = var("TOKEN").expect("Please set TOKEN as the Discord Bot's token to be used."); 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 // 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 // Set up base framework
@ -49,11 +60,20 @@ fn main() {
}); });
youmubot_prelude::setup::setup_prelude(&db_path, &mut data, &mut fw); youmubot_prelude::setup::setup_prelude(&db_path, &mut data, &mut fw);
// Setup core // Setup core
#[cfg(feature = "core")]
youmubot_core::setup(&db_path, &client, &mut data).expect("Setup db should succeed"); youmubot_core::setup(&db_path, &client, &mut data).expect("Setup db should succeed");
// osu! // 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..."); println!("Starting...");
if let Err(v) = client.start() { if let Err(v) = client.start() {
panic!(v) panic!(v)
@ -72,10 +92,10 @@ fn setup_framework(client: &Client) -> StandardFramework {
.expect("Should be able to get app info") .expect("Should be able to get app info")
.owner; .owner;
StandardFramework::new() let fw = StandardFramework::new()
.configure(|c| { .configure(|c| {
c.with_whitespace(false) c.with_whitespace(false)
.prefix("y!") .prefix(&var("PREFIX").unwrap_or("y!".to_owned()))
.delimiters(vec![" / ", "/ ", " /", "/"]) .delimiters(vec![" / ", "/ ", " /", "/"])
.owners([owner.id].iter().cloned().collect()) .owners([owner.id].iter().cloned().collect())
}) })
@ -126,10 +146,14 @@ fn setup_framework(client: &Client) -> StandardFramework {
.bucket("images", |c| c.time_span(60).limit(2)) .bucket("images", |c| c.time_span(60).limit(2))
.bucket("community", |c| { .bucket("community", |c| {
c.delay(30).time_span(30).limit(1) c.delay(30).time_span(30).limit(1)
}) });
// groups here // groups here
.group(&youmubot_core::ADMIN_GROUP) #[cfg(feature = "core")]
.group(&youmubot_core::FUN_GROUP) let fw = fw
.group(&youmubot_core::COMMUNITY_GROUP) .group(&youmubot_core::ADMIN_GROUP)
.group(&OSU_GROUP) .group(&youmubot_core::FUN_GROUP)
.group(&youmubot_core::COMMUNITY_GROUP);
#[cfg(feature = "osu")]
let fw = fw.group(&youmubot_osu::discord::OSU_GROUP);
fw
} }