Skip to content

Commit

Permalink
Remove fancy drop tricks
Browse files Browse the repository at this point in the history
This makes the repo easier to auto-format and makes it easier to read
  • Loading branch information
GnomedDev committed Oct 5, 2023
1 parent 027b9bd commit f5a60cc
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 39 deletions.
4 changes: 3 additions & 1 deletion src/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ pub async fn command_func(ctx: Context<'_>, command: Option<&str>) -> CommandRes
ctx.say(ctx
.gettext("The group {group_name} does not have a subcommand called {subcommand_name}!")
.replace("{subcommand_name}", &remaining_args).replace("{group_name}", &command_obj.name)
).await.map(drop).map_err(Into::into)
).await?;

Ok(())
});
};

Expand Down
27 changes: 18 additions & 9 deletions src/commands/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ pub async fn join(ctx: Context<'_>) -> CommandResult {
let bot_member = guild_id.member(ctx, bot_id).await?;
if let Some(communication_disabled_until) = bot_member.communication_disabled_until {
if communication_disabled_until > serenity::Timestamp::now() {
return ctx.send_error(
ctx.send_error(
ctx.gettext("I am timed out"),
Some(ctx.gettext("ask a moderator to remove the timeout"))
).await.map(drop).map_err(Into::into)
).await?;

return Ok(())
}
}

Expand All @@ -83,13 +85,15 @@ pub async fn join(ctx: Context<'_>) -> CommandResult {
channel.permissions_for_user(ctx, bot_id)?;

if !missing_permissions.is_empty() {
return ctx.send_error(
ctx.send_error(
ctx.gettext("I do not have permissions to TTS in your voice channel"),
Some(&ctx
.gettext("please ask an administrator to give me: {missing_permissions}")
.replace("{missing_permissions}", &missing_permissions.get_permission_names().join(", "))
)
).await.map(drop).map_err(Into::into)
).await?;

return Ok(())
}

let data = ctx.data();
Expand Down Expand Up @@ -117,7 +121,8 @@ pub async fn join(ctx: Context<'_>) -> CommandResult {
ctx.send_error(
ctx.gettext("a timeout occurred while joining your voice channel"),
Some(ctx.gettext("wait a few seconds and try again"))
).await.map(drop).map_err(Into::into)
).await?;
Ok(())
} else {
Err(err.into())
}
Expand All @@ -134,7 +139,8 @@ pub async fn join(ctx: Context<'_>) -> CommandResult {
.footer(CreateEmbedFooter::new(random_footer(
&data.config.main_server_invite, bot_id, ctx.current_catalog()
)))
)).await.map(drop).map_err(Into::into)
)).await?;
Ok(())
}

/// Leaves voice channel TTS Bot is in!
Expand Down Expand Up @@ -226,7 +232,8 @@ pub async fn premium_activate(ctx: Context<'_>) -> CommandResult {
let data = ctx.data();

if data.premium_check(Some(guild_id)).await?.is_none() {
return ctx.say(ctx.gettext("Hey, this server is already premium!")).await.map(drop).map_err(Into::into)
ctx.say(ctx.gettext("Hey, this server is already premium!")).await?;
return Ok(())
}

let author = ctx.author();
Expand All @@ -253,7 +260,7 @@ pub async fn premium_activate(ctx: Context<'_>) -> CommandResult {
};

if let Some(error_msg) = error_msg {
return ctx.send(CreateReply::default().embed(CreateEmbed::default()
ctx.send(CreateReply::default().embed(CreateEmbed::default()
.title("TTS Bot Premium")
.description(error_msg)
.thumbnail(&data.premium_avatar_url)
Expand All @@ -267,7 +274,9 @@ pub async fn premium_activate(ctx: Context<'_>) -> CommandResult {
concat.push_str(line2);
concat
}))
)).await.map(drop).map_err(Into::into)
)).await?;

return Ok(())
}

data.userinfo_db.create_row(author_id).await?;
Expand Down
25 changes: 18 additions & 7 deletions src/commands/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ pub async fn uptime(ctx: Context<'_>,) -> CommandResult {
.gettext("{user_mention} has been up since: <t:{timestamp}:R>")
.replace("{user_mention}", &current_user_mention)
.replace("{timestamp}", &timestamp.to_string())
).await.map(drop).map_err(Into::into)
).await?;

Ok(())
}

/// Generates TTS and sends it in the current text channel!
Expand Down Expand Up @@ -80,7 +82,8 @@ pub async fn tts(
};

