Skip to content

Commit

Permalink
Replace several instances of if let Err with ? in example 5 (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
peppizza committed Oct 15, 2020
1 parent c0c2137 commit b1187ce
Showing 1 changed file with 16 additions and 42 deletions.
58 changes: 16 additions & 42 deletions examples/e05_command_framework/src/main.rs
Expand Up @@ -296,9 +296,7 @@ async fn commands(ctx: &Context, msg: &Message) -> CommandResult {
writeln!(contents, "- {name}: {amount}", name=k, amount=v)?;
}

if let Err(why) = msg.channel_id.say(&ctx.http, &contents).await {
println!("Error sending message: {:?}", why);
}
msg.channel_id.say(&ctx.http, &contents).await?;

Ok(())
}
Expand All @@ -325,9 +323,7 @@ async fn say(ctx: &Context, msg: &Message, args: Args) -> CommandResult {

let content = content_safe(&ctx.cache, &args.rest(), &settings).await;

if let Err(why) = msg.channel_id.say(&ctx.http, &content).await {
println!("Error sending message: {:?}", why);
}
msg.channel_id.say(&ctx.http, &content).await?;

Ok(())
}
Expand Down Expand Up @@ -359,9 +355,7 @@ async fn owner_check(_: &Context, msg: &Message, _: &mut Args, _: &CommandOption

#[command]
async fn some_long_command(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, &format!("Arguments: {:?}", args.rest())).await {
println!("Error sending message: {:?}", why);
}
msg.channel_id.say(&ctx.http, &format!("Arguments: {:?}", args.rest())).await?;

Ok(())
}
Expand All @@ -384,9 +378,7 @@ async fn about_role(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
}
}

if let Err(why) = msg.channel_id.say(&ctx.http, format!("Could not find role named: {:?}", potential_role_name)).await {
println!("Error sending message: {:?}", why);
}
msg.channel_id.say(&ctx.http, format!("Could not find role named: {:?}", potential_role_name)).await?;

Ok(())
}
Expand All @@ -400,18 +392,14 @@ async fn multiply(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult

let res = first * second;

if let Err(why) = msg.channel_id.say(&ctx.http, &res.to_string()).await {
println!("Err sending product of {} and {}: {:?}", first, second, why);
}
msg.channel_id.say(&ctx.http, &res.to_string()).await?;

Ok(())
}

#[command]
async fn about(ctx: &Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, "This is a small test-bot! : )").await {
println!("Error sending message: {:?}", why);
}
msg.channel_id.say(&ctx.http, "This is a small test-bot! : )").await?;

Ok(())
}
Expand All @@ -425,7 +413,7 @@ async fn latency(ctx: &Context, msg: &Message) -> CommandResult {
let shard_manager = match data.get::<ShardManagerContainer>() {
Some(v) => v,
None => {
let _ = msg.reply(ctx, "There was a problem getting the shard manager").await;
msg.reply(ctx, "There was a problem getting the shard manager").await?;

return Ok(());
},
Expand All @@ -440,13 +428,13 @@ async fn latency(ctx: &Context, msg: &Message) -> CommandResult {
let runner = match runners.get(&ShardId(ctx.shard_id)) {
Some(runner) => runner,
None => {
let _ = msg.reply(ctx, "No shard found");
msg.reply(ctx, "No shard found").await?;

return Ok(());
},
};

let _ = msg.reply(ctx, &format!("The shard latency is {:?}", runner.latency)).await;
msg.reply(ctx, &format!("The shard latency is {:?}", runner.latency)).await?;

Ok(())
}
Expand All @@ -456,9 +444,7 @@ async fn latency(ctx: &Context, msg: &Message) -> CommandResult {
#[only_in(guilds)]
#[checks(Owner)]
async fn ping(ctx: &Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong! : )").await {
println!("Error sending message: {:?}", why);
}
msg.channel_id.say(&ctx.http, "Pong! : )").await?;

Ok(())
}
Expand All @@ -471,9 +457,7 @@ async fn ping(ctx: &Context, msg: &Message) -> CommandResult {
// Allow only administrators to call this:
#[required_permissions("ADMINISTRATOR")]
async fn cat(ctx: &Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, ":cat:").await {
println!("Error sending message: {:?}", why);
}
msg.channel_id.say(&ctx.http, ":cat:").await?;

Ok(())
}
Expand All @@ -482,9 +466,7 @@ async fn cat(ctx: &Context, msg: &Message) -> CommandResult {
#[description = "Sends an emoji with a dog."]
#[bucket = "emoji"]
async fn dog(ctx: &Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, ":dog:").await {
println!("Error sending message: {:?}", why);
}
msg.channel_id.say(&ctx.http, ":dog:").await?;

Ok(())
}
Expand All @@ -497,9 +479,7 @@ async fn bird(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
format!(":bird: could not find animal named: `{}`.", args.rest())
};

if let Err(why) = msg.channel_id.say(&ctx.http, say_content).await {
println!("Error sending message: {:?}", why);
}
msg.channel_id.say(&ctx.http, say_content).await?;

Ok(())
}
Expand All @@ -513,18 +493,14 @@ async fn am_i_admin(ctx: &Context, msg: &Message, _args: Args) -> CommandResult

for role in &member.roles {
if role.to_role_cached(&ctx.cache).await.map_or(false, |r| r.has_permission(Permissions::ADMINISTRATOR)) {
if let Err(why) = msg.channel_id.say(&ctx.http, "Yes, you are.").await {
println!("Error sending message: {:?}", why);
}
msg.channel_id.say(&ctx.http, "Yes, you are.").await?;

return Ok(());
}
}
}

if let Err(why) = msg.channel_id.say(&ctx.http, "No, you are not.").await {
println!("Error sending message: {:?}", why);
}
msg.channel_id.say(&ctx.http, "No, you are not.").await?;

Ok(())
}
Expand All @@ -545,9 +521,7 @@ async fn slow_mode(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
"Failed to find channel in cache.".to_string()
};

if let Err(why) = msg.channel_id.say(&ctx.http, say_content).await {
println!("Error sending message: {:?}", why);
}
msg.channel_id.say(&ctx.http, say_content).await?;

Ok(())
}

0 comments on commit b1187ce

Please sign in to comment.