Add forcesave command

This commit is contained in:
Natsu Kagami 2024-12-31 03:35:10 +01:00
parent e2cbb6d10e
commit 6169eaee10
Signed by: nki
GPG key ID: 55A032EB38B49ADB
2 changed files with 61 additions and 28 deletions

View file

@ -3,7 +3,10 @@ use poise::CreateReply;
use serenity::all::User;
/// osu!-related command group.
#[poise::command(slash_command, subcommands("profile", "top", "recent", "save"))]
#[poise::command(
slash_command,
subcommands("profile", "top", "recent", "save", "forcesave")
)]
pub async fn osu<U: HasOsuEnv>(_ctx: CmdContext<'_, U>) -> Result<()> {
Ok(())
}
@ -254,6 +257,40 @@ pub async fn save<U: HasOsuEnv>(
Ok(())
}
/// Force-save an osu! profile into Youmu's database for tracking and quick commands.
#[poise::command(slash_command, owners_only)]
pub async fn forcesave<U: HasOsuEnv>(
ctx: CmdContext<'_, U>,
#[description = "The osu! username to set to"] username: String,
#[description = "The discord user to assign to"] discord_name: User,
) -> Result<()> {
let env = ctx.data().osu_env();
let osu_client = &env.client;
ctx.defer().await?;
let Some(u) = osu_client
.user(&UserID::from_string(username.clone()), |f| f)
.await?
else {
return Err(Error::msg("osu! user not found"));
};
add_user(discord_name.id, &u, &env).await?;
let ex = UserExtras::from_user(&env, &u, u.preferred_mode).await?;
ctx.send(
CreateReply::default()
.content(
MessageBuilder::new()
.push("Youmu is now tracking user ")
.push(discord_name.mention().to_string())
.push(" with osu! account ")
.push_bold_safe(username)
.build(),
)
.embed(user_embed(u, ex)),
)
.await?;
Ok(())
}
fn arg_from_username_or_discord(
username: Option<String>,
discord_name: Option<User>,

View file

@ -407,17 +407,18 @@ pub(crate) async fn handle_save_respond(
#[num_args(2)]
pub async fn forcesave(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let env = ctx.data.read().await.get::<OsuEnv>().unwrap().clone();
let osu_client = &env.client;
let target = args.single::<UserId>()?.0;
let username = args.quoted().trimmed().single::<String>()?;
let user: Option<User> = osu_client
let Some(u) = osu_client
.user(&UserID::from_string(username.clone()), |f| f)
.await?;
match user {
Some(u) => {
.await?
else {
msg.reply(&ctx, "user not found...").await?;
return Ok(());
};
add_user(target, &u, &env).await?;
let ex = UserExtras::from_user(&env, &u, u.preferred_mode).await?;
msg.channel_id
@ -436,11 +437,6 @@ pub async fn forcesave(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
.embed(user_embed(u, ex)),
)
.await?;
}
None => {
msg.reply(&ctx, "user not found...").await?;
}
}
Ok(())
}