Compare commits
7 Commits
cabae9ea19
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
6b3629436a
|
|||
| feccbebd4d | |||
| d6a54dfc06 | |||
|
fa17ed75f6
|
|||
| b19f6e552c | |||
|
0bee437e53
|
|||
|
3781785a50
|
@@ -26,6 +26,6 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
platforms: |
|
platforms: |
|
||||||
linux/amd64
|
linux/amd64
|
||||||
linux/arm64
|
# linux/arm64
|
||||||
push: true
|
push: true
|
||||||
tags: jheuel/ohrwurm:latest
|
tags: jheuel/ohrwurm:latest
|
||||||
|
|||||||
@@ -1,2 +1,20 @@
|
|||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) const BLURPLE: u32 = 0x58_65_F2;
|
pub(crate) const BLURPLE: u32 = 0x58_65_F2;
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) const YELLOW: u32 = 0xFE_E7_5C;
|
pub(crate) const YELLOW: u32 = 0xFE_E7_5C;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub(crate) const WHITE: u32 = 0xFF_FF_FF;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub(crate) const GREYPLE: u32 = 0x99_AA_B5;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub(crate) const BLACK: u32 = 0x23_27_2A;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub(crate) const DARK_BUT_NOT_BLACK: u32 = 0x2C_2F_33;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub(crate) const NOT_QUITE_BLACK: u32 = 0x23_27_2A;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub(crate) const GREEN: u32 = 0x57_F2_87;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub(crate) const FUCHSIA: u32 = 0xEB_45_9E;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub(crate) const RED: u32 = 0xED_42_45;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::state::{State, StateRef};
|
use crate::metadata::MetadataMap;
|
||||||
|
use crate::state::{Settings, State, StateRef};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use songbird::input::Compose;
|
|
||||||
use songbird::{Event, EventContext, EventHandler, TrackEvent};
|
use songbird::{Event, EventContext, EventHandler, TrackEvent};
|
||||||
use std::ops::Sub;
|
use std::ops::Sub;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@@ -29,12 +29,21 @@ pub(crate) async fn loop_queue(
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
let looping = if let Some(mut settings) = state.guild_settings.get_mut(&guild_id) {
|
state
|
||||||
|
.guild_settings
|
||||||
|
.entry(guild_id)
|
||||||
|
.or_insert_with(|| Settings { loop_queue: false });
|
||||||
|
|
||||||
|
state.guild_settings.entry(guild_id).and_modify(|settings| {
|
||||||
settings.loop_queue = !settings.loop_queue;
|
settings.loop_queue = !settings.loop_queue;
|
||||||
settings.loop_queue
|
println!("loop_queue: {}", settings.loop_queue);
|
||||||
} else {
|
});
|
||||||
false
|
|
||||||
};
|
let looping = state
|
||||||
|
.guild_settings
|
||||||
|
.get(&guild_id)
|
||||||
|
.expect("Cannot get loop state")
|
||||||
|
.loop_queue;
|
||||||
|
|
||||||
if let Some(call_lock) = state.songbird.get(guild_id) {
|
if let Some(call_lock) = state.songbird.get(guild_id) {
|
||||||
let mut call = call_lock.lock().await;
|
let mut call = call_lock.lock().await;
|
||||||
@@ -47,10 +56,11 @@ pub(crate) async fn loop_queue(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut message = "I'm not looping anymore!".to_string();
|
let message = if looping {
|
||||||
if looping {
|
"I'm now looping the current queue!".to_string()
|
||||||
message = "I'm now looping the current queue!".to_string();
|
} else {
|
||||||
}
|
"I'm not looping anymore!".to_string()
|
||||||
|
};
|
||||||
|
|
||||||
let interaction_response_data = InteractionResponseDataBuilder::new()
|
let interaction_response_data = InteractionResponseDataBuilder::new()
|
||||||
.content(message)
|
.content(message)
|
||||||
@@ -91,29 +101,28 @@ impl EventHandler for TrackEndNotifier {
|
|||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
let (_, track_handle) = track_list.first()?;
|
let (_, track_handle) = track_list.first()?;
|
||||||
if let Some(yt) = self
|
if let Some(call_lock) = self.state.songbird.get(self.guild_id) {
|
||||||
.state
|
let mut call = call_lock.lock().await;
|
||||||
.tracks
|
|
||||||
.get(&self.guild_id)
|
// get metadata from finished track
|
||||||
.unwrap()
|
let old_typemap_lock = track_handle.typemap().read().await;
|
||||||
.get(&track_handle.uuid())
|
let old_metadata = old_typemap_lock.get::<MetadataMap>().unwrap();
|
||||||
{
|
|
||||||
let mut src = yt.clone();
|
// enqueue track
|
||||||
if let Ok(metadata) = src.aux_metadata().await {
|
let handle = call.enqueue_with_preload(
|
||||||
if let Some(call_lock) = self.state.songbird.get(self.guild_id) {
|
old_metadata.src.clone().into(),
|
||||||
let mut call = call_lock.lock().await;
|
old_metadata.duration.map(|duration| -> Duration {
|
||||||
call.enqueue_with_preload(
|
if duration.as_secs() > 5 {
|
||||||
src.into(),
|
duration.sub(Duration::from_secs(5))
|
||||||
metadata.duration.map(|duration| -> Duration {
|
} else {
|
||||||
if duration.as_secs() > 5 {
|
duration
|
||||||
duration.sub(Duration::from_secs(5))
|
}
|
||||||
} else {
|
}),
|
||||||
duration
|
);
|
||||||
}
|
|
||||||
}),
|
// insert metadata into new track
|
||||||
);
|
let mut new_typemap = handle.typemap().write().await;
|
||||||
}
|
new_typemap.insert::<MetadataMap>(old_metadata.clone());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,12 +35,14 @@ pub(crate) fn get_chat_commands() -> Vec<twilight_model::application::command::C
|
|||||||
vec![
|
vec![
|
||||||
CommandBuilder::new("join", "Join the channel", CommandType::ChatInput).build(),
|
CommandBuilder::new("join", "Join the channel", CommandType::ChatInput).build(),
|
||||||
CommandBuilder::new("leave", "Leave the channel", CommandType::ChatInput).build(),
|
CommandBuilder::new("leave", "Leave the channel", CommandType::ChatInput).build(),
|
||||||
|
CommandBuilder::new("loop", "Loop queue", CommandType::ChatInput).build(),
|
||||||
|
CommandBuilder::new("skip", "Skip track", CommandType::ChatInput).build(),
|
||||||
|
CommandBuilder::new("queue", "Print track queue", CommandType::ChatInput).build(),
|
||||||
|
CommandBuilder::new("stop", "Stop playing", CommandType::ChatInput).build(),
|
||||||
CommandBuilder::new("pause", "Pause playing", CommandType::ChatInput).build(),
|
CommandBuilder::new("pause", "Pause playing", CommandType::ChatInput).build(),
|
||||||
|
CommandBuilder::new("resume", "Resume playing", CommandType::ChatInput).build(),
|
||||||
CommandBuilder::new("play", "Add a song to the queue", CommandType::ChatInput)
|
CommandBuilder::new("play", "Add a song to the queue", CommandType::ChatInput)
|
||||||
.option(StringBuilder::new("query", "URL of a song").required(true))
|
.option(StringBuilder::new("query", "URL of a song").required(true))
|
||||||
.build(),
|
.build(),
|
||||||
CommandBuilder::new("queue", "Print track queue", CommandType::ChatInput).build(),
|
|
||||||
CommandBuilder::new("resume", "Resume playing", CommandType::ChatInput).build(),
|
|
||||||
CommandBuilder::new("stop", "Stop playing", CommandType::ChatInput).build(),
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,6 +47,18 @@ async fn get_tracks(
|
|||||||
.map_while(Result::ok)
|
.map_while(Result::ok)
|
||||||
.flat_map(|line| serde_json::from_str(&line))
|
.flat_map(|line| serde_json::from_str(&line))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
if tracks.is_empty() {
|
||||||
|
if let Ok(stderr) = String::from_utf8(output.stderr) {
|
||||||
|
if stderr.contains("This video is only available to Music Premium members") {
|
||||||
|
return Err("This video is only available to Music Premium members".into());
|
||||||
|
}
|
||||||
|
if stderr.contains("YouTube said: The playlist does not exist.") {
|
||||||
|
return Err("YouTube said: The playlist does not exist.".into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Err("No tracks found".into());
|
||||||
|
}
|
||||||
tracing::debug!("tracks: {:?}", tracks);
|
tracing::debug!("tracks: {:?}", tracks);
|
||||||
Ok(tracks)
|
Ok(tracks)
|
||||||
}
|
}
|
||||||
@@ -99,7 +111,23 @@ pub(crate) async fn play(
|
|||||||
|
|
||||||
debug!("query: {:?}", query);
|
debug!("query: {:?}", query);
|
||||||
|
|
||||||
let tracks = get_tracks(query).await?;
|
let tracks = match get_tracks(query).await {
|
||||||
|
Err(e) => {
|
||||||
|
let content = format!("{}", e);
|
||||||
|
let embeds = vec![EmbedBuilder::new()
|
||||||
|
.description(content)
|
||||||
|
.color(colors::RED)
|
||||||
|
.build()];
|
||||||
|
state
|
||||||
|
.http
|
||||||
|
.interaction(interaction.application_id)
|
||||||
|
.update_response(&interaction.token)
|
||||||
|
.embeds(Some(&embeds))?
|
||||||
|
.await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
Ok(tracks) => tracks,
|
||||||
|
};
|
||||||
|
|
||||||
if tracks.len() > 1 {
|
if tracks.len() > 1 {
|
||||||
let first_track = tracks.first().unwrap();
|
let first_track = tracks.first().unwrap();
|
||||||
@@ -138,13 +166,8 @@ pub(crate) async fn play(
|
|||||||
tracing::debug!("track: {:?}", track);
|
tracing::debug!("track: {:?}", track);
|
||||||
let url = track.url.clone().or(track.original_url.clone()).ok_or("")?;
|
let url = track.url.clone().or(track.original_url.clone()).ok_or("")?;
|
||||||
let mut src = YoutubeDl::new(reqwest::Client::new(), url.clone());
|
let mut src = YoutubeDl::new(reqwest::Client::new(), url.clone());
|
||||||
let s = src.clone();
|
let src_copy = src.clone();
|
||||||
let track: Track = src.clone().into();
|
let track: Track = src_copy.into();
|
||||||
state
|
|
||||||
.tracks
|
|
||||||
.entry(guild_id)
|
|
||||||
.or_default()
|
|
||||||
.insert(track.uuid, s);
|
|
||||||
|
|
||||||
if let Ok(metadata) = src.aux_metadata().await {
|
if let Ok(metadata) = src.aux_metadata().await {
|
||||||
debug!("metadata: {:?}", metadata);
|
debug!("metadata: {:?}", metadata);
|
||||||
@@ -167,6 +190,7 @@ pub(crate) async fn play(
|
|||||||
title: metadata.title,
|
title: metadata.title,
|
||||||
duration: metadata.duration,
|
duration: metadata.duration,
|
||||||
url,
|
url,
|
||||||
|
src,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -238,4 +262,34 @@ mod tests {
|
|||||||
assert!(!tracks.is_empty());
|
assert!(!tracks.is_empty());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_premium_tracks() {
|
||||||
|
let urls = ["https://www.youtube.com/watch?v=QgMZRmxQ0Dc"];
|
||||||
|
for url in urls.iter() {
|
||||||
|
println!("url: {:?}", url);
|
||||||
|
let tracks = get_tracks(url.to_string()).await;
|
||||||
|
assert!(tracks.is_err());
|
||||||
|
assert!(tracks
|
||||||
|
.err()
|
||||||
|
.unwrap()
|
||||||
|
.to_string()
|
||||||
|
.contains("This video is only available to Music Premium members"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_playlist_does_not_exist_tracks() {
|
||||||
|
let urls = ["https://www.youtube.com/playlist?list=PLox0oG0uy8Lc1IaIfGyrvtuRItuEyJiyG"];
|
||||||
|
for url in urls.iter() {
|
||||||
|
println!("url: {:?}", url);
|
||||||
|
let tracks = get_tracks(url.to_string()).await;
|
||||||
|
assert!(tracks.is_err());
|
||||||
|
assert!(tracks
|
||||||
|
.err()
|
||||||
|
.unwrap()
|
||||||
|
.to_string()
|
||||||
|
.contains("YouTube said: The playlist does not exist."));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ pub(crate) async fn skip(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let interaction_response_data = InteractionResponseDataBuilder::new()
|
let interaction_response_data = InteractionResponseDataBuilder::new()
|
||||||
.content("Skipped the next track")
|
.content("Skipped a track")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let response = InteractionResponse {
|
let response = InteractionResponse {
|
||||||
|
|||||||
@@ -78,7 +78,6 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
|||||||
songbird,
|
songbird,
|
||||||
standby: Standby::new(),
|
standby: Standby::new(),
|
||||||
guild_settings: Default::default(),
|
guild_settings: Default::default(),
|
||||||
tracks: Default::default(),
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
use songbird::typemap::TypeMapKey;
|
use songbird::{input::YoutubeDl, typemap::TypeMapKey};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub(crate) struct Metadata {
|
pub(crate) struct Metadata {
|
||||||
pub(crate) title: Option<String>,
|
pub(crate) title: Option<String>,
|
||||||
pub(crate) duration: Option<Duration>,
|
pub(crate) duration: Option<Duration>,
|
||||||
pub(crate) url: String,
|
pub(crate) url: String,
|
||||||
|
pub(crate) src: YoutubeDl,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct MetadataMap;
|
pub(crate) struct MetadataMap;
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use songbird::{input::YoutubeDl, Songbird};
|
use songbird::Songbird;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use twilight_cache_inmemory::InMemoryCache;
|
use twilight_cache_inmemory::InMemoryCache;
|
||||||
use twilight_http::Client as HttpClient;
|
use twilight_http::Client as HttpClient;
|
||||||
use twilight_model::id::{marker::GuildMarker, Id};
|
use twilight_model::id::{marker::GuildMarker, Id};
|
||||||
use twilight_standby::Standby;
|
use twilight_standby::Standby;
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub(crate) type State = Arc<StateRef>;
|
pub(crate) type State = Arc<StateRef>;
|
||||||
|
|
||||||
@@ -21,5 +20,4 @@ pub(crate) struct StateRef {
|
|||||||
pub(crate) songbird: Songbird,
|
pub(crate) songbird: Songbird,
|
||||||
pub(crate) standby: Standby,
|
pub(crate) standby: Standby,
|
||||||
pub(crate) guild_settings: DashMap<Id<GuildMarker>, Settings>,
|
pub(crate) guild_settings: DashMap<Id<GuildMarker>, Settings>,
|
||||||
pub(crate) tracks: DashMap<Id<GuildMarker>, DashMap<Uuid, YoutubeDl>>,
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user