Add forcesave command

This commit is contained in:
Natsu Kagami 2024-12-31 03:35:10 +01:00 committed by Natsu Kagami
parent ecab57f31a
commit 3034f8e809
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,40 +407,36 @@ 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) => {
add_user(target, &u, &env).await?;
let ex = UserExtras::from_user(&env, &u, u.preferred_mode).await?;
msg.channel_id
.send_message(
&ctx,
CreateMessage::new()
.reference_message(msg)
.content(
MessageBuilder::new()
.push("Youmu is now tracking user ")
.push(target.mention().to_string())
.push(" with osu! account ")
.push_bold_safe(username)
.build(),
)
.embed(user_embed(u, ex)),
.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
.send_message(
&ctx,
CreateMessage::new()
.reference_message(msg)
.content(
MessageBuilder::new()
.push("Youmu is now tracking user ")
.push(target.mention().to_string())
.push(" with osu! account ")
.push_bold_safe(username)
.build(),
)
.await?;
}
None => {
msg.reply(&ctx, "user not found...").await?;
}
}
.embed(user_embed(u, ex)),
)
.await?;
Ok(())
}