From 55431d580e58664a774c3f1a5544c40d1152fa74 Mon Sep 17 00:00:00 2001 From: Rafael Bradley Date: Thu, 14 Aug 2025 13:42:36 -0300 Subject: [PATCH] Add the skip command and remove the unused `commands` folder --- src/commands/about.rs | 0 src/commands/calls.rs | 0 src/groups/calls/commands.rs | 95 ++++++++++++++++++++++++++++++++++++ src/main.rs | 3 +- 4 files changed, 97 insertions(+), 1 deletion(-) delete mode 100644 src/commands/about.rs delete mode 100644 src/commands/calls.rs diff --git a/src/commands/about.rs b/src/commands/about.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/commands/calls.rs b/src/commands/calls.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/groups/calls/commands.rs b/src/groups/calls/commands.rs index 44b2390..dda24d2 100644 --- a/src/groups/calls/commands.rs +++ b/src/groups/calls/commands.rs @@ -109,6 +109,101 @@ impl Command for CallCommand { } } +#[derive(Debug)] +pub struct SkipCommand; + +#[async_trait] +impl Command for SkipCommand { + fn name(&self) -> &'static str { + "skip" + } + + fn aliases(&self) -> &'static [&'static str] { + &["s", "next"] + } + + async fn execute( + &self, + ctx: CommandContext<'_, State>, + _args: &str, + ) -> Result<(), CommandError> { + let channel = Channel::get_or_create_by_id( + ctx.event.channel_id.get(), + &ctx.rt.state.db, + &ctx.rt.http, + ) + .await + .inspect_err(|e| { + log::error!("Failed to get or create channel: {e:?}"); + }) + .map_err(|_| CommandError::DatabaseQueryFailed)?; + + if channel.call_id.is_none() { + ctx.send() + .content(format!( + "❌ You're not on a call! Use `{}call` to start one.", + ctx.prefix + )) + .await + .map_err(|_| CommandError::MessageSendingFailed)?; + + return Ok(()); + } + + if channel.is_searching { + ctx.send() + .content(format!( + "☎️ You're already searching for a call! Use `{}hangup` to cancel the search.", + ctx.prefix + )) + .await + .map_err(|_| CommandError::MessageSendingFailed)?; + + return Ok(()); + } + + let call = Call::get_by_id(channel.call_id.unwrap(), &ctx.rt.state.db) + .await + .map_err(|_| CommandError::DatabaseQueryFailed)? + .ok_or(CommandError::DatabaseInconsistentState)?; + + call.hangup(&ctx.rt.state.db) + .await + .inspect_err(|e| log::error!("Failed to hang up call: {e:?}")) + .map_err(|_| CommandError::DatabaseQueryFailed)?; + + let other_channel_id = call.other_party_channel_id(ctx.event.channel_id.get()); + + ctx.rt + .http + .create_message(Id::new(other_channel_id)) + .content("☎️ The other party hung up the phone.") + .await + .map_err(|_| CommandError::MessageSendingFailed)?; + + let call = Call::start(&channel, &ctx.rt.state.db) + .await + .map_err(|_| CommandError::DatabaseQueryFailed)?; + + if let Some(call) = call { + ctx.send().content("☎️ Call connected. Say hi!").await.bye(); + ctx.rt + .http + .create_message(Id::new(call.other_party_channel_id(channel.id))) + .content("☎️ Call connected. Say hi!") + .await + .bye(); + } else { + ctx.send() + .content("☎️ Skipped! Searching for a call...") + .await + .map_err(|_| CommandError::MessageSendingFailed)?; + } + + Ok(()) + } +} + #[derive(Debug)] pub struct HangupCommand; diff --git a/src/main.rs b/src/main.rs index fcc5518..6ae855c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,7 +120,7 @@ use crate::{ groups::{ about::{commands::HelpCommand, components::HelpGoBackButton}, calls::{ - commands::{CallCommand, HangupCommand}, + commands::{CallCommand, HangupCommand, SkipCommand}, events::MessageRedirector, }, gateway::events::{ReadyHandler, ReconnectHandler}, @@ -190,6 +190,7 @@ async fn start(token: String, database_addr: String) { .intent(Intents::MESSAGE_CONTENT) .command(HelpCommand) .command(CallCommand::new()) + .command(SkipCommand) .command(HangupCommand) .component(HelpGoBackButton) .on_ready(ReadyHandler)