Skip to content

Commit

Permalink
add translate
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacherr committed Jun 25, 2024
1 parent 4e85999 commit 2d66dc6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 35 deletions.
48 changes: 48 additions & 0 deletions assyst-core/src/command/fun/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,50 @@
use std::time::Duration;

use anyhow::Context;
use assyst_proc_macro::command;

use crate::command::{Availability, Category};
use crate::rest::audio_identification::identify_song_notsoidentify;

use super::arguments::ImageUrl;
use super::CommandCtxt;

pub mod colour;
pub mod translation;

#[command(
description = "Find a song in a video",
cooldown = Duration::from_secs(2),
access = Availability::Public,
category = Category::Fun,
usage = "[video]",
examples = ["https://link.to.my/video.mp4"],
send_processing = true
)]
pub async fn findsong(ctxt: CommandCtxt<'_>, input: ImageUrl) -> anyhow::Result<()> {
let result = identify_song_notsoidentify(ctxt.assyst().clone(), input.0)
.await
.context("Failed to identify song")?;

if result.len() > 0 {
let formatted = format!(
"**Title:** {}\n**Artist(s):** {}\n**YouTube Link:** <{}>",
result[0].title.clone(),
result[0]
.artists
.iter()
.map(|x| x.name.clone())
.collect::<Vec<_>>()
.join(", "),
match &result[0].platforms.youtube {
Some(x) => x.url.clone(),
None => "Unknown".to_owned(),
}
);
ctxt.reply(formatted).await?;
} else {
ctxt.reply("No results found").await?;
}

Ok(())
}
22 changes: 19 additions & 3 deletions assyst-core/src/command/fun/translation.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
use std::time::Duration;

use anyhow::{bail, Context};
use assyst_common::markdown::Markdown;
use assyst_common::util::table;
use assyst_proc_macro::command;

use crate::command::arguments::{Rest, Word};
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, translate_single, TranslateResult, Translation,
bad_translate as bad_translate_default, bad_translate_with_count, get_languages, translate_single, TranslateResult,
Translation,
};

#[command(
name = "badtranslate",
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"],
usage = "[text|\"languages\"]",
examples = ["hello i love assyst", "languages"],
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<()> {
if text.0 == "languages" {
let languages = get_languages(&ctxt.assyst().reqwest_client)
.await
.context("Failed to fetch translation languages")?;

let formatted = table::generate_list("Code", "Name", &languages);

ctxt.reply(formatted.codeblock("")).await?;

return Ok(());
};

let TranslateResult {
result: Translation { text, .. },
translations,
Expand Down
47 changes: 18 additions & 29 deletions assyst-core/src/command/misc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::time::{Duration, Instant};

use crate::command::Availability;
use crate::rest::audio_identification::identify_song_notsoidentify;
use crate::rest::eval::fake_eval;

use super::arguments::{Image, ImageUrl, Rest, Word};
use super::{Category, CommandCtxt};

use anyhow::Context;
use assyst_common::ansi::Ansi;
use assyst_common::eval::FakeEvalImageResponse;
use assyst_common::markdown::Markdown;
use assyst_common::util::format_duration;
use assyst_common::util::process::exec_sync;
Expand Down Expand Up @@ -105,37 +106,25 @@ pub async fn exec(ctxt: CommandCtxt<'_>, script: Rest) -> anyhow::Result<()> {
}

#[command(
description = "Find a song in a video",
cooldown = Duration::from_secs(2),
description = "evaluate javascript code",
cooldown = Duration::from_millis(1),
access = Availability::Public,
category = Category::Wsi,
usage = "[video]",
examples = ["https://link.to.my/video.mp4"],
send_processing = true
category = Category::Misc,
usage = "[script]",
examples = ["1"]
)]
pub async fn findsong(ctxt: CommandCtxt<'_>, input: ImageUrl) -> anyhow::Result<()> {
let result = identify_song_notsoidentify(ctxt.assyst().clone(), input.0)
pub async fn eval(ctxt: CommandCtxt<'_>, script: Rest) -> anyhow::Result<()> {
let result = fake_eval(&ctxt.assyst(), script.0, true, ctxt.data.message, Vec::new())
.await
.context("Failed to identify song")?;

if result.len() > 0 {
let formatted = format!(
"**Title:** {}\n**Artist(s):** {}\n**YouTube Link:** <{}>",
result[0].title.clone(),
result[0]
.artists
.iter()
.map(|x| x.name.clone())
.collect::<Vec<_>>()
.join(", "),
match &result[0].platforms.youtube {
Some(x) => x.url.clone(),
None => "Unknown".to_owned(),
}
);
ctxt.reply(formatted).await?;
} else {
ctxt.reply("No results found").await?;
.context("Evaluation failed")?;

match result {
FakeEvalImageResponse::Image(im, _) => {
ctxt.reply(im).await?;
},
FakeEvalImageResponse::Text(text) => {
ctxt.reply(text.message.codeblock("js")).await?;
},
}

Ok(())
Expand Down
7 changes: 4 additions & 3 deletions assyst-core/src/command/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ macro_rules! declare_commands {
}

declare_commands!(
fun::colour::colour_command,
fun::findsong_command,
fun::translation::bad_translate_command,
fun::translation::translate_command,
fun::colour::colour_command,
misc::enlarge_command,
misc::eval_command,
misc::exec_command,
misc::run::run_command,
misc::help::help_command,
misc::ping_command,
misc::remind::remind_command,
misc::run::run_command,
misc::stats::stats_command,
misc::tag::tag_command,
misc::url_command,
misc::findsong_command,
services::burntext_command,
services::download_command,
services::r34_command,
Expand Down

0 comments on commit 2d66dc6

Please sign in to comment.