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
+135
View File
@@ -0,0 +1,135 @@
use async_trait::async_trait;
use twilight_model::channel::message::component::ButtonStyle;
use crate::{
database::models::UserIncident,
extras::Extras,
framework::interactions::{InteractionError, components::Button, context::InteractionContext},
groups::moderation::get_incident_page,
state::State,
utils::get_random_u64,
};
#[derive(Debug)]
pub struct ArrowIncidentButton {
pub arrow: char,
pub user_id: u64,
pub go_to: Option<usize>,
}
#[async_trait]
impl Button<State, Extras> for ArrowIncidentButton {
fn id(&self) -> Option<String> {
Some(format!("incidents:arrow:{}", get_random_u64()))
}
fn label(&self) -> Option<String> {
Some(format!("{}", self.arrow))
}
fn disabled(&self) -> bool {
self.go_to.is_none()
}
async fn on_press(
&self,
ctx: InteractionContext<State, Extras>,
) -> Result<(), InteractionError> {
if !ctx.extras.user.is_moderator {
ctx.defer().await?;
return Ok(());
}
if let Some(go_to) = self.go_to {
ctx.edit_original_message(
get_incident_page(self.user_id, &ctx.rt.state.db, go_to)
.await
.inspect_err(|e| log::error!("Failed to get incident page: {e:?}"))?
.into(),
)
.await
.inspect_err(|e| log::error!("Failed to edit original message: {e:?}"))?;
} else {
ctx.defer().await?;
}
Ok(())
}
}
#[derive(Debug)]
pub struct CounterButton {
pub current: usize,
pub total: usize,
}
#[async_trait]
impl Button<State, Extras> for CounterButton {
fn id(&self) -> Option<String> {
Some("incidents:counter".into())
}
fn label(&self) -> Option<String> {
Some(format!("{}/{}", self.current, self.total))
}
fn disabled(&self) -> bool {
true
}
}
#[derive(Debug)]
pub struct DeleteIncidentButton {
pub user_id: u64,
pub incident_id: u64,
pub incident_index: usize,
}
#[async_trait]
impl Button<State, Extras> for DeleteIncidentButton {
fn id(&self) -> Option<String> {
Some(format!("incidents:delete:{}", get_random_u64()))
}
fn label(&self) -> Option<String> {
Some("Delete Incident".into())
}
fn style(&self) -> ButtonStyle {
ButtonStyle::Danger
}
fn disabled(&self) -> bool {
false
}
async fn on_press(
&self,
ctx: InteractionContext<State, Extras>,
) -> Result<(), InteractionError> {
if !ctx.extras.user.is_moderator {
ctx.defer().await?;
return Ok(());
}
UserIncident::delete_by_user_and_incident_id(
self.user_id,
self.incident_id,
&ctx.rt.state.db,
)
.await?;
let new_page_index = self.incident_index.saturating_sub(1);
ctx.edit_original_message(
get_incident_page(self.user_id, &ctx.rt.state.db, new_page_index)
.await
.inspect_err(|e| log::error!("Failed to get incident page: {e:?}"))?
.into(),
)
.await
.inspect_err(|e| log::error!("Failed to edit original message: {e:?}"))?;
Ok(())
}
}