Add creation of now playing message to play commmand

The message can also be deleted with the delete button.

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/20/head
trivernis 3 years ago
parent 2fcbaef32c
commit 43f61d51f2
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -8,6 +8,7 @@ use crate::commands::music::{
get_channel_for_author, get_queue_for_guild, get_songs_for_query, get_voice_manager,
join_channel, play_next_in_queue,
};
use crate::messages::music::now_playing::create_now_playing_msg;
use crate::providers::settings::{get_setting, Setting};
#[command]
@ -43,7 +44,7 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let queue = get_queue_for_guild(ctx, &guild.id).await?;
let play_first = {
let (play_first, create_now_playing) = {
log::debug!("Adding song to queue");
let mut queue_lock = queue.lock().await;
for song in songs {
@ -57,13 +58,19 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
log::debug!("Autoshuffeling");
queue_lock.shuffle();
}
queue_lock.current().is_none()
(
queue_lock.current().is_none(),
queue_lock.now_playing_msg.is_none(),
)
};
if play_first {
log::debug!("Playing first song in queue");
while !play_next_in_queue(&ctx.http, &msg.channel_id, &queue, &handler_lock).await {}
}
if create_now_playing {
create_now_playing_msg(ctx, queue, msg.channel_id).await?;
}
handle_autodelete(ctx, msg).await?;
Ok(())

@ -8,6 +8,7 @@ use crate::commands::music::{
get_channel_for_author, get_queue_for_guild, get_songs_for_query, get_voice_manager,
join_channel, play_next_in_queue, DJ_CHECK,
};
use crate::messages::music::now_playing::create_now_playing_msg;
#[command]
#[only_in(guilds)]
@ -41,7 +42,7 @@ async fn play_next(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let mut songs = get_songs_for_query(&ctx, msg, query).await?;
let queue = get_queue_for_guild(ctx, &guild.id).await?;
let play_first = {
let (play_first, create_now_playing) = {
let mut queue_lock = queue.lock().await;
songs.reverse();
log::debug!("Enqueueing songs as next songs in the queue");
@ -49,12 +50,18 @@ async fn play_next(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
for song in songs {
queue_lock.add_next(song);
}
queue_lock.current().is_none()
(
queue_lock.current().is_none(),
queue_lock.now_playing_msg.is_none(),
)
};
if play_first {
while !play_next_in_queue(&ctx.http, &msg.channel_id, &queue, &handler).await {}
}
if create_now_playing {
create_now_playing_msg(ctx, queue, msg.channel_id).await?;
}
handle_autodelete(ctx, msg).await?;
Ok(())

@ -21,6 +21,7 @@ use std::env;
use std::time::Duration;
use tokio::sync::{Mutex, RwLock};
static DELETE_BUTTON: &str = "🗑️";
static PAUSE_BUTTON: &str = "⏯️";
static SKIP_BUTTON: &str = "⏭️";
static STOP_BUTTON: &str = "⏹️";
@ -34,6 +35,10 @@ pub async fn create_now_playing_msg(
) -> BotResult<Arc<RwLock<MessageHandle>>> {
log::debug!("Creating now playing menu");
let handle = MenuBuilder::default()
.add_control(-1, DELETE_BUTTON, |c, m, r| {
Box::pin(delete_action(c, m, r))
})
.add_help(DELETE_BUTTON, "Deletes this message")
.add_control(0, STOP_BUTTON, |c, m, r| {
Box::pin(stop_button_action(c, m, r))
})
@ -257,3 +262,25 @@ async fn good_pick_action(
Ok(())
}
async fn delete_action(
ctx: &Context,
menu: &mut Menu<'_>,
reaction: Reaction,
) -> SerenityUtilsResult<()> {
let guild_id = reaction.guild_id.unwrap();
let handle = {
let handle = menu.message.read().await;
handle.clone()
};
{
let queue = get_queue_for_guild(ctx, &guild_id).await?;
let mut queue = queue.lock().await;
queue.now_playing_msg = None;
}
ctx.http
.delete_message(handle.channel_id, handle.message_id)
.await?;
Ok(())
}

Loading…
Cancel
Save