Core: asyncify fun commands

This commit is contained in:
Natsu Kagami 2020-09-05 21:21:36 -04:00
parent d607a0d701
commit 9e38f88e97
No known key found for this signature in database
GPG key ID: F17543D4B9424B94

View file

@ -28,7 +28,7 @@ struct Fun;
#[max_args(2)] #[max_args(2)]
#[usage = "[max-dice-faces = 6] / [message]"] #[usage = "[max-dice-faces = 6] / [message]"]
#[example = "100 / What's my score?"] #[example = "100 / What's my score?"]
fn roll(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { async fn roll(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let dice = if args.is_empty() { let dice = if args.is_empty() {
6 6
} else { } else {
@ -36,7 +36,8 @@ fn roll(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
}; };
if dice == 0 { if dice == 0 {
msg.reply(&ctx, "Give me a dice with 0 faces, what do you expect 😒")?; msg.reply(&ctx, "Give me a dice with 0 faces, what do you expect 😒")
.await?;
return Ok(()); return Ok(());
} }
@ -47,7 +48,8 @@ fn roll(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
}; };
match args.single_quoted::<String>() { match args.single_quoted::<String>() {
Ok(s) => msg.reply( Ok(s) => {
msg.reply(
&ctx, &ctx,
MessageBuilder::new() MessageBuilder::new()
.push("you asked ") .push("you asked ")
@ -57,14 +59,19 @@ fn roll(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
dice, result dice, result
)) ))
.build(), .build(),
), )
Err(_) if args.is_empty() => msg.reply( .await
}
Err(_) if args.is_empty() => {
msg.reply(
&ctx, &ctx,
format!( format!(
"I rolled a 🎲 of **{}** faces, and got **{}**!", "I rolled a 🎲 of **{}** faces, and got **{}**!",
dice, result dice, result
), ),
), )
.await
}
Err(e) => return Err(e.into()), Err(e) => return Err(e.into()),
}?; }?;
@ -77,7 +84,7 @@ You may prefix the first choice with `?` to make it a question!
If no choices are given, Youmu defaults to `Yes!` and `No!`"#] If no choices are given, Youmu defaults to `Yes!` and `No!`"#]
#[usage = "[?question]/[choice #1]/[choice #2]/..."] #[usage = "[?question]/[choice #1]/[choice #2]/..."]
#[example = "?What for dinner/Pizza/Hamburger"] #[example = "?What for dinner/Pizza/Hamburger"]
fn pick(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { async fn pick(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let (question, choices) = { let (question, choices) = {
// Get a list of options. // Get a list of options.
let mut choices = args let mut choices = args
@ -114,15 +121,19 @@ fn pick(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
}; };
match question { match question {
None => msg.reply( None => {
msg.reply(
&ctx, &ctx,
MessageBuilder::new() MessageBuilder::new()
.push("Youmu picks 👉") .push("Youmu picks 👉")
.push_bold_safe(choice) .push_bold_safe(choice)
.push("👈!") .push("👈!")
.build(), .build(),
), )
Some(s) => msg.reply( .await
}
Some(s) => {
msg.reply(
&ctx, &ctx,
MessageBuilder::new() MessageBuilder::new()
.push("you asked ") .push("you asked ")
@ -131,7 +142,9 @@ fn pick(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
.push_bold_safe(choice) .push_bold_safe(choice)
.push("👈!") .push("👈!")
.build(), .build(),
), )
.await
}
}?; }?;
Ok(()) Ok(())
@ -142,7 +155,7 @@ fn pick(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
#[usage = "[user_mention = yourself]"] #[usage = "[user_mention = yourself]"]
#[example = "@user#1234"] #[example = "@user#1234"]
#[max_args(1)] #[max_args(1)]
fn name(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { async fn name(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let user_id = if args.is_empty() { let user_id = if args.is_empty() {
msg.author.id msg.author.id
} else { } else {
@ -153,15 +166,15 @@ fn name(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
"your".to_owned() "your".to_owned()
} else { } else {
MessageBuilder::new() MessageBuilder::new()
.push_bold_safe(user_id.to_user(&ctx)?.tag()) .push_bold_safe(user_id.to_user(&ctx).await?.tag())
.push("'s") .push("'s")
.build() .build()
}; };
// Rule out a couple of cases // Rule out a couple of cases
if user_id == ctx.http.get_current_application_info()?.id { if user_id == ctx.http.get_current_application_info().await?.id {
// This is my own user_id // This is my own user_id
msg.reply(&ctx, "😠 My name is **Youmu Konpaku**!")?; msg.reply(&ctx, "😠 My name is **Youmu Konpaku**!").await?;
return Ok(()); return Ok(());
} }
@ -173,6 +186,7 @@ fn name(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
"{} Japanese🇯🇵 name is **{} {}**!", "{} Japanese🇯🇵 name is **{} {}**!",
user_mention, first_name, last_name user_mention, first_name, last_name
), ),
)?; )
.await?;
Ok(()) Ok(())
} }