Add basic moderator commands

This commit is contained in:
2025-08-23 00:02:10 -03:00
parent 8868b18be3
commit eaa38f3474
27 changed files with 752 additions and 249 deletions
+31 -65
View File
@@ -66,8 +66,7 @@ impl Command<State, Extras> for CallCommand {
.await
.inspect_err(|e| {
log::error!("Failed to get or create channel: {e:?}");
})
.map_err(|_| CommandError::DatabaseFailed)?;
})?;
if channel.is_searching || self.channel_lock.contains(&ctx.event.channel_id.get()) {
ctx.send()
@@ -75,8 +74,7 @@ impl Command<State, Extras> for CallCommand {
"☎️ You are already searching for a call! Use `{}hangup` to hang up the phone.",
ctx.prefix
))
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
.await?;
return Ok(());
}
@@ -85,27 +83,21 @@ impl Command<State, Extras> for CallCommand {
ctx.send()
.content(format!("☎️ You are already on a call! Use `{}hangup` to hang up the phone, or `{}skip` to skip to the next call.", ctx.prefix, ctx.prefix))
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
?;
return Ok(());
}
self.channel_lock
.insert_async(ctx.event.channel_id.get())
.await
.bye();
self.channel_lock.insert(ctx.event.channel_id.get()).bye();
let call = Call::start(&channel, &ctx.rt.state.db)
.await
.inspect_err(|e| {
log::error!("Failed to start call: {e:?}");
self.channel_lock.remove(&ctx.event.channel_id.get());
})
.map_err(|_| CommandError::DatabaseFailed)?;
})?;
self.channel_lock
.remove_async(&ctx.event.channel_id.get())
.await;
self.channel_lock.remove(&ctx.event.channel_id.get());
if let Some(call) = call {
ctx.send()
@@ -130,8 +122,7 @@ impl Command<State, Extras> for CallCommand {
get_string_tips(ctx.prefix),
get_string_call_searching(ctx.prefix)
))
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
.await?;
}
Ok(())
@@ -168,8 +159,7 @@ impl Command<State, Extras> for SkipCommand {
.await
.inspect_err(|e| {
log::error!("Failed to get or create channel: {e:?}");
})
.map_err(|_| CommandError::DatabaseFailed)?;
})?;
if channel.call_id.is_none() {
ctx.send()
@@ -177,8 +167,7 @@ impl Command<State, Extras> for SkipCommand {
"❌ You're not on a call! Use `{}call` to start one.",
ctx.prefix
))
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
.await?;
return Ok(());
}
@@ -189,22 +178,19 @@ impl Command<State, Extras> for SkipCommand {
"☎️ You're already searching for a call! Use `{}hangup` to cancel the search.",
ctx.prefix
))
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
.await?;
return Ok(());
}
let call = Call::get_by_id(channel.call_id.unwrap(), &ctx.rt.state.db)
.await
.inspect_err(|e| log::error!("Failed to get call: {e:?}"))
.map_err(|_| CommandError::DatabaseFailed)?
.inspect_err(|e| log::error!("Failed to get call: {e:?}"))?
.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::DatabaseFailed)?;
.inspect_err(|e| log::error!("Failed to hang up call: {e:?}"))?;
let other_channel_id = call.other_party_channel_id(ctx.event.channel_id.get());
@@ -218,15 +204,13 @@ impl Command<State, Extras> for SkipCommand {
.await
.inspect_err(|e| {
log::error!("Failed to send message to other party: {e:?}");
})
.map_err(|_| CommandError::MessageSendingFailed)?;
})?;
let call = Call::start(&channel, &ctx.rt.state.db)
.await
.inspect_err(|e| {
log::error!("Failed to start (skipped) call: {e:?}");
})
.map_err(|_| CommandError::DatabaseFailed)?;
})?;
if let Some(call) = call {
ctx.send()
@@ -248,8 +232,7 @@ impl Command<State, Extras> for SkipCommand {
} else {
ctx.send()
.content("☎️ Skipped! Searching for a call...")
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
.await?;
}
Ok(())
@@ -280,16 +263,14 @@ impl Command<State, Extras> for HangupCommand {
) -> Result<(), CommandError> {
let Some(channel) = Channel::get_by_id(ctx.event.channel_id.get(), &ctx.rt.state.db)
.await
.inspect_err(|e| log::error!("Failed to get channel: {e:?}"))
.map_err(|_| CommandError::DatabaseFailed)?
.inspect_err(|e| log::error!("Failed to get channel: {e:?}"))?
else {
ctx.send()
.content(format!(
"❌ You're not on a call! Use `{}call` to start one.",
ctx.prefix
))
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
.await?;
return Ok(());
};
@@ -300,13 +281,11 @@ impl Command<State, Extras> for HangupCommand {
.await
.inspect_err(|e| {
log::error!("Failed to cancel search: {e:?}");
})
.map_err(|_| CommandError::DatabaseFailed)?;
})?;
ctx.send()
.content(format!("☎️ {}", get_string_call_no_answer(ctx.prefix)))
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
.await?;
return Ok(());
}
@@ -314,21 +293,16 @@ impl Command<State, Extras> for HangupCommand {
if let Some(call_id) = channel.call_id {
let Some(call) = Call::get_by_id(call_id, &ctx.rt.state.db)
.await
.inspect_err(|e| log::error!("Failed to get call: {e:?}"))
.map_err(|_| CommandError::DatabaseFailed)?
.inspect_err(|e| log::error!("Failed to get call: {e:?}"))?
else {
ctx.send()
.content("❌ Call not found.")
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
ctx.send().content("❌ Call not found.").await?;
return Err(CommandError::DatabaseInconsistentState);
};
call.hangup(&ctx.rt.state.db)
.await
.inspect_err(|e| log::error!("Failed to hang up call: {e:?}"))
.map_err(|_| CommandError::DatabaseFailed)?;
.inspect_err(|e| log::error!("Failed to hang up call: {e:?}"))?;
ctx.send()
.content(format!(
@@ -345,16 +319,14 @@ impl Command<State, Extras> for HangupCommand {
"☎️ {}",
get_string_call_hangup_other_party(ctx.prefix)
))
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
.await?;
} else {
ctx.send()
.content(format!(
"❌ You're not on a call! Use `{}call` to start one.",
ctx.prefix
))
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
.await?;
return Ok(());
}
@@ -391,8 +363,7 @@ impl Command<State, Extras> for FriendRequestCommand {
) -> Result<(), CommandError> {
let channel = Channel::get_by_id(ctx.event.channel_id.get(), &ctx.rt.state.db)
.await
.inspect_err(|e| log::error!("Failed to get channel: {e:?}"))
.map_err(|_| CommandError::DatabaseFailed)?;
.inspect_err(|e| log::error!("Failed to get channel: {e:?}"))?;
let Some(channel) = channel else {
ctx.send()
@@ -400,8 +371,7 @@ impl Command<State, Extras> for FriendRequestCommand {
"❌ You're not on a call! Use `{}call` to start one.",
ctx.prefix
))
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
.await?;
return Ok(());
};
@@ -409,16 +379,14 @@ impl Command<State, Extras> for FriendRequestCommand {
let call = if let Some(call_id) = channel.call_id {
Call::get_by_id(call_id, &ctx.rt.state.db)
.await
.inspect_err(|e| log::error!("Failed to get call: {e:?}"))
.map_err(|_| CommandError::DatabaseFailed)?
.inspect_err(|e| log::error!("Failed to get call: {e:?}"))?
} else {
ctx.send()
.content(format!(
"❌ You're not on a call! Use `{}call` to start one.",
ctx.prefix
))
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
.await?;
return Ok(());
};
@@ -426,8 +394,7 @@ impl Command<State, Extras> for FriendRequestCommand {
let Some(call) = call else {
ctx.send()
.content("❌ Oops! We could not find that call.")
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
.await?;
return Err(CommandError::DatabaseInconsistentState);
};
@@ -450,12 +417,11 @@ impl Command<State, Extras> for FriendRequestCommand {
)
)
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
?;
ctx.send()
.content("📬 Asked the other party to send you a friend request!")
.await
.map_err(|_| CommandError::MessageSendingFailed)?;
.await?;
Ok(())
}