Implement user saving

This commit is contained in:
Natsu Kagami 2020-01-13 15:35:50 -05:00
parent f7063a2992
commit 4020405801
3 changed files with 62 additions and 6 deletions

View file

@ -30,7 +30,7 @@ impl Client {
r: reqwest::RequestBuilder, r: reqwest::RequestBuilder,
) -> Result<reqwest::Response, Error> { ) -> Result<reqwest::Response, Error> {
let v = r.query(&[("k", &self.key)]).build()?; let v = r.query(&[("k", &self.key)]).build()?;
// println!("{}", v.url()); dbg!(v.url());
Ok(c.execute(v)?) Ok(c.execute(v)?)
} }

View file

@ -1,11 +1,12 @@
use crate::commands::args::Duration; use crate::commands::args::Duration;
use crate::db::{DBWriteGuard, OsuSavedUsers};
use crate::http; use crate::http;
use chrono::Utc; use chrono::Utc;
use serenity::{ use serenity::{
builder::CreateEmbed, builder::CreateEmbed,
framework::standard::{ framework::standard::{
macros::{command, group}, macros::{command, group},
Args, CommandResult, Args, CommandError as Error, CommandResult,
}, },
model::channel::Message, model::channel::Message,
prelude::*, prelude::*,
@ -26,7 +27,7 @@ group!({
prefix: "osu", prefix: "osu",
description: "osu! related commands.", description: "osu! related commands.",
}, },
commands: [std, taiko, catch, mania], commands: [std, taiko, catch, mania, save],
}); });
#[command] #[command]
@ -65,9 +66,60 @@ pub fn mania(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
get_user(ctx, msg, args, Mode::Mania) get_user(ctx, msg, args, Mode::Mania)
} }
fn get_user(ctx: &mut Context, msg: &Message, mut args: Args, mode: Mode) -> CommandResult { #[command]
let username = args.single::<String>()?; #[description = "Save the given username as your username."]
let data = ctx.data.write(); #[usage = "[username or user_id]"]
#[num_args(1)]
pub fn save(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
let mut data = ctx.data.write();
let reqwest = data.get::<http::HTTP>().unwrap();
let osu = data.get::<http::Osu>().unwrap();
let user = args.single::<String>()?;
let user: Option<User> = osu.user(reqwest, UserID::Auto(user), |f| f)?;
match user {
Some(u) => {
let mut db: DBWriteGuard<_> = data
.get_mut::<OsuSavedUsers>()
.ok_or(Error::from("DB uninitialized"))?
.into();
let mut db = db.borrow_mut()?;
db.insert(msg.author.id, u.id);
msg.reply(
&ctx,
MessageBuilder::new()
.push("user has been set to ")
.push_mono_safe(u.username)
.build(),
)?;
}
None => {
msg.reply(&ctx, "user not found...")?;
}
}
Ok(())
}
fn get_user(ctx: &mut Context, msg: &Message, args: Args, mode: Mode) -> CommandResult {
let mut data = ctx.data.write();
let username = match args.remains() {
Some(v) => v.to_owned(),
None => {
let db: DBWriteGuard<_> = data
.get_mut::<OsuSavedUsers>()
.ok_or(Error::from("DB uninitialized"))?
.into();
let db = db.borrow()?;
match db.get(&msg.author.id) {
Some(ref v) => v.to_string(),
None => {
msg.reply(&ctx, "You have not saved any account.")?;
return Ok(());
}
}
}
};
let reqwest = data.get::<http::HTTP>().unwrap(); let reqwest = data.get::<http::HTTP>().unwrap();
let osu = data.get::<http::Osu>().unwrap(); let osu = data.get::<http::Osu>().unwrap();
let user = osu.user(reqwest, UserID::Auto(username), |f| f.mode(mode))?; let user = osu.user(reqwest, UserID::Auto(username), |f| f.mode(mode))?;

View file

@ -35,6 +35,9 @@ where
/// A list of SoftBans for all servers. /// A list of SoftBans for all servers.
pub type SoftBans = DB<GuildMap<ServerSoftBans>>; pub type SoftBans = DB<GuildMap<ServerSoftBans>>;
/// Save the user IDs.
pub type OsuSavedUsers = DB<HashMap<UserId, u64>>;
/// Sets up all databases in the client. /// Sets up all databases in the client.
pub fn setup_db(client: &mut Client) -> Result<(), Error> { pub fn setup_db(client: &mut Client) -> Result<(), Error> {
let path: PathBuf = var("DBPATH").map(|v| PathBuf::from(v)).unwrap_or_else(|e| { let path: PathBuf = var("DBPATH").map(|v| PathBuf::from(v)).unwrap_or_else(|e| {
@ -43,6 +46,7 @@ pub fn setup_db(client: &mut Client) -> Result<(), Error> {
}); });
let mut data = client.data.write(); let mut data = client.data.write();
SoftBans::insert_into(&mut *data, &path.join("soft_bans.ron"))?; SoftBans::insert_into(&mut *data, &path.join("soft_bans.ron"))?;
OsuSavedUsers::insert_into(&mut *data, &path.join("osu_saved_users.ron"))?;
Ok(()) Ok(())
} }