Skip to content

Commit

Permalink
add setloop
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacherr committed Aug 27, 2024
1 parent 2ed1deb commit 58229c6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
27 changes: 27 additions & 0 deletions assyst-core/src/command/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ pub trait ParseArgument: Sized {
}
}

impl ParseArgument for i64 {
async fn parse_raw_message(ctxt: &mut RawMessageParseCtxt<'_>, label: Label) -> Result<Self, TagParseError> {
let word = ctxt.next_word(label)?;
Ok(word.parse()?)
}

async fn parse_command_option(
ctxt: &mut InteractionCommandParseCtxt<'_>,
label: Label,
) -> Result<Self, TagParseError> {
let next = &ctxt.option_by_name(&label.unwrap().0)?.value;
if let CommandOptionValue::Integer(option) = next {
Ok(*option as i64)
} else {
// cloning is fine since this should (ideally) never happen
Err(TagParseError::MismatchedCommandOptionType((
"i64".to_owned(),
next.clone(),
)))
}
}

fn as_command_option(name: &str) -> CommandOption {
IntegerBuilder::new(name, "integer option").required(true).build()
}
}

impl ParseArgument for u64 {
async fn parse_raw_message(ctxt: &mut RawMessageParseCtxt<'_>, label: Label) -> Result<Self, TagParseError> {
let word = ctxt.next_word(label)?;
Expand Down
21 changes: 21 additions & 0 deletions assyst-core/src/command/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,27 @@ pub async fn scramble(ctxt: CommandCtxt<'_>, source: Image) -> anyhow::Result<()
Ok(())
}

#[command(
description = "set how many times a gif loops",
aliases = ["loop"],
cooldown = Duration::from_secs(6),
access = Availability::Public,
category = Category::Image,
usage = "[image] [loop count (-1 for infinite)]",
examples = ["https://link.to.my/image.png 3", "https://link.to.my/image.png -1"],
send_processing = true
)]
pub async fn setloop(ctxt: CommandCtxt<'_>, source: Image, loops: i64) -> anyhow::Result<()> {
let result = ctxt
.flux_handler()
.set_loop(source.0, ctxt.data.author.id.get(), ctxt.data.guild_id.map(|x| x.get()), loops)
.await?;

ctxt.reply(result).await?;

Ok(())
}

#[command(
description = "speed up or slow down a gif or video",
aliases = ["gspeed", "gifspeed"],
Expand Down
1 change: 1 addition & 0 deletions assyst-core/src/command/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ declare_commands!(
image::reverse_command,
image::rotate_command,
image::scramble_command,
image::setloop_command,
image::speechbubble::speechbubble_command,
image::speed_command,
image::spin_command,
Expand Down
14 changes: 14 additions & 0 deletions assyst-flux-iface/src/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,20 @@ impl FluxHandler {
self.run_flux(request, limits.time).await
}

pub async fn set_loop(&self, media: Vec<u8>, user_id: u64, guild_id: Option<u64>, loops: i64) -> FluxResult {
let limits = self.get_request_limits(user_id, guild_id).await?;

let mut request = FluxRequest::new_with_input_and_limits(media, &limits);

let mut options = HashMap::new();
options.insert("loops".to_owned(), loops.to_string());

request.operation("set-loop".to_owned(), options);
request.output();

self.run_flux(request, limits.time).await
}

pub async fn scramble(&self, media: Vec<u8>, user_id: u64, guild_id: Option<u64>) -> FluxResult {
let limits = self.get_request_limits(user_id, guild_id).await?;

Expand Down
6 changes: 3 additions & 3 deletions assyst-flux-iface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ impl FluxHandler {

if !output.status.success() {
bail!(
"Something went wrong ({}): {}",
output.status.to_string(),
string_from_likely_utf8(output.stderr).trim()
"{} ({})",
string_from_likely_utf8(output.stderr).trim(),
output.status.to_string()
);
}

Expand Down

0 comments on commit 58229c6

Please sign in to comment.