Made join command defer (timeout protection), fixed paused track bug

main
DaXcess 2022-11-05 15:53:42 +01:00
parent f3dff49c06
commit 877f758172
4 changed files with 25 additions and 17 deletions

View File

@ -44,6 +44,21 @@ pub async fn respond_message(
} }
} }
pub async fn update_message(
ctx: &Context,
command: &ApplicationCommandInteraction,
options: EmbedMessageOptions,
) {
if let Err(why) = command
.edit_original_interaction_response(&ctx.http, |message| {
message.embed(|embed| make_embed_message(embed, options))
})
.await
{
error!("Error sending message: {:?}", why);
}
}
pub async fn defer_message( pub async fn defer_message(
ctx: &Context, ctx: &Context,
command: &ApplicationCommandInteraction, command: &ApplicationCommandInteraction,

View File

@ -5,7 +5,7 @@ use serenity::{
}; };
use crate::{ use crate::{
bot::commands::{defer_message, respond_message, CommandOutput}, bot::commands::{defer_message, respond_message, update_message, CommandOutput},
session::manager::{SessionCreateError, SessionManager}, session::manager::{SessionCreateError, SessionManager},
utils::embed::{EmbedBuilder, Status}, utils::embed::{EmbedBuilder, Status},
}; };
@ -92,13 +92,13 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
return; return;
} }
defer_message(&ctx, &command, true).await; defer_message(&ctx, &command, false).await;
if let Some(session) = &session_opt { if let Some(session) = &session_opt {
if let Err(why) = session.update_owner(&ctx, command.user.id).await { if let Err(why) = session.update_owner(&ctx, command.user.id).await {
// Need to link first // Need to link first
if let SessionCreateError::NoSpotifyError = why { if let SessionCreateError::NoSpotifyError = why {
respond_message( update_message(
&ctx, &ctx,
&command, &command,
EmbedBuilder::new() EmbedBuilder::new()
@ -107,7 +107,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
.description("You need to link your Spotify account. Use </link:1036714850367320136> or go to [the accounts website](https://account.spoticord.com/) to get started.") .description("You need to link your Spotify account. Use </link:1036714850367320136> or go to [the accounts website](https://account.spoticord.com/) to get started.")
.status(Status::Error) .status(Status::Error)
.build(), .build(),
true,
) )
.await; .await;
@ -115,7 +114,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
} }
// Any other error // Any other error
respond_message( update_message(
&ctx, &ctx,
&command, &command,
EmbedBuilder::new() EmbedBuilder::new()
@ -124,7 +123,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
.description("An error occured while joining the channel. Please try again later.") .description("An error occured while joining the channel. Please try again later.")
.status(Status::Error) .status(Status::Error)
.build(), .build(),
true,
) )
.await; .await;
@ -138,7 +136,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
{ {
// Need to link first // Need to link first
if let SessionCreateError::NoSpotifyError = why { if let SessionCreateError::NoSpotifyError = why {
respond_message( update_message(
&ctx, &ctx,
&command, &command,
EmbedBuilder::new() EmbedBuilder::new()
@ -147,7 +145,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
.description("You need to link your Spotify account. Use </link:1036714850367320136> or go to [the accounts website](https://account.spoticord.com/) to get started.") .description("You need to link your Spotify account. Use </link:1036714850367320136> or go to [the accounts website](https://account.spoticord.com/) to get started.")
.status(Status::Error) .status(Status::Error)
.build(), .build(),
true,
) )
.await; .await;
@ -155,7 +152,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
} }
// Any other error // Any other error
respond_message( update_message(
&ctx, &ctx,
&command, &command,
EmbedBuilder::new() EmbedBuilder::new()
@ -164,7 +161,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
.description("An error occured while joining the channel. Please try again later.") .description("An error occured while joining the channel. Please try again later.")
.status(Status::Error) .status(Status::Error)
.build(), .build(),
true,
) )
.await; .await;
@ -172,7 +168,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
}; };
} }
respond_message( update_message(
&ctx, &ctx,
&command, &command,
EmbedBuilder::new() EmbedBuilder::new()
@ -182,7 +178,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
.footer("Spotify will automatically start playing on Spoticord") .footer("Spotify will automatically start playing on Spoticord")
.status(Status::Success) .status(Status::Success)
.build(), .build(),
false,
) )
.await; .await;
}) })

View File

@ -35,8 +35,6 @@ impl EventHandler for Handler {
// INTERACTION_CREATE event, emitted when the bot receives an interaction (slash command, button, etc.) // INTERACTION_CREATE event, emitted when the bot receives an interaction (slash command, button, etc.)
async fn interaction_create(&self, ctx: Context, interaction: Interaction) { async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
trace!("interaction_create START");
if let Interaction::ApplicationCommand(command) = interaction { if let Interaction::ApplicationCommand(command) = interaction {
// Commands must only be executed inside of guilds // Commands must only be executed inside of guilds
@ -72,7 +70,5 @@ impl EventHandler for Handler {
command_manager.execute_command(&ctx, command).await; command_manager.execute_command(&ctx, command).await;
} }
trace!("interaction_create END");
} }
} }

View File

@ -282,6 +282,8 @@ impl SpoticordSession {
} }
}; };
trace!("Received IPC message: {:?}", msg);
match msg { match msg {
// Sink requests playback to start/resume // Sink requests playback to start/resume
IpcPacket::StartPlayback => { IpcPacket::StartPlayback => {
@ -338,7 +340,7 @@ impl SpoticordSession {
let track_id = SpotifyId::from_uri(&track).unwrap(); let track_id = SpotifyId::from_uri(&track).unwrap();
let was_none = ipc_instance let was_none = ipc_instance
.update_playback(duration_ms, position_ms, true) .update_playback(duration_ms, position_ms, false)
.await; .await;
if was_none { if was_none {