feat: implemented general player using yt-dlp

This commit is contained in:
Ladislav Hano 2025-01-09 18:57:42 +01:00
parent 452ac919a0
commit d18e79696a
10 changed files with 115 additions and 30 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "moover_rust" name = "moover_rust"
version = "0.3.0" version = "3.3.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -1,8 +1,6 @@
use poise; use poise;
// use super::super::types::Data;
type Error = Box<dyn std::error::Error + Send + Sync>; use crate::types::{Context, Error};
type Context<'a> = poise::Context<'a, (), Error>;
#[poise::command( #[poise::command(
slash_command, slash_command,

View file

@ -0,0 +1,63 @@
use std::vec;
use songbird::input::YoutubeDl;
use crate::commands::util::connect;
use crate::util::poise_context_extension::ContextExt;
use crate::types::{Context, Error};
use crate::commands::voice::util::autocomplete_channel;
// TODO: search, queue
#[poise::command(
slash_command,
description_localized("en-US", "Plays music from supported URL")
)]
pub async fn play(ctx: Context<'_>,
#[autocomplete = "autocomplete_channel"]
#[description = "Voice channel name: "]
channel: Option<String>,
#[description = "Source URL: "]
url: String,
) -> Result<(), Error> {
if ctx.guild().is_none() {
ctx.reply_ephemeral("Can't use this outside of guild").await?;
return Ok(());
}
let manager = songbird::get(ctx.serenity_context())
.await
.expect("Songbird Voice client placed in at initialisation.")
.clone();
let Some(guild_id) = ctx.guild_id() else {
ctx.reply_ephemeral("Guild id not found").await?;
return Ok(())
};
let http_client = &ctx.data().http_client;
if manager.get(guild_id).is_none() {
match connect(&ctx, guild_id, channel).await {
Ok(_) => (),
Err(e) => {
ctx.reply_ephemeral(&e.to_string()).await?;
return Ok(())
}
}
}
if let Some(handler_lock) = manager.get(guild_id) {
let mut handler = handler_lock.lock().await;
let src = YoutubeDl::new(http_client.clone(), url);
handler.enqueue_input(src.into()).await;
}
else {
ctx.reply_ephemeral("Not in a voice channel").await?;
return Ok(())
}
ctx.reply_ephemeral("Done!").await?;
Ok(())
}

View file

@ -0,0 +1,3 @@
pub use general_player::*;
pub mod general_player;

View file

@ -1,5 +1,5 @@
pub mod util; pub mod util;
pub mod player; pub mod player_common;
pub mod radio; pub mod radio;
// pub mod spotify; // pub mod spotify;
// pub mod yt; pub mod general;

View file

@ -9,18 +9,9 @@ use crate::types::{Context, Error};
use super::util::{connect, autocomplete_channel}; use super::util::{connect, autocomplete_channel};
// #[poise::command( /**
// slash_command, * Common commands that are the same for every implementation
// description_localized("en-US", "Play song") */
// )]
// pub async fn play(ctx: Context<'_>,
// #[description = "Song: "]
// _url: String
// ) -> Result<(), Error> {
// ctx.reply("Done!").await?;
// Ok(())
// }
#[poise::command( #[poise::command(
slash_command, slash_command,
@ -31,7 +22,7 @@ pub async fn disconnect(
) -> Result<(), Error> { ) -> Result<(), Error> {
let Some(guild_id) = ctx.guild_id() else { let Some(guild_id) = ctx.guild_id() else {
ctx.reply("Can't use this outside of guild").await?; ctx.reply_ephemeral("Can't use this outside of guild").await?;
return Ok(()); return Ok(());
}; };
@ -49,7 +40,7 @@ pub async fn disconnect(
match manager.remove(guild_id).await { match manager.remove(guild_id).await {
Ok(()) => { Ok(()) => {
ctx.reply("Disconnected").await?; ctx.reply_ephemeral("Disconnected").await?;
return Ok(()) return Ok(())
} }
Err(e) => { Err(e) => {

View file

@ -1 +1,3 @@
pub mod player; pub use spotify_player::*;
pub mod spotify_player;

View file

@ -12,6 +12,8 @@ use serenity::model::gateway::Ready;
use serenity::all::{EventHandler, Message}; use serenity::all::{EventHandler, Message};
use serenity::Client; use serenity::Client;
use reqwest::Client as HttpClient;
use dotenv::dotenv; use dotenv::dotenv;
use songbird::SerenityInit; use songbird::SerenityInit;
@ -27,11 +29,11 @@ mod other;
use other::notice; use other::notice;
mod types; mod types;
use types::Error; use types::{Data, Error};
struct Handler; struct Handler;
async fn on_error(error: poise::FrameworkError<'_, (), Error>) { async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
match error { match error {
poise::FrameworkError::Setup { error, .. } => panic!("Failed to start bot: {:?}", error), poise::FrameworkError::Setup { error, .. } => panic!("Failed to start bot: {:?}", error),
poise::FrameworkError::Command { error, ctx, .. } => { poise::FrameworkError::Command { error, ctx, .. } => {
@ -52,7 +54,7 @@ impl EventHandler for Handler {
} }
async fn ready(&self, ctx: Context, ready: Ready) { async fn ready(&self, ctx: Context, ready: Ready) {
println!("{} v3.2.0 is connected!", ready.user.name); println!("{} v3.3.0 is connected!", ready.user.name);
#[cfg(feature="RELEASE")] { #[cfg(feature="RELEASE")] {
use util::debug::hello; use util::debug::hello;
@ -80,13 +82,14 @@ async fn main() -> anyhow::Result<()> {
dotenv().ok(); dotenv().ok();
// create poise framework for registering commands // create poise framework for registering commands
let options: poise::FrameworkOptions<(), Box<dyn error::Error + Send + Sync>> = poise::FrameworkOptions { let options: poise::FrameworkOptions<Data, Box<dyn error::Error + Send + Sync>> = poise::FrameworkOptions {
commands: vec![ commands: vec![
commands::say(), commands::say(),
commands::hug(), commands::hug(),
commands::player::play_local(), // commands::player::play_local(),
commands::player::disconnect(), commands::player_common::disconnect(),
commands::radio::radio() commands::radio::radio(),
commands::general::play()
], ],
prefix_options: poise::PrefixFrameworkOptions { prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("/".into()), prefix: Some("/".into()),
@ -125,7 +128,9 @@ async fn main() -> anyhow::Result<()> {
register_globally(ctx, &framework.options().commands).await?; register_globally(ctx, &framework.options().commands).await?;
} }
Ok(()) Ok(Data {
http_client: HttpClient::new()
})
}) })
}) })
.options(options) .options(options)

View file

@ -1,5 +1,28 @@
// pub struct Data {} // pub struct Data {}
use std::{ffi::OsString, path::PathBuf};
use serenity::prelude::TypeMapKey;
use reqwest::Client as HttpClient;
// TODO: this should be probably expanded also to be used by multiple servers and commands
// radio and spotify commands always create new HttpClient
pub struct HttpKey;
impl TypeMapKey for HttpKey {
type Value = HttpClient;
}
pub struct Data {
pub http_client: HttpClient,
}
pub type Error = Box<dyn std::error::Error + Send + Sync>; pub type Error = Box<dyn std::error::Error + Send + Sync>;
// replace () with Data if you ever need to store some additional data // replace () with Data if you ever need to store some additional data
pub type Context<'a> = poise::Context<'a, (), Error>; pub type Context<'a> = poise::Context<'a, Data, Error>;
pub struct Track {
pub path: PathBuf,
pub name: OsString,
}