Skip to content

Commit

Permalink
add /config translate command
Browse files Browse the repository at this point in the history
  • Loading branch information
tazz4843 committed Dec 11, 2023
1 parent 4ebb106 commit 702db73
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Add migration script here
ALTER TABLE guilds ALTER COLUMN translate SET DEFAULT true;
22 changes: 21 additions & 1 deletion scripty_commands/src/cmds/config/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serenity::builder::CreateEmbed;
slash_command,
check = "is_guild",
required_permissions = "MANAGE_GUILD",
rename = "guild_language"
rename = "language"
)]
pub async fn config_server_language(
ctx: Context<'_>,
Expand All @@ -27,6 +27,26 @@ pub async fn config_server_language(
.ok_or_else(Error::expected_guild)?;
let resolved_language = scripty_i18n::get_guild_language(guild_id).await;

// check if the server has translate enabled and error out if so
if language != "en" {
let res = sqlx::query!(
"SELECT translate FROM guilds WHERE guild_id = $1",
guild_id as i64
)
.fetch_optional(scripty_db::get_db())
.await?;
if let Some(row) = res {
if row.translate {
ctx.reply(format_message!(
resolved_language,
"guild-language-set-failure-translate-enabled"
))
.await?;
return Ok(());
}
}
}

match scripty_i18n::set_guild_language(guild_id, language.as_str()).await {
Ok(_) => {
ctx.send(
Expand Down
2 changes: 2 additions & 0 deletions scripty_commands/src/cmds/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod transcribe_audio;
mod transcribe_only_role;
mod transcribe_video;
mod transcribe_voice_messages;
mod translate;
mod verbose;

pub use auto_detect_lang::config_auto_detect_lang;
Expand All @@ -15,6 +16,7 @@ pub use transcribe_audio::config_transcribe_audio;
pub use transcribe_only_role::config_transcribe_only_role;
pub use transcribe_video::config_transcribe_video;
pub use transcribe_voice_messages::config_transcribe_voice_messages;
pub use translate::config_translate;
pub use verbose::config_verbose;

/// Configure Scripty's settings
Expand Down
51 changes: 51 additions & 0 deletions scripty_commands/src/cmds/config/translate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use scripty_bot_utils::{checks::is_guild, Context, Error};

/// Automatically translate transcriptions to English?
#[poise::command(
prefix_command,
slash_command,
check = "is_guild",
required_permissions = "MANAGE_GUILD",
rename = "translate"
)]
pub async fn config_translate(
ctx: Context<'_>,
#[description = "Defaults to false"] translate: bool,
) -> Result<(), Error> {
let guild_id = ctx
.guild_id()
.map(|g| g.get())
.ok_or_else(Error::expected_guild)?;
let resolved_language =
scripty_i18n::get_resolved_language(ctx.author().id.get(), Some(guild_id)).await;

if resolved_language.language != "en" {
ctx.say(format_message!(
resolved_language,
"config-translate-not-english"
))
.await?;
return Ok(());
}

sqlx::query!(
"INSERT INTO guilds (guild_id, translate) VALUES ($1, $2) ON CONFLICT (guild_id) DO \
UPDATE SET translate = $2",
guild_id as i64,
translate
)
.execute(scripty_db::get_db())
.await?;

ctx.say(format_message!(
resolved_language,
if translate {
"config-translate-enabled"
} else {
"config-translate-disabled"
}
))
.await?;

Ok(())
}
1 change: 1 addition & 0 deletions scripty_commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub fn build_commands() -> Vec<poise::Command<Data, Error>> {
cmds::config::config_verbose(),
cmds::config::config_auto_detect_lang(),
cmds::config::config_transcribe_only_role(),
cmds::config::config_translate(),
],
subcommand_required: true,
..cmds::config::config_root()
Expand Down
10 changes: 10 additions & 0 deletions scripty_i18n/locales/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ config_transcribe_only_role = transcribe_only_role
config-transcribe-only-role-enabled = Scripty will now only transcribe messages from users in { $roleId }.
config-transcribe-only-role-disabled = Scripty will now transcribe all users, regardless of role.
## config - translate command
config_translate = translate
.description = Automatically translate transcriptions to English?
.translate = translate
.translate-description = Defaults to false
config-translate-enabled = Scripty will now translate transcriptions to English.
config-translate-disabled = Scripty will now attempt to match the phrases being spoken to English words, but will not translate.
## Help menu translation strings

command-not-found = No command with name `{ $commandName }` found.
Expand Down Expand Up @@ -217,6 +226,7 @@ language-set-failure-description-invalid = The language you specified is an inva
language-set-failure-title-db = Database error.
# This message is shown as the embed description when the database returns an error when setting the language for an entity.
language-set-failure-description-db = The database encountered an error while attempting to set your language. This error has been reported, and we'll look into it. Please do not spam this command. (If you're curious, here's the error: { $error })
guild-language-set-failure-translate-enabled = Your server has auto-translation enabled. This is only supported when translating to English. Disable this feature if you want to set your language.
## Command invocation contexts

Expand Down

0 comments on commit 702db73

Please sign in to comment.