mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-19 16:58:55 +00:00
Implement save command and user-aware profile command
This commit is contained in:
parent
adc33076f3
commit
f512aa8726
2 changed files with 63 additions and 4 deletions
|
@ -1,4 +1,5 @@
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
use codeforces::User;
|
||||||
use serenity::model::id::UserId;
|
use serenity::model::id::UserId;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use youmubot_db::DB;
|
use youmubot_db::DB;
|
||||||
|
@ -24,3 +25,13 @@ impl Default for CfUser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<User> for CfUser {
|
||||||
|
fn from(u: User) -> Self {
|
||||||
|
Self {
|
||||||
|
handle: u.handle,
|
||||||
|
last_update: Utc::now(),
|
||||||
|
rating: u.rating,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -14,7 +14,8 @@ mod hook;
|
||||||
// /// Live-commentating a Codeforces round.
|
// /// Live-commentating a Codeforces round.
|
||||||
// pub mod live;
|
// pub mod live;
|
||||||
|
|
||||||
pub use db::CfSavedUsers;
|
use db::CfSavedUsers;
|
||||||
|
|
||||||
pub use hook::codeforces_info_hook;
|
pub use hook::codeforces_info_hook;
|
||||||
|
|
||||||
/// Sets up the CF databases.
|
/// Sets up the CF databases.
|
||||||
|
@ -26,7 +27,7 @@ pub fn setup(path: &std::path::Path, data: &mut ShareMap) {
|
||||||
#[group]
|
#[group]
|
||||||
#[prefix = "cf"]
|
#[prefix = "cf"]
|
||||||
#[description = "Codeforces-related commands"]
|
#[description = "Codeforces-related commands"]
|
||||||
#[commands(profile)]
|
#[commands(profile, save)]
|
||||||
#[default_command(profile)]
|
#[default_command(profile)]
|
||||||
pub struct Codeforces;
|
pub struct Codeforces;
|
||||||
|
|
||||||
|
@ -35,11 +36,28 @@ pub struct Codeforces;
|
||||||
#[description = "Get an user's profile"]
|
#[description = "Get an user's profile"]
|
||||||
#[usage = "[handle or tag = yourself]"]
|
#[usage = "[handle or tag = yourself]"]
|
||||||
#[example = "natsukagami"]
|
#[example = "natsukagami"]
|
||||||
#[num_args(1)]
|
#[max_args(1)]
|
||||||
pub fn profile(ctx: &mut Context, m: &Message, mut args: Args) -> CommandResult {
|
pub fn profile(ctx: &mut Context, m: &Message, mut args: Args) -> CommandResult {
|
||||||
let handle = args.single::<String>()?;
|
let handle = args
|
||||||
|
.single::<UsernameArg>()
|
||||||
|
.unwrap_or(UsernameArg::mention(m.author.id));
|
||||||
let http = ctx.data.get_cloned::<HTTPClient>();
|
let http = ctx.data.get_cloned::<HTTPClient>();
|
||||||
|
|
||||||
|
let handle = match handle {
|
||||||
|
UsernameArg::Raw(s) => s,
|
||||||
|
UsernameArg::Tagged(u) => {
|
||||||
|
let db = CfSavedUsers::open(&*ctx.data.read());
|
||||||
|
let db = db.borrow()?;
|
||||||
|
match db.get(&u) {
|
||||||
|
Some(v) => v.handle.clone(),
|
||||||
|
None => {
|
||||||
|
m.reply(&ctx, "no saved account found.")?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let account = codeforces::User::info(&http, &[&handle[..]])?
|
let account = codeforces::User::info(&http, &[&handle[..]])?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.next();
|
.next();
|
||||||
|
@ -57,3 +75,33 @@ pub fn profile(ctx: &mut Context, m: &Message, mut args: Args) -> CommandResult
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[command]
|
||||||
|
#[description = "Link your Codeforces account to the Discord account, to enjoy Youmu's tracking capabilities."]
|
||||||
|
#[usage = "[handle]"]
|
||||||
|
#[num_args(1)]
|
||||||
|
pub fn save(ctx: &mut Context, m: &Message, mut args: Args) -> CommandResult {
|
||||||
|
let handle = args.single::<String>()?;
|
||||||
|
let http = ctx.data.get_cloned::<HTTPClient>();
|
||||||
|
|
||||||
|
let account = codeforces::User::info(&http, &[&handle[..]])?
|
||||||
|
.into_iter()
|
||||||
|
.next();
|
||||||
|
|
||||||
|
match account {
|
||||||
|
None => {
|
||||||
|
m.reply(&ctx, "cannot find an account with such handle")?;
|
||||||
|
}
|
||||||
|
Some(acc) => {
|
||||||
|
let db = CfSavedUsers::open(&*ctx.data.read());
|
||||||
|
let mut db = db.borrow_mut()?;
|
||||||
|
m.reply(
|
||||||
|
&ctx,
|
||||||
|
format!("account `{}` has been linked to your account.", &acc.handle),
|
||||||
|
)?;
|
||||||
|
db.insert(m.author.id, acc.into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue