mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-19 16:58:55 +00:00
osu: Make load_beatmap arguments less confusing
This commit is contained in:
parent
32053c3fe3
commit
3f8198f19c
1 changed files with 23 additions and 23 deletions
|
@ -1,4 +1,4 @@
|
||||||
use std::{str::FromStr, sync::Arc};
|
use std::{borrow::Borrow, str::FromStr, sync::Arc};
|
||||||
|
|
||||||
use futures_util::join;
|
use futures_util::join;
|
||||||
use interaction::{beatmap_components, score_components};
|
use interaction::{beatmap_components, score_components};
|
||||||
|
@ -561,10 +561,11 @@ impl FromStr for OptBeatmapSet {
|
||||||
/// Load the mentioned beatmap from the given message.
|
/// Load the mentioned beatmap from the given message.
|
||||||
pub(crate) async fn load_beatmap(
|
pub(crate) async fn load_beatmap(
|
||||||
env: &OsuEnv,
|
env: &OsuEnv,
|
||||||
msg: &Message,
|
channel_id: serenity::all::ChannelId,
|
||||||
|
referenced: Option<&impl Borrow<Message>>,
|
||||||
) -> Option<(BeatmapWithMode, Option<Mods>)> {
|
) -> Option<(BeatmapWithMode, Option<Mods>)> {
|
||||||
use link_parser::{parse_short_links, EmbedType};
|
use link_parser::{parse_short_links, EmbedType};
|
||||||
if let Some(replied) = &msg.referenced_message {
|
if let Some(replied) = referenced {
|
||||||
async fn try_content(
|
async fn try_content(
|
||||||
env: &OsuEnv,
|
env: &OsuEnv,
|
||||||
content: &str,
|
content: &str,
|
||||||
|
@ -578,27 +579,24 @@ pub(crate) async fn load_beatmap(
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(v) = try_content(env, &replied.content).await {
|
for embed in &replied.borrow().embeds {
|
||||||
return Some(v);
|
|
||||||
}
|
|
||||||
for embed in &replied.embeds {
|
|
||||||
if let Some(desc) = &embed.description {
|
|
||||||
if let Some(v) = try_content(env, desc).await {
|
|
||||||
return Some(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for field in &embed.fields {
|
for field in &embed.fields {
|
||||||
if let Some(v) = try_content(env, &field.value).await {
|
if let Some(v) = try_content(env, &field.value).await {
|
||||||
return Some(v);
|
return Some(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let Some(desc) = &embed.description {
|
||||||
|
if let Some(v) = try_content(env, desc).await {
|
||||||
|
return Some(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(v) = try_content(env, &replied.borrow().content).await {
|
||||||
|
return Some(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let b = cache::get_beatmap(&env, msg.channel_id)
|
let b = cache::get_beatmap(&env, channel_id).await.ok().flatten();
|
||||||
.await
|
|
||||||
.ok()
|
|
||||||
.flatten();
|
|
||||||
b.map(|b| (b, None))
|
b.map(|b| (b, None))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -611,14 +609,14 @@ pub(crate) async fn load_beatmap(
|
||||||
pub async fn last(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
pub async fn last(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
let env = ctx.data.read().await.get::<OsuEnv>().unwrap().clone();
|
let env = ctx.data.read().await.get::<OsuEnv>().unwrap().clone();
|
||||||
|
|
||||||
let b = load_beatmap(&env, msg).await;
|
let b = load_beatmap(&env, msg.channel_id, msg.referenced_message.as_ref()).await;
|
||||||
let beatmapset = args.find::<OptBeatmapSet>().is_ok();
|
let beatmapset = args.find::<OptBeatmapSet>().is_ok();
|
||||||
|
|
||||||
match b {
|
match b {
|
||||||
Some((BeatmapWithMode(b, m), mods_def)) => {
|
Some((bm, mods_def)) => {
|
||||||
let mods = args.find::<Mods>().ok().or(mods_def).unwrap_or(Mods::NOMOD);
|
let mods = args.find::<Mods>().ok().or(mods_def).unwrap_or(Mods::NOMOD);
|
||||||
if beatmapset {
|
if beatmapset {
|
||||||
let beatmapset = env.beatmaps.get_beatmapset(b.beatmapset_id).await?;
|
let beatmapset = env.beatmaps.get_beatmapset(bm.0.beatmapset_id).await?;
|
||||||
display::display_beatmapset(
|
display::display_beatmapset(
|
||||||
ctx,
|
ctx,
|
||||||
beatmapset,
|
beatmapset,
|
||||||
|
@ -632,19 +630,21 @@ pub async fn last(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
|
||||||
}
|
}
|
||||||
let info = env
|
let info = env
|
||||||
.oppai
|
.oppai
|
||||||
.get_beatmap(b.beatmap_id)
|
.get_beatmap(bm.0.beatmap_id)
|
||||||
.await?
|
.await?
|
||||||
.get_possible_pp_with(m, mods)?;
|
.get_possible_pp_with(bm.1, mods)?;
|
||||||
msg.channel_id
|
msg.channel_id
|
||||||
.send_message(
|
.send_message(
|
||||||
&ctx,
|
&ctx,
|
||||||
CreateMessage::new()
|
CreateMessage::new()
|
||||||
.content("Here is the beatmap you requested!")
|
.content("Here is the beatmap you requested!")
|
||||||
.embed(beatmap_embed(&b, m, mods, info))
|
.embed(beatmap_embed(&bm.0, bm.1, mods, info))
|
||||||
.components(vec![beatmap_components()])
|
.components(vec![beatmap_components()])
|
||||||
.reference_message(msg),
|
.reference_message(msg),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
// Save the beatmap...
|
||||||
|
cache::save_beatmap(&env, msg.channel_id, &bm).await?;
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
msg.reply(&ctx, "No beatmap was queried on this channel.")
|
msg.reply(&ctx, "No beatmap was queried on this channel.")
|
||||||
|
@ -662,7 +662,7 @@ pub async fn last(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
|
||||||
#[max_args(3)]
|
#[max_args(3)]
|
||||||
pub async fn check(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
pub async fn check(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
let env = ctx.data.read().await.get::<OsuEnv>().unwrap().clone();
|
let env = ctx.data.read().await.get::<OsuEnv>().unwrap().clone();
|
||||||
let bm = load_beatmap(&env, msg).await;
|
let bm = load_beatmap(&env, msg.channel_id, msg.referenced_message.as_ref()).await;
|
||||||
|
|
||||||
let bm = match bm {
|
let bm = match bm {
|
||||||
Some((bm, _)) => bm,
|
Some((bm, _)) => bm,
|
||||||
|
|
Loading…
Add table
Reference in a new issue