Implemented radio player and partially some other players #6

Merged
HLadislav merged 21 commits from player into main 2025-01-26 21:57:27 +00:00
4 changed files with 96 additions and 3 deletions
Showing only changes of commit 1a2221d379 - Show all commits

View file

@ -2,4 +2,5 @@ pub mod util;
pub mod player_common;
pub mod radio;
// pub mod spotify;
pub mod general;
pub mod general;
pub mod yt;

View file

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

View file

@ -0,0 +1,88 @@
use std::vec;
use std::env;
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 YouTube URL")
)]
pub async fn play_yt(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.clone();
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 cookies_path = match env::var("COOKIES") {
Ok(path) => path,
Err(e) => {
let _ = ctx.reply_ephemeral("There was an error (did not find cookies)").await;
dbg!(e);
return Ok(());
}
};
let po_token = match env::var("PO_TOKEN") {
Ok(token) => token,
Err(e) => {
let _ = ctx.reply_ephemeral("There was an error (did not find token)").await;
dbg!(e);
return Ok(());
}
};
let arguments: Vec<String> = vec![
"--extractor-args".into(),
format!("youtube:player-client=web,default;po_token=web+{po_token}"),
"--cookies".into(),
cookies_path
];
let src = YoutubeDl::new(http_client, url).user_args(arguments);
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

@ -54,7 +54,7 @@ impl EventHandler for Handler {
}
async fn ready(&self, ctx: Context, ready: Ready) {
println!("{} v3.3.0 is connected!", ready.user.name);
println!("{} v3.3.1 is connected!", ready.user.name);
#[cfg(feature="RELEASE")] {
use util::debug::hello;
@ -89,7 +89,8 @@ async fn main() -> anyhow::Result<()> {
// commands::player::play_local(),
commands::player_common::disconnect(),
commands::radio::radio(),
commands::general::play()
commands::general::play(),
commands::yt::play_yt()
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("/".into()),