if is_unnecessary_command_invoke.await? {
ctx.say(ctx.gettext("You don't need to include the `/tts` for messages to be said!")).await.map(drop).map_err(Into::into)
ctx.say(ctx.gettext("You don't need to include the `/tts` for messages to be said!")).await?;
Ok(())
} else {
_tts(ctx, ctx.author(), &message).await
}
Expand Down Expand Up @@ -113,7 +116,9 @@ async fn _tts(ctx: Context<'_>, author: &serenity::User, message: &str) -> Comma
ctx.send(CreateReply::default()
.content(ctx.gettext("Generated some TTS!"))
.attachment(attachment)
).await.map(drop).map_err(Into::into)
).await?;

Ok(())
}

#[poise::command(category="Extra Commands", hide_in_help, context_menu_command="Speak with their voice!")]
Expand Down Expand Up @@ -227,7 +232,9 @@ and can be used by {total_members} people!")
.replace("{shard_count}", &shard_count.to_string())
.replace("{ram_usage}", &format!("{ram_usage:.1}"))
.replace("{scheduler_stats}", &scheduler_stats)
))).await.map(drop).map_err(Into::into)
))).await?;

Ok(())
}

/// Shows the current setup channel!
Expand All @@ -252,7 +259,9 @@ pub async fn premium(ctx: Context<'_>,) -> CommandResult {
ctx.say(ctx.gettext("
To support the development and hosting of TTS Bot and get access to TTS Bot Premium, including more modes (`/set mode`), many more voices (`/set voice`), and extra options such as TTS translation, see:
https://www.patreon.com/Gnome_the_Bot_Maker
")).await.map(drop).map_err(Into::into)
")).await?;

Ok(())
}

/// Gets current ping to discord!
Expand Down Expand Up @@ -283,7 +292,8 @@ pub async fn suggest(ctx: Context<'_>, #[rest] suggestion: String) -> CommandRes
.author(CreateEmbedAuthor::new(bot_name).icon_url(face))
.title("`/suggest` has been removed due to spam and misuse.")
.description("If you want to suggest a new feature, use `/invite` and ask us in the support server!")
)).await.map(drop).map_err(Into::into)
)).await?;
Ok(())
}

/// Sends the instructions to invite TTS Bot and join the support server!
Expand All @@ -308,7 +318,8 @@ pub async fn invite(ctx: Context<'_>,) -> CommandResult {
.replace("{server_invite}", &config.main_server_invite)
).try_unwrap()?
}
).await.map(drop).map_err(Into::into)
).await?;
Ok(())
}

pub fn commands() -> [Command; 10] {
Expand Down
21 changes: 14 additions & 7 deletions src/commands/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ pub async fn dm(ctx: PrefixContext<'_>, todm: serenity::User, #[rest] message: S
ctx.msg.channel_id.send_message(&ctx.serenity_context(), CreateMessage::default()
.content(content)
.add_embed(CreateEmbed::from(embed))
).await.map(drop).map_err(Into::into)
).await?;

Ok(())
}

#[poise::command(prefix_command, hide_in_help, owners_only)]
Expand Down Expand Up @@ -118,7 +120,8 @@ pub async fn purge_guilds(ctx: Context<'_>, mode: PurgeGuildsMode) -> CommandRes
let data = ctx.data();
if mode == PurgeGuildsMode::Abort {
data.currently_purging.store(false, SeqCst);
return ctx.say("Done!").await.map(drop).map_err(Into::into)
ctx.say("Done!").await?;
return Ok(());
}

let cache = ctx.cache();
Expand All @@ -140,14 +143,17 @@ pub async fn purge_guilds(ctx: Context<'_>, mode: PurgeGuildsMode) -> CommandRes
guild.leave(ctx).await?;

if !data.currently_purging.load(SeqCst) {
return msg.edit(ctx, poise::CreateReply::default().content("Aborted!")).await.map(drop).map_err(Into::into)
msg.edit(ctx, poise::CreateReply::default().content("Aborted!")).await?;
return Ok(());
}
}

msg.edit(ctx, poise::CreateReply::default().content("Done! Left {to_leave_count} guilds!")).await.map(drop)
msg.edit(ctx, poise::CreateReply::default().content("Done! Left {to_leave_count} guilds!")).await?;
} else {
ctx.say(format!("Would purge {to_leave_count} guilds!")).await.map(drop)
}.map_err(Into::into)
ctx.say(format!("Would purge {to_leave_count} guilds!")).await?;
}

Ok(())
}

#[poise::command(prefix_command, owners_only, hide_in_help)]
Expand Down Expand Up @@ -240,7 +246,8 @@ Nickname Data: `{nick_row:?}`
User Voice Data: `{user_voice_row:?}`
Guild Voice Data: `{guild_voice_row:?}`
"))
)).await.map(drop).map_err(Into::into)
)).await?;
Ok(())
}

/// Force leaves the voice channel in the current server to bypass buggy states
Expand Down
32 changes: 22 additions & 10 deletions src/commands/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ pub async fn settings(ctx: Context<'_>) -> CommandResult {
.replace("{speaking_rate}", &speaking_rate)
.replace("{speaking_rate_kind}", speaking_rate_kind),
false)
)).await.map(drop).map_err(Into::into)
)).await?;

