Skip to content

Commit

Permalink
Rewrite error messages to be manual instead of formatted
Browse files Browse the repository at this point in the history
This helps translation and makes them read better
  • Loading branch information
GnomedDev committed Oct 6, 2023
1 parent f5a60cc commit 96f3ee8
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 115 deletions.
54 changes: 27 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 12 additions & 22 deletions src/commands/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ async fn channel_check(ctx: &Context<'_>, author_vc: Option<serenity::ChannelId>
if setup_id == channel_id.get() as i64 || author_vc == Some(channel_id) {
Ok(true)
} else {
ctx.send_error(
ctx.gettext("you ran this command in the wrong channel"),
Some(ctx.gettext("do `/channel` get the channel that has been setup"))
).await?;
let msg = ctx.gettext("You ran this command in the wrong channel, please move to <#{channel_id}>.")
.replace("{channel_id}", &setup_id.to_string());

ctx.send_error(msg).await?;
Ok(false)
}
}
Expand All @@ -50,8 +50,7 @@ async fn channel_check(ctx: &Context<'_>, author_vc: Option<serenity::ChannelId>
)]
pub async fn join(ctx: Context<'_>) -> CommandResult {
let author_vc = require!(ctx.author_vc(), ctx.send_error(
ctx.gettext("you need to be in a voice channel to make me join your voice channel"),
Some(ctx.gettext("join a voice channel and try again")),
ctx.gettext("I cannot join your voice channel unless you are in one!").to_owned()
).await.map(drop));

if !channel_check(&ctx, Some(author_vc)).await? {
Expand All @@ -67,11 +66,8 @@ 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() {
ctx.send_error(
ctx.gettext("I am timed out"),
Some(ctx.gettext("ask a moderator to remove the timeout"))
).await?;

let msg = ctx.gettext("I am timed out, please ask a moderator to remove the timeout");
ctx.send_error(msg.to_owned()).await?;
return Ok(())
}
}
Expand All @@ -85,14 +81,10 @@ pub async fn join(ctx: Context<'_>) -> CommandResult {
channel.permissions_for_user(ctx, bot_id)?;

if !missing_permissions.is_empty() {
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?;
let msg = ctx.gettext("I do not have permission to TTS in your voice channel, please ask a server administrator to give me: {missing_permissions}")
.replace("{missing_permissions}", &missing_permissions.get_permission_names().join(", "));

ctx.send_error(msg).await?;
return Ok(())
}

Expand All @@ -118,10 +110,8 @@ pub async fn join(ctx: Context<'_>) -> CommandResult {

if let Err(err) = join_vc_result {
return if let JoinError::TimedOut = err {
ctx.send_error(
ctx.gettext("a timeout occurred while joining your voice channel"),
Some(ctx.gettext("wait a few seconds and try again"))
).await?;
let msg = ctx.gettext("I failed to join your voice channel, please check I have the right permissions and try again!");
ctx.send_error(msg.to_owned()).await?;
Ok(())
} else {
Err(err.into())
Expand Down
72 changes: 36 additions & 36 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,9 @@ pub async fn handle_guild(name: &str, ctx: &serenity::Context, framework: Framew
// Command Error handlers
async fn handle_cooldown(ctx: Context<'_>, remaining_cooldown: std::time::Duration) -> Result<(), Error> {
let cooldown_response = ctx.send_error(
&ctx.gettext("{command_name} is on cooldown").replace("{command_name}", &ctx.command().name),
Some(&ctx.gettext("try again in {} seconds").replace("{}", &format!("{:.1}", remaining_cooldown.as_secs_f32())))
ctx.gettext("`/{command_name}` is on cooldown, please try again in {} seconds!")
.replace("{command_name}", &ctx.command().name)
.replace("{}", &format!("{:.1}", remaining_cooldown.as_secs_f32()))
).await?;

if let poise::Context::Prefix(ctx) = ctx {
Expand All @@ -248,28 +249,26 @@ async fn handle_cooldown(ctx: Context<'_>, remaining_cooldown: std::time::Durati
}

async fn handle_argparse(ctx: Context<'_>, error: Box<dyn std::error::Error + Send + Sync>, input: Option<String>) -> Result<(), Error> {
let mut reason = None;
let fix = &ctx
.gettext("check out `/help {command}`")
.replace("{command}", &ctx.command().qualified_name);

if error.is::<serenity::MemberParseError>() {
reason = Some(ctx.gettext("I cannot find the member: `{}`"));
let reason = if error.is::<serenity::MemberParseError>() {
ctx.gettext("I cannot find the member: `{}`")
} else if error.is::<serenity::GuildParseError>() {
reason = Some(ctx.gettext("I cannot find the server: `{}`"));
ctx.gettext("I cannot find the server: `{}`")
} else if error.is::<serenity::GuildChannelParseError>() {
reason = Some(ctx.gettext("I cannot find the channel: `{}`"));
ctx.gettext("I cannot find the channel: `{}`")
} else if error.is::<std::num::ParseIntError>() {
reason = Some(ctx.gettext("I cannot convert `{}` to a number"));
ctx.gettext("I cannot convert `{}` to a number")
} else if error.is::<std::str::ParseBoolError>() {
reason = Some(ctx.gettext("I cannot convert `{}` to True/False"));
}
ctx.gettext("I cannot convert `{}` to True/False")
} else {
ctx.gettext("I cannot understand your message")
};

ctx.send_error(
reason.map(|r| r.replace("{}", &input.unwrap()).replace('`', "")).as_deref().unwrap_or("you typed the command wrong"),
Some(fix)
).await?;
let reason = reason.replace("{}", &input.unwrap());
let fix = ctx
.gettext("please check out `/help {command}`")
.replace("{command}", &ctx.command().qualified_name);

ctx.send_error(format!("{reason} {fix}")).await?;
Ok(())
}

Expand Down Expand Up @@ -319,31 +318,35 @@ pub async fn handle(error: poise::FrameworkError<'_, Data, Error>) -> Result<()>
Some(author.name.clone()), Some(author.face())
).await?;

ctx.send_error("an unknown error occurred", None).await?;
let msg = ctx.gettext("An unknown error occurred, please report this on the support server!");
ctx.send_error(msg.to_owned()).await?;
}
poise::FrameworkError::ArgumentParse { error, ctx, input, .. } => handle_argparse(ctx, error, input).await?,
poise::FrameworkError::CooldownHit { remaining_cooldown, ctx, .. } => handle_cooldown(ctx, remaining_cooldown).await?,
poise::FrameworkError::MissingBotPermissions{missing_permissions, ctx, .. } => {
ctx.send_error(
&ctx.gettext("I cannot run `{command}` as I am missing permissions").replace("{command}", &ctx.command().name),
Some(&ctx.gettext("give me: {}").replace("{}", &missing_permissions.get_permission_names().join(", ")))
).await?;
let msg = ctx.gettext("I cannot run this command as I am missing permissions, please ask an administrator of the server to give me: {}")
.replace("{}", &missing_permissions.get_permission_names().join(", "));

ctx.send_error(msg).await?;
},
poise::FrameworkError::MissingUserPermissions{missing_permissions, ctx, ..} => {
ctx.send_error(
ctx.gettext("you cannot run this command"),
missing_permissions.map(|missing_permissions| (ctx
.gettext("ask an administrator for the following permissions: {}")
let msg = if let Some(missing_permissions) = missing_permissions {
ctx.gettext("You cannot run this command as you are missing permissions, please ask an administrator of the server to give you: {}")
.replace("{}", &missing_permissions.get_permission_names().join(", "))
)).as_deref()
).await?;
} else {
ctx.gettext("You cannot run this command as you are missing permissions.").to_owned()
};

ctx.send_error(msg).await?;
},

poise::FrameworkError::Setup { .. } => panic!("{error:#?}"),
poise::FrameworkError::CommandCheckFailed { error, ctx, .. } => {
if let Some(error) = error {
error!("Premium Check Error: {:?}", error);
ctx.send_error(ctx.gettext("an unknown error occurred during the premium check"), None).await?;

let msg = ctx.gettext("An unknown error occurred during the premium check, please report this on the support server!");
ctx.send_error(msg.to_owned()).await?;
}
},

Expand Down Expand Up @@ -389,14 +392,11 @@ pub async fn handle(error: poise::FrameworkError<'_, Data, Error>) -> Result<()>
poise::FrameworkError::UnknownCommand { .. } => {},
poise::FrameworkError::GuildOnly {ctx, ..} => {
let error = ctx
.gettext("{command_name} cannot be used in private messages")
.gettext("`/{command_name}` cannot be used in private messages, please run this command in a server channel.")
.replace("{bot_name}", &ctx.cache().current_user().name)
.replace("{command_name}", &ctx.command().qualified_name);

let fix = ctx
.gettext("try running it on a server with {bot_name} in")
.replace("{bot_name}", &ctx.serenity_context().cache.current_user().name);

ctx.send_error(&error, Some(&fix)).await?;
ctx.send_error(error).await?;
},
poise::FrameworkError::CommandPanic { .. } => panic!("Command panicked!"),
poise::FrameworkError::__NonExhaustive(_) => unreachable!(),
Expand Down
Loading

0 comments on commit 96f3ee8

Please sign in to comment.