chore: updates and formatting

This commit is contained in:
Miguel da Mota 2024-03-10 21:40:52 +01:00
parent 2f89b9dbb9
commit 72a1fe8af3
10 changed files with 175 additions and 131 deletions

127
Cargo.lock generated
View file

@ -147,9 +147,9 @@ dependencies = [
[[package]]
name = "bumpalo"
version = "3.15.3"
version = "3.15.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea184aa71bb362a1157c896979544cc23974e08fd265f29ea96b59f0b4a555b"
checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa"
[[package]]
name = "bytecount"
@ -208,9 +208,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.0.88"
version = "1.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02f341c093d19155a6e41631ce5971aac4e9a868262212153124c15fa22d1cdc"
checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5"
[[package]]
name = "cfg-if"
@ -220,9 +220,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.34"
version = "0.4.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b"
checksum = "8eaf5903dcbc0a39312feb77df2ff4c76387d591b9fc7b04a238dcf8bb62639a"
dependencies = [
"android-tzdata",
"iana-time-zone",
@ -631,12 +631,11 @@ dependencies = [
[[package]]
name = "generator"
version = "0.7.6"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b25e5b3e733153bcab35ee4671b46604b42516163cae442d1601cb716f2ac5"
checksum = "5cc16584ff22b460a382b7feec54b23d2908d858152e5739a120b949293bd74e"
dependencies = [
"cc",
"cfg-if",
"libc",
"log",
"rustversion",
@ -690,7 +689,7 @@ dependencies = [
"futures-core",
"futures-sink",
"futures-util",
"http 0.2.11",
"http 0.2.12",
"indexmap",
"slab",
"tokio",
@ -712,9 +711,9 @@ checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "http"
version = "0.2.11"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8947b1a6fad4393052c7ba1f4cd97bed3e953a95c79c92ad9b051a04611d9fbb"
checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1"
dependencies = [
"bytes",
"fnv",
@ -723,9 +722,9 @@ dependencies = [
[[package]]
name = "http"
version = "1.0.0"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b32afd38673a8016f7c9ae69e5af41a58f81b1d31689040f2f1959594ce194ea"
checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258"
dependencies = [
"bytes",
"fnv",
@ -739,7 +738,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2"
dependencies = [
"bytes",
"http 0.2.11",
"http 0.2.12",
"pin-project-lite",
]
@ -766,7 +765,7 @@ dependencies = [
"futures-core",
"futures-util",
"h2",
"http 0.2.11",
"http 0.2.12",
"http-body",
"httparse",
"httpdate",
@ -786,7 +785,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590"
dependencies = [
"futures-util",
"http 0.2.11",
"http 0.2.12",
"hyper",
"rustls 0.21.10",
"tokio",
@ -817,7 +816,7 @@ dependencies = [
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core 0.52.0",
"windows-core",
]
[[package]]
@ -872,9 +871,9 @@ checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]]
name = "js-sys"
version = "0.3.68"
version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "406cda4b368d531c842222cf9d2600a9a4acce8d29423695379c6868a143a9ee"
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
dependencies = [
"wasm-bindgen",
]
@ -1220,18 +1219,18 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "pin-project"
version = "1.1.4"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0302c4a0442c456bd56f841aee5c3bfd17967563f6fadc9ceb9f9c23cf3807e0"
checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "1.1.4"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "266c042b60c9c76b8d53061e52b2e0d1116abc57cefc8c5cd671619a56ac3690"
checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
dependencies = [
"proc-macro2",
"quote",
@ -1414,7 +1413,7 @@ checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata 0.4.5",
"regex-automata 0.4.6",
"regex-syntax 0.8.2",
]
@ -1429,9 +1428,9 @@ dependencies = [
[[package]]
name = "regex-automata"
version = "0.4.5"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd"
checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea"
dependencies = [
"aho-corasick",
"memchr",
@ -1452,9 +1451,9 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "reqwest"
version = "0.11.24"
version = "0.11.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6920094eb85afde5e4a138be3f2de8bbdf28000f0029e72c45025a56b042251"
checksum = "0eea5a9eb898d3783f17c6407670e3592fd174cb81a10e51d4c37f49450b9946"
dependencies = [
"base64 0.21.7",
"bytes",
@ -1462,7 +1461,7 @@ dependencies = [
"futures-core",
"futures-util",
"h2",
"http 0.2.11",
"http 0.2.12",
"http-body",
"hyper",
"hyper-rustls",
@ -2257,20 +2256,20 @@ checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
[[package]]
name = "system-configuration"
version = "0.5.1"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7"
checksum = "658bc6ee10a9b4fcf576e9b0819d95ec16f4d2c02d39fd83ac1c8789785c4a42"
dependencies = [
"bitflags 1.3.2",
"bitflags 2.4.2",
"core-foundation",
"system-configuration-sys",
]
[[package]]
name = "system-configuration-sys"
version = "0.5.0"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9"
checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4"
dependencies = [
"core-foundation-sys",
"libc",
@ -2630,7 +2629,7 @@ dependencies = [
"base64 0.13.1",
"byteorder",
"bytes",
"http 0.2.11",
"http 0.2.12",
"httparse",
"log",
"rand",
@ -2651,7 +2650,7 @@ dependencies = [
"byteorder",
"bytes",
"data-encoding",
"http 1.0.0",
"http 1.1.0",
"httparse",
"log",
"rand",
@ -2877,9 +2876,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.91"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f"
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
@ -2887,9 +2886,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.91"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b"
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
dependencies = [
"bumpalo",
"log",
@ -2902,9 +2901,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.41"
version = "0.4.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877b9c3f61ceea0e56331985743b13f3d25c406a7098d45180fb5f09bc19ed97"
checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0"
dependencies = [
"cfg-if",
"js-sys",
@ -2914,9 +2913,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.91"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed"
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@ -2924,9 +2923,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.91"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66"
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [
"proc-macro2",
"quote",
@ -2937,9 +2936,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.91"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838"
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
[[package]]
name = "wasm-streams"
@ -2956,9 +2955,9 @@ dependencies = [
[[package]]
name = "web-sys"
version = "0.3.68"
version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96565907687f7aceb35bc5fc03770a8a0471d82e479f25832f54a0e3f4b28446"
checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef"
dependencies = [
"js-sys",
"wasm-bindgen",
@ -3022,12 +3021,11 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.53.0"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efc5cf48f83140dcaab716eeaea345f9e93d0018fb81162753a3f76c3397b538"
checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f"
dependencies = [
"windows-core 0.53.0",
"windows-targets 0.52.4",
"windows-targets 0.48.5",
]
[[package]]
@ -3039,25 +3037,6 @@ dependencies = [
"windows-targets 0.52.4",
]
[[package]]
name = "windows-core"
version = "0.53.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dcc5b895a6377f1ab9fa55acedab1fd5ac0db66ad1e6c7f47e28a22e446a5dd"
dependencies = [
"windows-result",
"windows-targets 0.52.4",
]
[[package]]
name = "windows-result"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd19df78e5168dfb0aedc343d1d1b8d422ab2db6756d2dc3fef75035402a3f64"
dependencies = [
"windows-targets 0.52.4",
]
[[package]]
name = "windows-sys"
version = "0.48.0"

View file

@ -1,5 +1,5 @@
pub mod info;
pub mod now_playing;
pub mod play;
pub mod skip;
pub mod stop;
pub mod now_playing;

View file

@ -21,14 +21,22 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
let now_plaing = match music_queue::get_now_playing(&guild_id).await {
Some(ytdl) => ytdl,
None => {
return Embed::create_error_respose(username, "Not playing", "I'm not playing anything!");
return Embed::create_error_respose(
username,
"Not playing",
"I'm not playing anything!",
);
}
};
let now_handle = match music_queue::get_now_playing_track_handle(&guild_id).await {
Some(handle) => handle,
None => {
return Embed::create_error_respose(username, "Cannot get TrackHandle", "The TrackHandle is empty.");
return Embed::create_error_respose(
username,
"Cannot get TrackHandle",
"The TrackHandle is empty.",
);
}
};
@ -40,14 +48,27 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
let handler = match manager.get(*guild_id) {
Some(handler) => handler,
None => {
return Embed::create_error_respose(username, "Error", "Error while getting the audio handler.");
return Embed::create_error_respose(
username,
"Error",
"Error while getting the audio handler.",
);
}
};
let position = now_handle.get_info().await.unwrap().position;
Embed::create_yt_playing(now_plaing, username, "Currently playing").await
.field("Position", format!("{}min {}sec", position.as_secs() / 60, position.as_secs() % 60), true)
Embed::create_yt_playing(now_plaing, username, "Currently playing")
.await
.field(
"Position",
format!(
"{}min {}sec",
position.as_secs() / 60,
position.as_secs() % 60
),
true,
)
}
pub fn register() -> CreateCommand {

View file

@ -19,13 +19,21 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
});
if query.is_none() {
return Embed::create_error_respose(username, "400: Bad request", "There is no query provided");
return Embed::create_error_respose(
username,
"400: Bad request",
"There is no query provided",
);
}
let guild_id = match &command.guild_id {
Some(guild_id) => guild_id,
None => {
return Embed::create_error_respose(username, "guild_id not found", "Could not find guild id.");
return Embed::create_error_respose(
username,
"guild_id not found",
"Could not find guild id.",
);
}
};

View file

@ -9,7 +9,11 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
let guild_id = match &command.guild_id {
Some(guild_id) => guild_id,
None => {
return Embed::create_error_respose(username, "guild_id not found", "Could not find guild id.");
return Embed::create_error_respose(
username,
"guild_id not found",
"Could not find guild id.",
);
}
};

View file

@ -38,14 +38,14 @@ impl EventHandler for Handler {
if let Interaction::Command(command) = interaction {
let _ = &command.defer(&ctx.http()).await.expect("Cannot defer");
let content = Some(match command.data.name.as_str() {
"info" => commands::info::run(&ctx, &command).await,
"play" => commands::play::run(&ctx, &command).await,
"stop" => commands::stop::run(&ctx, &command).await,
"skip" => commands::skip::run(&ctx, &command).await,
"nowplaying" => commands::now_playing::run(&ctx, &command).await,
_ => respond_with_error(&ctx, &command).await,
});
let content = Some(match command.data.name.as_str() {
"info" => commands::info::run(&ctx, &command).await,
"play" => commands::play::run(&ctx, &command).await,
"stop" => commands::stop::run(&ctx, &command).await,
"skip" => commands::skip::run(&ctx, &command).await,
"nowplaying" => commands::now_playing::run(&ctx, &command).await,
_ => respond_with_error(&ctx, &command).await,
});
if let Some(embed) = content {
let followup = CreateInteractionResponseFollowup::new().embed(embed);
@ -63,7 +63,8 @@ impl EventHandler for Handler {
let _command = Command::create_global_command(&ctx.http, commands::stop::register()).await;
let _command = Command::create_global_command(&ctx.http, commands::play::register()).await;
let _command = Command::create_global_command(&ctx.http, commands::skip::register()).await;
let _command = Command::create_global_command(&ctx.http, commands::now_playing::register()).await;
let _command =
Command::create_global_command(&ctx.http, commands::now_playing::register()).await;
println!("Commands are registered and Rustendo is ready for Freddy.");
}
@ -94,7 +95,11 @@ impl EventHandler for Handler {
}
pub async fn respond_with_error(_ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
Embed::create_error_respose(command.user.name.as_str(), "Command not found", "Cannot find the executed command")
Embed::create_error_respose(
command.user.name.as_str(),
"Command not found",
"Cannot find the executed command",
)
}
#[tokio::main]

View file

@ -1,3 +1,3 @@
pub mod music_manager;
pub mod music_events;
pub mod music_manager;
pub mod music_queue;

View file

@ -89,7 +89,6 @@ pub async fn attempt_to_queue_song(
.expect("Cannot get head of queue");
music_queue::set_now_playing(guild_id, Some(src.clone())).await;
let handler_lock = match manager.get(*guild_id) {
Some(handler) => handler,
None => {

View file

@ -12,7 +12,7 @@ type MusicQueueItem = Arc<Mutex<MusicQueue>>;
pub struct MusicQueue {
pub queue: VecDeque<YoutubeDl>,
pub now_playing: Option<YoutubeDl>,
pub now_playing_track_handle: Option<TrackHandle>
pub now_playing_track_handle: Option<TrackHandle>,
}
lazy_static! {
@ -69,11 +69,14 @@ pub async fn get_now_playing(guild_id: &GuildId) -> Option<YoutubeDl> {
}
pub async fn set_now_playing_track_handle(guild_id: &GuildId, track_handle: Option<TrackHandle>) {
with_music_queue(guild_id, |queue| queue.now_playing_track_handle = track_handle).await
with_music_queue(guild_id, |queue| {
queue.now_playing_track_handle = track_handle
})
.await
}
pub async fn get_now_playing_track_handle(guild_id: &GuildId) -> Option<TrackHandle> {
with_music_queue(guild_id, | queue| queue.now_playing_track_handle.to_owned()).await
with_music_queue(guild_id, |queue| queue.now_playing_track_handle.to_owned()).await
}
pub async fn is_empty(guild_id: &GuildId) -> bool {

View file

@ -11,46 +11,71 @@ impl Embed {
let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S");
CreateEmbed::new()
.title(title)
.description(desc)
.color(Color::from_rgb(224, 49, 26))
.footer(CreateEmbedFooter::new(format!("> {} - {}", current_time, username)))
.title(title)
.description(desc)
.color(Color::from_rgb(224, 49, 26))
.footer(CreateEmbedFooter::new(format!(
"> {} - {}",
current_time, username
)))
}
pub fn create_error_respose(username: &str, error_title: &str, error_desc: &str) -> CreateEmbed {
pub fn create_error_respose(
username: &str,
error_title: &str,
error_desc: &str,
) -> CreateEmbed {
let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S");
CreateEmbed::new()
.author(CreateEmbedAuthor::new("Oops, something went wrong."))
.title(error_title)
.description(error_desc)
.color(Color::from_rgb(224, 49, 26))
.footer(CreateEmbedFooter::new(format!("> {} - {}", current_time, username)))
.author(CreateEmbedAuthor::new("Oops, something went wrong."))
.title(error_title)
.description(error_desc)
.color(Color::from_rgb(224, 49, 26))
.footer(CreateEmbedFooter::new(format!(
"> {} - {}",
current_time, username
)))
}
pub async fn create_yt_playing(mut src: YoutubeDl, username: &str, show_as_author: &str) -> CreateEmbed {
let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S");
pub async fn create_yt_playing(
mut src: YoutubeDl,
username: &str,
show_as_author: &str,
) -> CreateEmbed {
let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S");
// Get metadata
let metadata = src.aux_metadata().await.expect("Cannot get metadata");
let title = metadata.title.unwrap_or("Unknown title".to_string());
let artist = metadata.artist.unwrap_or("Unknown artist".to_string());
let duration = metadata.duration.unwrap_or(Duration::from_millis(0));
let thumbnail = metadata
.thumbnail
.unwrap_or("https://http.cat/images/403.jpg".to_string());
let link = metadata
.source_url
.unwrap_or("https://piped.moonleay.net/403".to_string());
// Get metadata
let metadata = src.aux_metadata().await.expect("Cannot get metadata");
let title = metadata.title.unwrap_or("Unknown title".to_string());
let artist = metadata.artist.unwrap_or("Unknown artist".to_string());
let duration = metadata.duration.unwrap_or(Duration::from_millis(0));
let thumbnail = metadata
.thumbnail
.unwrap_or("https://http.cat/images/403.jpg".to_string());
let link = metadata
.source_url
.unwrap_or("https://piped.moonleay.net/403".to_string());
CreateEmbed::new()
.author(CreateEmbedAuthor::new(show_as_author))
.title(title)
.url(link)
.thumbnail(thumbnail)
.field("Artist", artist, true)
.field("Duration", format!("{}min {}sec", duration.as_secs() / 60, duration.as_secs() % 60), true)
.color(Color::from_rgb(81, 224, 26))
.footer(CreateEmbedFooter::new(format!("> {} - {}",current_time, username)))
CreateEmbed::new()
.author(CreateEmbedAuthor::new(show_as_author))
.title(title)
.url(link)
.thumbnail(thumbnail)
.field("Artist", artist, true)
.field(
"Duration",
format!(
"{}min {}sec",
duration.as_secs() / 60,
duration.as_secs() % 60
),
true,
)
.color(Color::from_rgb(81, 224, 26))
.footer(CreateEmbedFooter::new(format!(
"> {} - {}",
current_time, username
)))
}
}