Ok(())
}


Expand Down Expand Up @@ -523,7 +525,9 @@ async fn generic_bool_command(ctx: Context<'_>, key: &'static str, value: Option
let resp = ctx.gettext(resp).replace("{}", to_enabled(ctx.current_catalog(), value));

ctx.data().guilds_db.set_one(ctx.guild_id().unwrap().into(), key, &value).await?;
ctx.say(resp).await.map(drop).map_err(Into::into)
ctx.say(resp).await?;

Ok(())
}

macro_rules! create_bool_command {
Expand Down Expand Up @@ -664,7 +668,9 @@ pub async fn required_role(
}).await
} else {
ctx.say(ctx.gettext("Cancelled!")).await
}.map(drop).map_err(Into::into)
}?;

Ok(())
}

/// Changes the required prefix for TTS.
Expand All @@ -682,11 +688,12 @@ async fn required_prefix(
) -> CommandResult {
let guild_id = ctx.guild_id().unwrap();
if let Some(prefix) = tts_prefix.as_deref() && let Err(err) = check_prefix(&ctx, prefix) {
return ctx.say(err).await.map(drop).map_err(Into::into);
ctx.say(err).await?;
} else {
ctx.data().guilds_db.set_one(guild_id.into(), "required_prefix", &tts_prefix).await?;
ctx.say(ctx.gettext("The required prefix for TTS is now: {}").replace("{}", tts_prefix.as_deref().unwrap_or("`None`"))).await?;
}

ctx.data().guilds_db.set_one(guild_id.into(), "required_prefix", &tts_prefix).await?;
ctx.say(ctx.gettext("The required prefix for TTS is now: {}").replace("{}", tts_prefix.as_deref().unwrap_or("`None`"))).await?;
Ok(())
}

Expand Down Expand Up @@ -778,7 +785,8 @@ pub async fn translation_lang(
String::from(ctx.gettext("Invalid translation language, do `/translation_languages`"))
};

ctx.say(to_say).await.map(drop).map_err(Into::into)
ctx.say(to_say).await?;
Ok(())
}


Expand Down Expand Up @@ -992,9 +1000,11 @@ pub async fn setup(
};

if text_channels.is_empty() {
return ctx.say(ctx.gettext("**Error**: This server doesn't have any text channels that we both have Read/Send Messages in!")).await.map(drop).map_err(Into::into);
ctx.say(ctx.gettext("**Error**: This server doesn't have any text channels that we both have Read/Send Messages in!")).await?;
return Ok(())
} else if text_channels.len() >= (25 * 5) {
return ctx.say(ctx.gettext("**Error**: This server has too many text channels to show in a menu! Please run `/setup #channel`")).await.map(drop).map_err(Into::into);
ctx.say(ctx.gettext("**Error**: This server has too many text channels to show in a menu! Please run `/setup #channel`")).await?;
return Ok(())
};

text_channels.sort_by(|f, s| Ord::cmp(&f.position, &s.position));
Expand Down Expand Up @@ -1152,7 +1162,9 @@ pub async fn translation_languages(ctx: Context<'_>) -> CommandResult {
.footer(CreateEmbedFooter::new(random_footer(
&data.config.main_server_invite, client_id, ctx.current_catalog()
)))
)).await.map(drop).map_err(Into::into)
)).await?;

Ok(())
}

/// Lists all the voices that TTS bot accepts for the current mode
Expand Down
3 changes: 2 additions & 1 deletion src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ where

async fn _get(pool: &sqlx::PgPool, key: CacheKey, select: &'static str) -> Result<Option<Arc<RowT>>> {
let query = key.bind_query_as(sqlx::query_as(select));
query.fetch_optional(pool).await.map(|r| r.map(Arc::new)).map_err(Into::into)
let row = query.fetch_optional(pool).await?;
Ok(row.map(Arc::new))
}

pub async fn get(&self, identifier: CacheKey) -> Result<Arc<RowT>> {
Expand Down
4 changes: 3 additions & 1 deletion src/events/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,5 +304,7 @@ async fn process_support_response(
channel.send_message(ctx, CreateMessage::default()
.content(content)
.embed(CreateEmbed::from(embed))
).await.map(drop).map_err(Into::into)
).await?;

Ok(())
}
7 changes: 4 additions & 3 deletions src/web_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ impl crate::Looper for Updater {
.header(CONTENT_TYPE, "application/json")
.body(json::to_string(&stats)?)
.send().await?
.error_for_status()
.map(drop).map_err(Into::into)
}
.error_for_status()?;

Ok(())
}
}

0 comments on commit f5a60cc

Please sign in to comment.