From de564d2a2bcb8d040b34c6e84966e60d11fa5bfb Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Sat, 29 Jan 2022 05:31:41 -0300 Subject: [PATCH] correctly wrap ffmpeg --- Cargo.lock | 2 +- Cargo.toml | 2 +- default.nix | 8 ++- src/discord/mod.rs | 153 +++++++++++++++++++++------------------------ 4 files changed, 78 insertions(+), 87 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de61f0a..828e289 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -486,7 +486,7 @@ dependencies = [ [[package]] name = "disconic" -version = "0.1.2-pre.3" +version = "0.2.0" dependencies = [ "anyhow", "dotenv", diff --git a/Cargo.toml b/Cargo.toml index db90dea..c71d1b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "disconic" description = "Discord bot for interacting with subsonic music libraries" -version = "0.1.2-pre.3" +version = "0.2.0" authors = [ "Gabriel Fontes " ] edition = "2018" homepage = "https://misterio.me" diff --git a/default.nix b/default.nix index 705c0e1..72e458e 100644 --- a/default.nix +++ b/default.nix @@ -1,4 +1,4 @@ -{ lib, rustPlatform, pkg-config, autoconf, alsa-lib, automake, libopus, ffmpeg }: +{ lib, rustPlatform, pkg-config, autoconf, alsa-lib, automake, libopus, ffmpeg, makeWrapper }: let manifest = (lib.importTOML ./Cargo.toml).package; in rustPlatform.buildRustPackage rec { @@ -8,7 +8,7 @@ in rustPlatform.buildRustPackage rec { src = lib.cleanSource ./.; nativeBuildInputs = [ pkg-config ]; - buildInputs = [ autoconf alsa-lib automake libopus ffmpeg ]; + buildInputs = [ autoconf alsa-lib automake libopus makeWrapper ffmpeg ]; cargoLock = { lockFile = ./Cargo.lock; @@ -17,6 +17,10 @@ in rustPlatform.buildRustPackage rec { }; }; + postFixup = '' + wrapProgram $out/bin/disconic --set PATH ${lib.makeBinPath [ ffmpeg ]} + ''; + meta = with lib; { description = manifest.desciption; homepage = manifest.homepage; diff --git a/src/discord/mod.rs b/src/discord/mod.rs index 77f2f98..02f0bfe 100644 --- a/src/discord/mod.rs +++ b/src/discord/mod.rs @@ -20,7 +20,9 @@ use sunk::{ use crate::MusicClient; #[group] -#[commands(song, random, skip, stop, pause, resume, queue, now_playing, remove)] +#[commands( + song, random, skip, stop, pause, resume, queue, nowplaying, remove, album +)] pub struct General; pub struct Handler; @@ -31,9 +33,8 @@ impl EventHandler for Handler {} #[hook] pub async fn after_hook(ctx: &Context, msg: &Message, cmd_name: &str, error: CommandResult) { if let Err(why) = error { - let text = format!("Error running {}: {:?}", cmd_name, why); - msg.reply(&ctx.http, &text).await.ok(); - eprintln!("{}", &text); + msg.reply(&ctx.http, format!("{:?}", why)).await.ok(); + eprintln!("{}: {:?}", cmd_name, why,); } } @@ -54,17 +55,40 @@ async fn song(ctx: &Context, msg: &Message, args: Args) -> CommandResult { .await? .songs; - match result.first() { - Some(song) => { - queue_song(ctx, msg, &song, &music_client).await?; - Ok(()) - } - None => { - let text = "No song matching search found"; - msg.reply(&ctx.http, text).await?; - Err(anyhow!(text).into()) - } + let song = result + .first() + .ok_or_else(|| anyhow!("No song matching search found"))?; + queue_song(ctx, msg, &song, &music_client).await?; + + Ok(()) +} + +#[command] +#[aliases(a, album)] +/// Play a named album +async fn album(ctx: &Context, msg: &Message, args: Args) -> CommandResult { + let data = ctx.data.read().await; + let music_client = data + .get::() + .expect("Couldn't retrieve music client"); + + let search_size = SearchPage::new().with_size(1); + let ignore = search::NONE; + + let result = music_client + .search(args.rest(), ignore, search_size, ignore) + .await? + .albums; + + let album = result + .first() + .ok_or_else(|| anyhow!("No albums matching search found"))?; + + for song in album.songs(music_client).await? { + queue_song(ctx, msg, &song, &music_client).await?; } + + Ok(()) } #[command] @@ -78,41 +102,12 @@ async fn random(ctx: &Context, msg: &Message) -> CommandResult { let result = Song::random(&music_client, 1).await?; - match result.first() { - Some(song) => { - queue_song(ctx, msg, &song, &music_client).await?; - Ok(()) - } - None => { - let text = "No song found"; - msg.reply(&ctx.http, text).await?; - Err(anyhow!(text).into()) - } - } -} + let song = result + .first() + .ok_or_else(|| anyhow!("No song matching search found"))?; + queue_song(ctx, msg, &song, &music_client).await?; -#[command] -#[aliases(pl)] -/// Play a playlist -async fn playlist(ctx: &Context, msg: &Message) -> CommandResult { - let data = ctx.data.read().await; - let music_client = data - .get::() - .expect("Couldn't retrieve music client"); - - let result = Song::random(&music_client, 1).await?; - - match result.first() { - Some(song) => { - queue_song(ctx, msg, &song, &music_client).await?; - Ok(()) - } - None => { - let text = "No song found"; - msg.reply(&ctx.http, text).await?; - Err(anyhow!(text).into()) - } - } + Ok(()) } #[command] @@ -150,14 +145,9 @@ async fn pause(ctx: &Context, msg: &Message) -> CommandResult { let handler = call.lock().await; let queue = handler.queue(); - let current = match queue.current() { - Some(channel) => channel, - None => { - let text = "Not currently playing"; - msg.reply(ctx, text).await?; - return Err(anyhow!(text).into()); - } - }; + let current = queue + .current() + .ok_or_else(|| anyhow!("Not currently playing"))?; current.pause()?; msg.reply(&ctx.http, "Paused playing").await?; @@ -181,23 +171,19 @@ async fn resume(ctx: &Context, msg: &Message) -> CommandResult { } #[command] -#[aliases(now_playing, now, np, playing)] +#[aliases(nowplaying, now, np, playing)] /// Show currently playing song -async fn now_playing(ctx: &Context, msg: &Message) -> CommandResult { +async fn nowplaying(ctx: &Context, msg: &Message) -> CommandResult { let call = join_channel(&ctx, &msg).await?; let handler = call.lock().await; let queue = handler.queue(); - let current = match queue.current() { - Some(channel) => channel, - None => { - let text = "Not currently playing"; - msg.reply(ctx, text).await?; - return Err(anyhow!(text).into()); - } - }; - let text = song_message(None, current.metadata()); - msg.reply(&ctx.http, text).await?; + let current = queue + .current() + .ok_or_else(|| anyhow!("Not currently playing"))?; + + msg.reply(&ctx.http, song_message(None, current.metadata())) + .await?; Ok(()) } @@ -235,18 +221,20 @@ async fn remove(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { let index = args.single()?; let queue = handler.queue(); - let text = if let Some(track) = queue.current_queue().get(index) { - queue.dequeue(index); - let metadata = track.metadata(); - format!( - "Removed track: {} - {} ", - metadata.artist.to_owned().unwrap_or_default(), - metadata.track.to_owned().unwrap_or_default(), - ) - } else { - "No song with that index".into() - }; + let current_queue = queue.current_queue(); + let track = current_queue + .get(index) + .ok_or_else(|| anyhow!("No song with that index"))?; + + queue.dequeue(index); + + let metadata = track.metadata(); + let text = format!( + "Removed track: {} - {} ", + metadata.artist.to_owned().unwrap_or_default(), + metadata.track.to_owned().unwrap_or_default(), + ); msg.reply(&ctx.http, text).await?; Ok(()) } @@ -322,9 +310,9 @@ async fn join_channel( let connect_to = match caller_channel { Some(channel) => channel, None => { - let text = "You must be in a voice channel to use this command"; - msg.reply(ctx, text).await?; - return Err(anyhow!(text)); + return Err(anyhow!( + "You must be in a voice channel to use this command" + )); } }; @@ -335,6 +323,5 @@ async fn join_channel( async fn load_song(song: &Song, client: &sunk::Client) -> Result { let url = song.stream_url(&client)?; - println!("{:?}", url); Ok(songbird::ffmpeg(url).await?) }