diff --git a/youmubot-db-sql/migrations/20240204214751_add_username_to_osu_users.sql b/youmubot-db-sql/migrations/20240204214751_add_username_to_osu_users.sql new file mode 100644 index 0000000..3759c01 --- /dev/null +++ b/youmubot-db-sql/migrations/20240204214751_add_username_to_osu_users.sql @@ -0,0 +1,4 @@ +-- Add migration script here + +ALTER TABLE osu_users + ADD COLUMN username TEXT NULL DEFAULT NULL; diff --git a/youmubot-db-sql/src/models/osu_user.rs b/youmubot-db-sql/src/models/osu_user.rs index 4665a1b..bef42b2 100644 --- a/youmubot-db-sql/src/models/osu_user.rs +++ b/youmubot-db-sql/src/models/osu_user.rs @@ -5,6 +5,7 @@ use sqlx::{query, query_as, Executor}; #[derive(Debug, Clone)] pub struct OsuUser { pub user_id: i64, + pub username: Option, // should always be there pub id: i64, pub last_update: DateTime, pub pp_std: Option, @@ -25,6 +26,7 @@ impl OsuUser { Self, r#"SELECT user_id as "user_id: i64", + username, id as "id: i64", last_update as "last_update: DateTime", pp_std, pp_taiko, pp_mania, pp_catch, @@ -46,6 +48,7 @@ impl OsuUser { Self, r#"SELECT user_id as "user_id: i64", + username, id as "id: i64", last_update as "last_update: DateTime", pp_std, pp_taiko, pp_mania, pp_catch, @@ -67,6 +70,7 @@ impl OsuUser { Self, r#"SELECT user_id as "user_id: i64", + username, id as "id: i64", last_update as "last_update: DateTime", pp_std, pp_taiko, pp_mania, pp_catch, @@ -86,8 +90,8 @@ impl OsuUser { { query!( r#"INSERT - INTO osu_users(user_id, id, last_update, pp_std, pp_taiko, pp_mania, pp_catch, failures) - VALUES(?, ?, ?, ?, ?, ?, ?, ?) + INTO osu_users(user_id, username, id, last_update, pp_std, pp_taiko, pp_mania, pp_catch, failures) + VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT (user_id) WHERE id = ? DO UPDATE SET last_update = excluded.last_update, @@ -98,6 +102,7 @@ impl OsuUser { failures = excluded.failures "#, self.user_id, + self.username, self.id, self.last_update, self.pp_std, diff --git a/youmubot-osu/src/discord/db.rs b/youmubot-osu/src/discord/db.rs index 32ece1c..78def35 100644 --- a/youmubot-osu/src/discord/db.rs +++ b/youmubot-osu/src/discord/db.rs @@ -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, pub pp: [Option; 4], @@ -162,6 +165,7 @@ impl From 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 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) { diff --git a/youmubot-osu/src/discord/mod.rs b/youmubot-osu/src/discord/mod.rs index a417086..d7e2f14 100644 --- a/youmubot-osu/src/discord/mod.rs +++ b/youmubot-osu/src/discord/mod.rs @@ -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::().unwrap(); let target = args.single::()?; - let user = args.quoted().trimmed().single::()?; - let user: Option = osu.user(UserID::Auto(user), |f| f).await?; + let username = args.quoted().trimmed().single::()?; + let user: Option = 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],