Skip to content

Commit

Permalink
feat: add ytplay command
Browse files Browse the repository at this point in the history
  • Loading branch information
Plarpoon committed Apr 20, 2024
1 parent 9b66860 commit 26d08bc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ tokio = { version = "1.37.0", features = ["full", "rt-multi-thread"] }
env_logger = "0.11.3"
anyhow = "1.0.82"
regex = "1.10.4"
lavalink-rs = "0.11.0"
songbird = "0.4.1"
youtube_dl = "0.10.0"
ffmpeg-the-third = "1.2.2+ffmpeg-6.0"
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Welcome to the repository for **EvilBunny**, a robust and versatile Discord bot
- **Built with Serenity:** EvilBunny is constructed using the widely-used and well-documented Serenity library in Rust, ensuring reliability and user-friendly operation.
- **Versioning:** There are two versions of EvilBunny available. The stable version is named "EvilBunny", and the development version is named "Red". This distinction allows users to select the version that best fits their requirements.

## 📦 System Dependencies

This package depends on `libavutil`. Make sure to install it on your system before building the project. In Fedora, you can install it by installing the `ffmpeg-devel` package from the RPMFusion repository.

## 🚀 Usage

To use EvilBunny, clone this repository and install the necessary Rust packages. Set your bot token in the `.env` file. Start the bot and use the `help` command to view the list of available commands. For detailed information about a specific command, use the `help [command]` command.
Expand Down
28 changes: 20 additions & 8 deletions src/commands/youtube/ytplay.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
use crate::Context;
use poise::serenity_prelude::client::Context as SerenityContext;
use poise::serenity_prelude::model::id::GuildId;
use songbird::input::ffmpeg;
use songbird::SerenityInit;
use youtube_dl::YoutubeDl;

/// Description of the new function
///
/// This command allows you to play a YouTube video in VC.
/// ```
/// ~ytplay
/// ```
#[poise::command(prefix_command, slash_command)]
pub async fn ytplay(ctx: Context<'_>) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
pub async fn ytplay(
ctx: Context<'_>,
url: String,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
ctx.defer().await?;

// ...
// Fetch audio stream from YouTube video
let video = YoutubeDl::new(&url).run().await?;
let audio = video.get_best_audio().ok_or("No audio found")?;

// Connect to voice channel and play audio
let manager = songbird::get(&ctx.discord).await.expect("Songbird Voice client placed in at initialisation.");
let handler_lock = manager.get(ctx.guild_id).ok_or("Not in a voice channel")?;
let mut handler = handler_lock.lock().await;

let source = ffmpeg(audio.url).await?;
handler.play_source(source);

Ok(())
}

0 comments on commit 26d08bc

Please sign in to comment.