Store osu! user username

This commit is contained in:
Natsu Kagami 2024-02-04 23:24:28 +01:00
parent 85954639d7
commit f7ea23ed3b
Signed by: nki
GPG key ID: 55A032EB38B49ADB
4 changed files with 34 additions and 11 deletions

View file

@ -1,3 +1,5 @@
use std::borrow::Cow;
use chrono::{DateTime, Utc};
use youmubot_db_sql::{models::osu as models, models::osu_user as model, Pool};
@ -151,6 +153,7 @@ impl OsuUserBests {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OsuUser {
pub user_id: UserId,
pub username: Cow<'static, str>,
pub id: u64,
pub last_update: DateTime<Utc>,
pub pp: [Option<f64>; 4],
@ -162,6 +165,7 @@ impl From<OsuUser> for model::OsuUser {
fn from(u: OsuUser) -> Self {
Self {
user_id: u.user_id.0 as i64,
username: Some(u.username.into_owned()),
id: u.id as i64,
last_update: u.last_update,
pp_std: u.pp[Mode::Std as usize],
@ -177,6 +181,10 @@ impl From<model::OsuUser> for OsuUser {
fn from(u: model::OsuUser) -> Self {
Self {
user_id: UserId(u.user_id as u64),
username: u
.username
.map(|v| Cow::Owned(v))
.unwrap_or("unknown".into()),
id: u.id as u64,
last_update: u.last_update,
pp: [0, 1, 2, 3].map(|v| match Mode::from(v) {

View file

@ -2,7 +2,7 @@ use crate::{
discord::beatmap_cache::BeatmapMetaCache,
discord::display::ScoreListStyle,
discord::oppai_cache::{BeatmapCache, BeatmapInfo},
models::{Beatmap, Mode, Mods, User},
models::{self, Beatmap, Mode, Mods, User},
request::{BeatmapRequestKind, UserID},
Client as OsuHttpClient,
};
@ -210,12 +210,13 @@ pub async fn save(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
.await?;
return Ok(());
}
add_user(msg.author.id, u.id, &data).await?;
let username = u.username.clone();
add_user(msg.author.id, u, &data).await?;
msg.reply(
&ctx,
MessageBuilder::new()
.push("user has been set to ")
.push_mono_safe(u.username)
.push_mono_safe(username)
.build(),
)
.await?;
@ -238,16 +239,16 @@ pub async fn forcesave(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
let osu = data.get::<OsuClient>().unwrap();
let target = args.single::<serenity::model::id::UserId>()?;
let user = args.quoted().trimmed().single::<String>()?;
let user: Option<User> = osu.user(UserID::Auto(user), |f| f).await?;
let username = args.quoted().trimmed().single::<String>()?;
let user: Option<User> = osu.user(UserID::Auto(username.clone()), |f| f).await?;
match user {
Some(u) => {
add_user(target, u.id, &data).await?;
add_user(target, u, &data).await?;
msg.reply(
&ctx,
MessageBuilder::new()
.push("user has been set to ")
.push_mono_safe(u.username)
.push_mono_safe(username)
.build(),
)
.await?;
@ -259,10 +260,15 @@ pub async fn forcesave(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
Ok(())
}
async fn add_user(target: serenity::model::id::UserId, user_id: u64, data: &TypeMap) -> Result<()> {
async fn add_user(
target: serenity::model::id::UserId,
user: models::User,
data: &TypeMap,
) -> Result<()> {
let u = OsuUser {
user_id: target,
id: user_id,
username: user.username.into(),
id: user.id,
failures: 0,
last_update: chrono::Utc::now(),
pp: [None, None, None, None],