add playlists and a queue command
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -404,6 +404,7 @@ dependencies = [
|
|||||||
"futures",
|
"futures",
|
||||||
"regex",
|
"regex",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
"serde_json",
|
||||||
"songbird",
|
"songbird",
|
||||||
"symphonia",
|
"symphonia",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
|||||||
@@ -21,3 +21,4 @@ twilight-standby = "0.15"
|
|||||||
twilight-cache-inmemory = "0.15"
|
twilight-cache-inmemory = "0.15"
|
||||||
twilight-util = { version = "0.15", features=["builder"] }
|
twilight-util = { version = "0.15", features=["builder"] }
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
|
serde_json = "1.0"
|
||||||
|
|||||||
156
src/main.rs
156
src/main.rs
@@ -1,13 +1,23 @@
|
|||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
|
use serde_json::Value;
|
||||||
use songbird::{
|
use songbird::{
|
||||||
input::{Compose, YoutubeDl},
|
input::{Compose, YoutubeDl},
|
||||||
shards::TwilightMap,
|
shards::TwilightMap,
|
||||||
|
typemap::TypeMapKey,
|
||||||
Songbird,
|
Songbird,
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
env, error::Error, future::Future, num::NonZeroU64, ops::Sub, sync::Arc, time::Duration,
|
env,
|
||||||
|
error::Error,
|
||||||
|
future::Future,
|
||||||
|
io::{BufRead, BufReader},
|
||||||
|
num::NonZeroU64,
|
||||||
|
ops::Sub,
|
||||||
|
sync::Arc,
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
use tokio::process::Command;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
use twilight_cache_inmemory::InMemoryCache;
|
use twilight_cache_inmemory::InMemoryCache;
|
||||||
use twilight_gateway::{
|
use twilight_gateway::{
|
||||||
@@ -30,11 +40,21 @@ struct StateRef {
|
|||||||
standby: Standby,
|
standby: Standby,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Metadata {
|
||||||
|
title: Option<String>,
|
||||||
|
artist: Option<String>,
|
||||||
|
}
|
||||||
|
struct MetadataMap;
|
||||||
|
impl TypeMapKey for MetadataMap {
|
||||||
|
type Value = Metadata;
|
||||||
|
}
|
||||||
|
|
||||||
enum ChatCommand {
|
enum ChatCommand {
|
||||||
Play(Message, String),
|
Play(Message, String),
|
||||||
Stop(Message),
|
Stop(Message),
|
||||||
Leave(Message),
|
Leave(Message),
|
||||||
Join(Message),
|
Join(Message),
|
||||||
|
Queue(Message),
|
||||||
NotImplemented,
|
NotImplemented,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,6 +72,7 @@ fn parse_command(event: Event) -> Option<ChatCommand> {
|
|||||||
["!stop"] | ["!stop", _] => Some(ChatCommand::Stop(msg_create.0)),
|
["!stop"] | ["!stop", _] => Some(ChatCommand::Stop(msg_create.0)),
|
||||||
["!leave"] | ["!leave", _] => Some(ChatCommand::Leave(msg_create.0)),
|
["!leave"] | ["!leave", _] => Some(ChatCommand::Leave(msg_create.0)),
|
||||||
["!join"] | ["!join", _] => Some(ChatCommand::Join(msg_create.0)),
|
["!join"] | ["!join", _] => Some(ChatCommand::Join(msg_create.0)),
|
||||||
|
["!queue"] | ["!queue", _] => Some(ChatCommand::Queue(msg_create.0)),
|
||||||
_ => Some(ChatCommand::NotImplemented),
|
_ => Some(ChatCommand::NotImplemented),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -153,6 +174,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
|||||||
Some(ChatCommand::Stop(msg)) => spawn(stop(msg, Arc::clone(&state))),
|
Some(ChatCommand::Stop(msg)) => spawn(stop(msg, Arc::clone(&state))),
|
||||||
Some(ChatCommand::Leave(msg)) => spawn(leave(msg, Arc::clone(&state))),
|
Some(ChatCommand::Leave(msg)) => spawn(leave(msg, Arc::clone(&state))),
|
||||||
Some(ChatCommand::Join(msg)) => spawn(join(msg, Arc::clone(&state))),
|
Some(ChatCommand::Join(msg)) => spawn(join(msg, Arc::clone(&state))),
|
||||||
|
Some(ChatCommand::Queue(msg)) => spawn(queue(msg, Arc::clone(&state))),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,6 +197,12 @@ async fn join(msg: Message, state: State) -> Result<(), Box<dyn Error + Send + S
|
|||||||
.join(guild_id, channel_id)
|
.join(guild_id, channel_id)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("Could not join voice channel: {:?}", e))?;
|
.map_err(|e| format!("Could not join voice channel: {:?}", e))?;
|
||||||
|
|
||||||
|
// signal that we are not listening
|
||||||
|
if let Some(call_lock) = state.songbird.get(guild_id) {
|
||||||
|
let mut call = call_lock.lock().await;
|
||||||
|
call.deafen(true).await?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,6 +217,65 @@ async fn leave(msg: Message, state: State) -> Result<(), Box<dyn Error + Send +
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_playlist_urls(
|
||||||
|
url: String,
|
||||||
|
) -> Result<Vec<String>, Box<dyn Error + Send + Sync + 'static>> {
|
||||||
|
let output = Command::new("yt-dlp")
|
||||||
|
.args(vec![&url, "--flat-playlist", "-j"])
|
||||||
|
.output()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let reader = BufReader::new(output.stdout.as_slice());
|
||||||
|
let urls = reader
|
||||||
|
.lines()
|
||||||
|
.flatten()
|
||||||
|
.map(|line| {
|
||||||
|
let entry: Value = serde_json::from_str(&line).unwrap();
|
||||||
|
entry
|
||||||
|
.get("webpage_url")
|
||||||
|
.unwrap()
|
||||||
|
.as_str()
|
||||||
|
.unwrap()
|
||||||
|
.to_string()
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(urls)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn queue(msg: Message, state: State) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||||
|
tracing::debug!(
|
||||||
|
"queue command in channel {} by {}",
|
||||||
|
msg.channel_id,
|
||||||
|
msg.author.name
|
||||||
|
);
|
||||||
|
let guild_id = msg.guild_id.unwrap();
|
||||||
|
|
||||||
|
if let Some(call_lock) = state.songbird.get(guild_id) {
|
||||||
|
let call = call_lock.lock().await;
|
||||||
|
let queue = call.queue().current_queue();
|
||||||
|
let mut message = String::new();
|
||||||
|
message.push_str("Currently playing:\n");
|
||||||
|
for track in queue {
|
||||||
|
let map = track.typemap().read().await;
|
||||||
|
let metadata = map.get::<MetadataMap>().unwrap();
|
||||||
|
message.push_str(
|
||||||
|
format!(
|
||||||
|
"* {}\n",
|
||||||
|
metadata.title.clone().unwrap_or("Unknown".to_string()),
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
state
|
||||||
|
.http
|
||||||
|
.create_message(msg.channel_id)
|
||||||
|
.content(&message)?
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
async fn play(
|
async fn play(
|
||||||
msg: Message,
|
msg: Message,
|
||||||
state: State,
|
state: State,
|
||||||
@@ -204,39 +291,42 @@ async fn play(
|
|||||||
|
|
||||||
let guild_id = msg.guild_id.unwrap();
|
let guild_id = msg.guild_id.unwrap();
|
||||||
|
|
||||||
let mut src = YoutubeDl::new(reqwest::Client::new(), query);
|
let urls = if query.contains("list=") {
|
||||||
if let Ok(metadata) = src.aux_metadata().await {
|
get_playlist_urls(query).await?
|
||||||
debug!("metadata: {:?}", metadata);
|
|
||||||
|
|
||||||
state
|
|
||||||
.http
|
|
||||||
.create_message(msg.channel_id)
|
|
||||||
.content(&format!(
|
|
||||||
"Playing **{:?}** by **{:?}**",
|
|
||||||
metadata.title.as_ref().unwrap_or(&"<UNKNOWN>".to_string()),
|
|
||||||
metadata.artist.as_ref().unwrap_or(&"<UNKNOWN>".to_string()),
|
|
||||||
))?
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if let Some(call_lock) = state.songbird.get(guild_id) {
|
|
||||||
let mut call = call_lock.lock().await;
|
|
||||||
let _handle = call.enqueue_with_preload(
|
|
||||||
src.into(),
|
|
||||||
metadata.duration.map(|duration| -> Duration {
|
|
||||||
if duration.as_secs() > 5 {
|
|
||||||
duration.sub(Duration::from_secs(5))
|
|
||||||
} else {
|
|
||||||
duration
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
state
|
vec![query]
|
||||||
.http
|
};
|
||||||
.create_message(msg.channel_id)
|
|
||||||
.content("Didn't find any results")?
|
for url in urls {
|
||||||
.await?;
|
let mut src = YoutubeDl::new(reqwest::Client::new(), url.to_string());
|
||||||
|
if let Ok(metadata) = src.aux_metadata().await {
|
||||||
|
debug!("metadata: {:?}", metadata);
|
||||||
|
|
||||||
|
if let Some(call_lock) = state.songbird.get(guild_id) {
|
||||||
|
let mut call = call_lock.lock().await;
|
||||||
|
let handle = call.enqueue_with_preload(
|
||||||
|
src.into(),
|
||||||
|
metadata.duration.map(|duration| -> Duration {
|
||||||
|
if duration.as_secs() > 5 {
|
||||||
|
duration.sub(Duration::from_secs(5))
|
||||||
|
} else {
|
||||||
|
duration
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
let mut x = handle.typemap().write().await;
|
||||||
|
x.insert::<MetadataMap>(Metadata {
|
||||||
|
title: metadata.title,
|
||||||
|
artist: metadata.artist,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
state
|
||||||
|
.http
|
||||||
|
.create_message(msg.channel_id)
|
||||||
|
.content("Cannot find any results")?
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user