From b21fcc65f37cf23aaa35d3601fa0b2ef8c891444 Mon Sep 17 00:00:00 2001 From: oupson Date: Sun, 20 Jun 2021 16:30:12 +0200 Subject: [PATCH] CreateMessageExt --- src/commands/music/mod.rs | 16 ++--- src/commands/music/utils.rs | 133 ++++++++++++++++++++++++------------ 2 files changed, 96 insertions(+), 53 deletions(-) diff --git a/src/commands/music/mod.rs b/src/commands/music/mod.rs index acd81df..6195ba6 100644 --- a/src/commands/music/mod.rs +++ b/src/commands/music/mod.rs @@ -19,7 +19,7 @@ mod utils; mod youtube; use crate::utils::{message::embed_response, permissions::has_permission}; -use utils::embed_song; +use utils::CreateMessageExt; use self::utils::{can_use_voice_command, is_mute}; @@ -38,10 +38,7 @@ impl songbird::EventHandler for TrackStartNotifier { let metadata = np.metadata(); if let Err(why) = self .chan_id - .send_message(&self.http, |m| { - embed_song(m, "Now playing", metadata, None); - m - }) + .send_message(&self.http, |m| m.embed_song("Now playing", metadata, None)) .await { error!("Error sending message: {:?}", why); @@ -67,6 +64,7 @@ struct Music; #[command] #[description("Join the channel you are connected")] +#[aliases("j")] async fn join(ctx: &Context, msg: &Message) -> CommandResult { let guild = match msg.guild(&ctx.cache).await { Some(guild) => guild, @@ -154,6 +152,7 @@ async fn join(ctx: &Context, msg: &Message) -> CommandResult { &format!("Joined channel {}", connect_to.mention()), Some(&member), ) + .add_music_buttons() }) .await?; } else { @@ -441,8 +440,7 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { let author = msg.member(&ctx).await?; msg.channel_id .send_message(&ctx.http, |m| { - embed_song( - m, + m.embed_song( if handler.queue().len() == 1 { "Now playing" } else { @@ -715,7 +713,7 @@ async fn remove(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { handler.queue().skip()?; msg.channel_id .send_message(&ctx.http, |m| { - embed_song(m, "Removed", track.metadata(), Some(&member)) + m.embed_song("Removed", track.metadata(), Some(&member)) }) .await?; } @@ -732,7 +730,7 @@ async fn remove(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { Some(q) => { msg.channel_id .send_message(&ctx.http, |m| { - embed_song(m, "Removed", q.metadata(), Some(&member)) + m.embed_song("Removed", q.metadata(), Some(&member)) }) .await?; } diff --git a/src/commands/music/utils.rs b/src/commands/music/utils.rs index 11db9fc..9d72b9c 100644 --- a/src/commands/music/utils.rs +++ b/src/commands/music/utils.rs @@ -3,9 +3,10 @@ use serenity::{ client::Context, framework::standard::CommandResult, model::{ - channel::Message, + channel::{Message, ReactionType}, guild::{Member, PartialMember}, id::GuildId, + interactions::ButtonStyle, Permissions, }, }; @@ -18,49 +19,6 @@ use crate::{ use super::error::UseVoiceError; -pub(crate) fn embed_song<'a, 'b>( - msg: &'a mut CreateMessage<'b>, - title: &str, - metadata: &Metadata, - author: Option<&Member>, -) -> &'a mut CreateMessage<'b> { - msg.embed(|e| { - e.title(title); - - if let Some(title) = &metadata.title { - e.field("Title", title, true); - } - - if let Some(url) = &metadata.source_url { - e.url(url); - } - - if let Some(duration) = &metadata.duration { - let seconds = duration.as_secs(); - - let hours = seconds / (60 * 60); - let min = (seconds - (hours * 60 * 60)) / 60; - let seconds = seconds - (hours * 3600) - (min * 60); - - e.field( - "Duration", - if hours > 0 { - format!("{}:{:02}:{:02}", hours, min, seconds) - } else { - format!("{}:{:02}", min, seconds) - }, - false, - ); - } - - if let Some(img) = &metadata.thumbnail { - e.image(img); - } - - embed_author(e, author).colour((247, 76, 0)) - }) -} - pub(crate) async fn is_mute( ctx: &Context, member: &PartialMember, @@ -121,3 +79,90 @@ pub(crate) async fn can_use_voice_command( Err(UseVoiceError::Unknown) } } + +pub(crate) trait CreateMessageExt { + fn embed_song( + &mut self, + title: &str, + metadata: &Metadata, + author: Option<&Member>, + ) -> &mut Self; + fn add_music_buttons(&mut self) -> &mut Self; +} + +impl<'a> CreateMessageExt for CreateMessage<'a> { + fn embed_song<'b>( + &'b mut self, + title: &str, + metadata: &Metadata, + author: Option<&Member>, + ) -> &'b mut CreateMessage<'a> { + self.embed(|e| { + e.title(title); + + if let Some(title) = &metadata.title { + e.field("Title", title, true); + } + + if let Some(url) = &metadata.source_url { + e.url(url); + } + + if let Some(duration) = &metadata.duration { + let seconds = duration.as_secs(); + + let hours = seconds / (60 * 60); + let min = (seconds - (hours * 60 * 60)) / 60; + let seconds = seconds - (hours * 3600) - (min * 60); + + e.field( + "Duration", + if hours > 0 { + format!("{}:{:02}:{:02}", hours, min, seconds) + } else { + format!("{}:{:02}", min, seconds) + }, + false, + ); + } + + if let Some(img) = &metadata.thumbnail { + e.image(img); + } + + embed_author(e, author).colour((247, 76, 0)) + }) + } + + fn add_music_buttons<'b>(&'b mut self) -> &'b mut Self { + self.components(|comp| { + comp.create_action_row(|row| { + row.create_button(|but| { + but.style(ButtonStyle::Primary) + .emoji(ReactionType::Unicode("⏮️".to_string())) + .custom_id("music_previous_button") + }) + .create_button(|but| { + but.style(ButtonStyle::Primary) + .emoji(ReactionType::Unicode("⏹️".to_string())) + .custom_id("music_stop_button") + }) + .create_button(|but| { + but.style(ButtonStyle::Primary) + .emoji(ReactionType::Unicode("⏯️".to_string())) + .custom_id("music_play_pause_button") + }) + .create_button(|but| { + but.style(ButtonStyle::Primary) + .emoji(ReactionType::Unicode("⏏️".to_string())) + .custom_id("music_leave_button") + }) + .create_button(|but| { + but.style(ButtonStyle::Primary) + .emoji(ReactionType::Unicode("⏭".to_string())) + .custom_id("music_next_button") + }) + }) + }) + } +}