-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jacherr
committed
Jun 25, 2024
1 parent
f989cb6
commit a1c6a63
Showing
11 changed files
with
237 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod colour; | ||
pub mod translation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::time::Duration; | ||
|
||
use anyhow::{bail, Context}; | ||
use assyst_proc_macro::command; | ||
|
||
use crate::command::arguments::Rest; | ||
use crate::command::flags::BadTranslateFlags; | ||
use crate::command::{Availability, Category, CommandCtxt}; | ||
use crate::rest::bad_translation::{ | ||
bad_translate as bad_translate_default, bad_translate_with_count, TranslateResult, Translation, | ||
}; | ||
|
||
#[command( | ||
aliases = ["bt"], | ||
description = "Badly translate some text", | ||
access = Availability::Public, | ||
cooldown = Duration::from_secs(5), | ||
category = Category::Fun, | ||
usage = "[text]", | ||
examples = ["hello i love assyst"], | ||
flag_descriptions = [ | ||
("chain", "Show language chain"), | ||
("count", "Set the amount of translations to perform") | ||
], | ||
send_processing = true | ||
)] | ||
pub async fn bad_translate(ctxt: CommandCtxt<'_>, text: Rest, flags: BadTranslateFlags) -> anyhow::Result<()> { | ||
let TranslateResult { | ||
result: Translation { text, .. }, | ||
translations, | ||
} = if let Some(count) = flags.count { | ||
if count < 10 { | ||
bad_translate_with_count(&ctxt.assyst().reqwest_client, &text.0, count as u32) | ||
.await | ||
.context("Failed to run bad translation")? | ||
} else { | ||
bail!("Translation count cannot exceed 10") | ||
} | ||
} else { | ||
bad_translate_default(&ctxt.assyst().reqwest_client, &text.0) | ||
.await | ||
.context("Failed to run bad translation")? | ||
}; | ||
|
||
let mut output = format!("**Output:**\n{}", text); | ||
|
||
if flags.chain { | ||
output += "\n\n**Language chain:**\n"; | ||
|
||
for (idx, translation) in translations.iter().enumerate() { | ||
output += &format!("{}) {}: {}\n", idx + 1, translation.lang, translation.text); | ||
} | ||
} | ||
|
||
ctxt.reply(output).await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
use std::fmt::Display; | ||
|
||
use assyst_common::config::CONFIG; | ||
use reqwest::{Client, Error as ReqwestError}; | ||
use serde::Deserialize; | ||
use std::error::Error; | ||
|
||
const MAX_ATTEMPTS: u8 = 5; | ||
|
||
mod routes { | ||
pub const LANGUAGES: &str = "/languages"; | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum TranslateError { | ||
Reqwest(ReqwestError), | ||
Raw(&'static str), | ||
} | ||
|
||
impl Display for TranslateError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
TranslateError::Reqwest(_) => write!(f, "A network error occurred"), | ||
TranslateError::Raw(s) => write!(f, "{}", s), | ||
} | ||
} | ||
} | ||
|
||
impl Error for TranslateError {} | ||
|
||
#[derive(Deserialize)] | ||
pub struct Translation { | ||
pub lang: String, | ||
pub text: String, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct TranslateResult { | ||
pub translations: Vec<Translation>, | ||
pub result: Translation, | ||
} | ||
|
||
async fn translate_retry( | ||
client: &Client, | ||
text: &str, | ||
target: Option<&str>, | ||
count: Option<u32>, | ||
additional_data: Option<&[(&str, String)]>, | ||
) -> Result<TranslateResult, TranslateError> { | ||
let mut query_args = vec![("text", text.to_owned())]; | ||
|
||
if let Some(target) = target { | ||
query_args.push(("target", target.to_owned())); | ||
} | ||
|
||
if let Some(count) = count { | ||
query_args.push(("count", count.to_string())); | ||
} | ||
|
||
if let Some(data) = additional_data { | ||
for (k, v) in data.into_iter() { | ||
query_args.push((k, v.to_string())); | ||
} | ||
} | ||
|
||
client | ||
.get(&CONFIG.urls.bad_translation) | ||
.query(&query_args) | ||
.send() | ||
.await | ||
.map_err(TranslateError::Reqwest)? | ||
.json() | ||
.await | ||
.map_err(TranslateError::Reqwest) | ||
} | ||
|
||
async fn translate( | ||
client: &Client, | ||
text: &str, | ||
target: Option<&str>, | ||
count: Option<u32>, | ||
additional_data: Option<&[(&str, String)]>, | ||
) -> Result<TranslateResult, TranslateError> { | ||
let mut attempt = 0; | ||
|
||
while attempt <= MAX_ATTEMPTS { | ||
match translate_retry(client, text, target, count, additional_data).await { | ||
Ok(result) => return Ok(result), | ||
Err(e) => eprintln!("Proxy failed! {:?}", e), | ||
}; | ||
|
||
attempt += 1; | ||
} | ||
|
||
Err(TranslateError::Raw("BT Failed: Too many attempts")) | ||
} | ||
|
||
pub async fn bad_translate(client: &Client, text: &str) -> Result<TranslateResult, TranslateError> { | ||
translate(client, text, None, None, None).await | ||
} | ||
|
||
pub async fn bad_translate_with_count( | ||
client: &Client, | ||
text: &str, | ||
count: u32, | ||
) -> Result<TranslateResult, TranslateError> { | ||
translate(client, text, None, Some(count), None).await | ||
} | ||
|
||
pub async fn translate_single(client: &Client, text: &str, target: &str) -> Result<TranslateResult, TranslateError> { | ||
translate(client, text, Some(target), Some(1), None).await | ||
} | ||
|
||
pub async fn get_languages(client: &Client) -> Result<Vec<(Box<str>, Box<str>)>, TranslateError> { | ||
client | ||
.get(format!("{}{}", CONFIG.urls.bad_translation, routes::LANGUAGES)) | ||
.send() | ||
.await | ||
.map_err(TranslateError::Reqwest)? | ||
.json() | ||
.await | ||
.map_err(TranslateError::Reqwest) | ||
} | ||
|
||
pub async fn validate_language(client: &Client, provided_language: &str) -> Result<bool, TranslateError> { | ||
let languages = get_languages(client).await?; | ||
Ok(languages.iter().any(|(language, _)| &**language == provided_language)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod audio_identification; | ||
pub mod bad_translation; | ||
pub mod cooltext; | ||
pub mod eval; | ||
pub mod filer; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters