From e5dd0206ca7e6fc3f188ec4f73f992f240e52874 Mon Sep 17 00:00:00 2001 From: yourfriendoss Date: Wed, 10 Sep 2025 07:19:57 +0300 Subject: [PATCH] feat: do todos --- README.md | 32 +++++++++++++++++++-- src/commands/midi/play.rs | 8 ++++-- src/commands/midi/playlist.rs | 54 +++++++++-------------------------- src/main.rs | 18 +++++++----- 4 files changed, 59 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index df94184..ec79fe0 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,7 @@ Clone this repository with `git clone --recursive https://git.sad.ovh/sophie/cop - Lots of generic MPP bot features such as following, moving between rooms, et cetera ## Todo -- Playlist has hardcoded Playlist -- Get rid of all mentions of files.sad.ovh as it's my copyparty server, and make it customizable to other servers. +- No todo :3 ## Running 1. Requirements: Rust Nightly, should be automatically used when you do cargo run. @@ -24,10 +23,39 @@ Clone this repository with `git clone --recursive https://git.sad.ovh/sophie/cop database { url: "postgres://user:pass@ip:port/database" } + commands { prefix: "r" name: "Copper" + + copyparty: "https://files.sad.ovh/public/midis" + + playlists { + chill_music: [ + "https://files.sad.ovh/public/midis/A/autumn_leaves2-G85.mid", + "https://files.sad.ovh/public/midis/B/blue_bossa-kenny-dorham_dz.mid", + "https://files.sad.ovh/public/midis/J/Jobim_Desafinado.mid", + "https://files.sad.ovh/public/midis/J/Jobim_Wave.mid", + "https://files.sad.ovh/public/midis/J/Jobim_corcovado.mid", + "https://files.sad.ovh/public/midis/I/ipanema.mid", + "https://files.sad.ovh/public/midis/J/Jobim_Meditacao2.mid", + "https://files.sad.ovh/public/midis/S/so_what.mid", + "https://files.sad.ovh/public/midis/T/take_five2-Gb176-davebrubeck.mid", + "https://files.sad.ovh/public/midis/M/My_Funny_Valentine.mid", + "https://files.sad.ovh/public/midis/A/all_the_things_you_are-2_dm.mid", + "https://files.sad.ovh/public/midis/J/Johnny_Mathis_Misty.mid", + "https://files.sad.ovh/public/midis/H/HOWARD.Fly me to the moon.mid", + "https://files.sad.ovh/public/midis/R/Ray Charles - Georgia On My Mind.mid", + "https://files.sad.ovh/public/midis/B/blue_in_green.mid", + "https://files.sad.ovh/public/midis/S/Stella-By-Starlight-1.mid", + "https://files.sad.ovh/public/midis/F/FITZGERALD.Summertime K.mid", + "https://files.sad.ovh/public/midis/M/Moon River.mid", + "https://files.sad.ovh/public/midis/A/autumn_in_new_york2-Bb84.mid", + "https://files.sad.ovh/public/midis/J/Jobim_One_Note_Samba.mid", + ] + } } + client { token: "token" ws: "wss://mppclone.com" diff --git a/src/commands/midi/play.rs b/src/commands/midi/play.rs index 5b1328c..99c597b 100644 --- a/src/commands/midi/play.rs +++ b/src/commands/midi/play.rs @@ -1,3 +1,4 @@ +use crate::Configuration; use crate::client::Client; use crate::client::ClientEvent; use crate::client::Note; @@ -17,11 +18,12 @@ use tokio::sync::Mutex; pub struct PlayCommand { midi_state: Arc>, + conf: Configuration, } impl PlayCommand { - pub fn new(midi_state: Arc>) -> Self { - Self { midi_state } + pub fn new(midi_state: Arc>, conf: Configuration) -> Self { + Self { midi_state, conf } } } @@ -180,7 +182,7 @@ impl Command for PlayCommand { filename_to_play = format!("midis/{}.mid", hashed); filename_beautiful = first_capture.to_string(); - let url = format!("https://files.sad.ovh/public/midis/{}", first_capture); + let url = format!("{}/{}", self.conf.commands.copyparty, first_capture); let file_exists = tokio::fs::try_exists(&filename_to_play) .await diff --git a/src/commands/midi/playlist.rs b/src/commands/midi/playlist.rs index b465d11..6e09e98 100644 --- a/src/commands/midi/playlist.rs +++ b/src/commands/midi/playlist.rs @@ -2,6 +2,7 @@ use crate::client::Client; use crate::client::ClientEvent; use crate::client::Player; +use crate::Configuration; use crate::commands::Command; use crate::commands::MidiState; use crate::commands::argument::{ArgumentSpec, ArgumentType, ParsedArgument, ParsedArguments}; @@ -15,11 +16,12 @@ use tokio::sync::Mutex; pub struct PlaylistCommand { midi_state: Arc>, + conf: Configuration, } impl PlaylistCommand { - pub fn new(midi_state: Arc>) -> Self { - Self { midi_state } + pub fn new(midi_state: Arc>, conf: Configuration) -> Self { + Self { midi_state, conf } } } @@ -59,50 +61,20 @@ impl Command for PlaylistCommand { let mut filename_to_play: String = "".to_string(); let mut filename_beautiful: String = "".to_string(); - if joined_args == "playlist_1" { - const TRACKS: &[&str] = &[ - "A/autumn_leaves2-G85.mid", - "B/blue_bossa-kenny-dorham_dz.mid", - "J/Jobim_Desafinado.mid", - "J/Jobim_Wave.mid", - "J/Jobim_corcovado.mid", - "I/ipanema.mid", - "J/Jobim_Meditacao2.mid", - "S/so_what.mid", - "T/take_five2-Gb176-davebrubeck.mid", - "M/My_Funny_Valentine.mid", - "A/all_the_things_you_are-2_dm.mid", - "J/Johnny_Mathis_Misty.mid", - "H/HOWARD.Fly me to the moon.mid", - "R/Ray Charles - Georgia On My Mind.mid", - "B/blue_in_green.mid", - "S/Stella-By-Starlight-1.mid", - "F/FITZGERALD.Summertime K.mid", - "M/Moon River.mid", - "A/autumn_in_new_york2-Bb84.mid", - "J/Jobim_One_Note_Samba.mid", - ]; - - for track in TRACKS { + if self.conf.commands.playlists.contains_key(joined_args) { + let tracks_ref = self.conf.commands.playlists.get(joined_args).unwrap(); + for track in tracks_ref { let hashed = simple_hash(track); let filename = format!("midis/{}.mid", hashed); - let url = format!("https://files.sad.ovh/public/midis/{}", track); let file_exists = tokio::fs::try_exists(&filename).await.unwrap_or(false); if !file_exists { - match reqwest::get(&url).await { + match reqwest::get(&track.clone()).await { Ok(resp) => { if resp.status().is_success() { match resp.bytes().await { Ok(bytes) => match tokio::fs::write(&filename, &bytes).await { - Ok(_) => { - client - .message(format!( - "Downloaded midi from files.sad.ovh: {}", - track - )) - .await; - } + Ok(_) => {} Err(e) => { client .message(format!("Failed to write file: {}", e)) @@ -137,7 +109,7 @@ impl Command for PlaylistCommand { } } - let mut tracks: Vec<&str> = TRACKS.to_vec(); + let mut tracks = tracks_ref.clone(); tracks.shuffle(&mut rand::rng()); if let Some(first) = tracks.first() { @@ -175,7 +147,7 @@ impl Command for PlaylistCommand { let first_capture = caps.trim(); if first_capture.ends_with('/') { - let url = format!("https://files.sad.ovh/public/midis/{}?ls", first_capture); + let url = format!("{}/{}?ls", self.conf.commands.copyparty, first_capture); match reqwest::get(&url).await { Ok(resp) => { if resp.status().is_success() { @@ -209,8 +181,8 @@ impl Command for PlaylistCommand { let hashed = simple_hash(&file_path); let filename = format!("midis/{}.mid", hashed); let url_file = format!( - "https://files.sad.ovh/public/midis/{}", - file_path + "{}/{}", + self.conf.commands.copyparty, file_path ); let file_exists = tokio::fs::try_exists(&filename) diff --git a/src/main.rs b/src/main.rs index 384aba0..a23cfe3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,8 @@ use crate::client::Client; use crate::client::ClientEvent; use crate::commands::Arguments; use crate::commands::CommandRegistry; +use std::collections::HashMap; + use cap::Cap; use serde::Deserialize; use sqlx::postgres::PgPoolOptions; @@ -46,25 +48,27 @@ macro_rules! register_all { )+ }; } -#[derive(Deserialize)] -struct Configuration { +#[derive(Deserialize, Clone)] +pub struct Configuration { database: DatabaseConfig, commands: CommandsConfig, client: ClientConfig, } -#[derive(Deserialize)] +#[derive(Deserialize, Clone)] struct DatabaseConfig { url: String, } -#[derive(Deserialize)] +#[derive(Deserialize, Clone)] struct CommandsConfig { prefix: String, name: String, + copyparty: String, + playlists: HashMap>, } -#[derive(Deserialize)] +#[derive(Deserialize, Clone)] struct ClientConfig { token: String, ws: String, @@ -98,9 +102,9 @@ async fn main() -> Result<(), Box> { registry, client, [ - PlayCommand::new(midi_state.clone()), + PlayCommand::new(midi_state.clone(), conf.clone()), StopCommand::new(midi_state.clone()), - PlaylistCommand::new(midi_state.clone()), + PlaylistCommand::new(midi_state.clone(), conf.clone()), QueueCommand::new(midi_state.clone()), SkipCommand::new(midi_state), LaunchCommand,