Add basic moderator commands
This commit is contained in:
@@ -22,10 +22,7 @@ use crate::{
|
||||
/// [`Result<User, ArgumentParser>`] - The user from the DB, or an [`ArgumentParsingError`] ready
|
||||
/// to be returned by the parser.
|
||||
async fn get_user_by_id(id: u64, db: &Session) -> Result<User, ArgumentParsingError> {
|
||||
let Some(user) = User::get_by_id(id, db)
|
||||
.await
|
||||
.map_err(|_| ArgumentParsingError::DatabaseFailed)?
|
||||
else {
|
||||
let Some(user) = User::get_by_id(id, db).await? else {
|
||||
return Err(ArgumentParsingError::InvalidArgument);
|
||||
};
|
||||
Ok(user)
|
||||
@@ -49,8 +46,7 @@ async fn user_from_reply_or_command_executor(
|
||||
if referenced.author.bot {
|
||||
if let Some(message) =
|
||||
Message::get_by_propagation_message_id(referenced.id.get(), &ctx.rt.state.db)
|
||||
.await
|
||||
.map_err(|_| ArgumentParsingError::DatabaseFailed)?
|
||||
.await?
|
||||
{
|
||||
return get_user_by_id(message.author_id, &ctx.rt.state.db).await;
|
||||
}
|
||||
@@ -112,10 +108,7 @@ impl FromArguments<State, Extras> for UserArgument {
|
||||
} else {
|
||||
// The argument is (probably) a username.
|
||||
|
||||
let Some(db_user) = User::get_by_name(identifier.0, &ctx.rt.state.db)
|
||||
.await
|
||||
.map_err(|_| ArgumentParsingError::DatabaseFailed)?
|
||||
else {
|
||||
let Some(db_user) = User::get_by_name(identifier.0, &ctx.rt.state.db).await? else {
|
||||
// Check in the message's reply, and if not use the command executor.
|
||||
user = user_from_reply_or_command_executor(ctx).await?;
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::{
|
||||
database::models::UserIncident,
|
||||
extras::Extras,
|
||||
framework::{
|
||||
commands::{
|
||||
@@ -14,10 +13,10 @@ use crate::{
|
||||
},
|
||||
groups::moderation::{
|
||||
arguments::UserArgument,
|
||||
get_incident_page,
|
||||
permissions::{BotModeratorPermission, BotOwnerPermission},
|
||||
},
|
||||
state::State,
|
||||
utils::Bye,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -58,14 +57,11 @@ impl Command<State, Extras> for ModeratorCommand {
|
||||
Ok(arg) => arg,
|
||||
Err(e) => match e {
|
||||
ArgumentParsingError::InvalidArgument => {
|
||||
ctx.send()
|
||||
.content("❌ User not found in DB.")
|
||||
.await
|
||||
.map_err(|_| CommandError::MessageSendingFailed)?;
|
||||
ctx.send().content("❌ User not found in DB.").await?;
|
||||
return Ok(());
|
||||
}
|
||||
ArgumentParsingError::DatabaseFailed => {
|
||||
return Err(CommandError::DatabaseFailed);
|
||||
ArgumentParsingError::DatabaseFailed(e) => {
|
||||
return Err(CommandError::DatabaseFailed(e));
|
||||
}
|
||||
e => {
|
||||
unreachable!("Unexpected error: {e}");
|
||||
@@ -75,9 +71,7 @@ impl Command<State, Extras> for ModeratorCommand {
|
||||
|
||||
user.is_moderator = !user.is_moderator;
|
||||
|
||||
user.insert(&ctx.rt.state.db)
|
||||
.await
|
||||
.map_err(|_| CommandError::DatabaseFailed)?;
|
||||
user.insert(&ctx.rt.state.db).await?;
|
||||
|
||||
ctx.send()
|
||||
.content(format!(
|
||||
@@ -89,8 +83,7 @@ impl Command<State, Extras> for ModeratorCommand {
|
||||
"**is no longer** a moderator"
|
||||
}
|
||||
))
|
||||
.await
|
||||
.map_err(|_| CommandError::MessageSendingFailed)?;
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -126,14 +119,11 @@ impl Command<State, Extras> for IncidentsCommand {
|
||||
Ok(Arg(user)) => user,
|
||||
Err(e) => match e {
|
||||
ArgumentParsingError::InvalidArgument => {
|
||||
ctx.send()
|
||||
.content("❌ User not found in DB.")
|
||||
.await
|
||||
.map_err(|_| CommandError::MessageSendingFailed)?;
|
||||
ctx.send().content("❌ User not found in DB.").await?;
|
||||
return Ok(());
|
||||
}
|
||||
ArgumentParsingError::DatabaseFailed => {
|
||||
return Err(CommandError::DatabaseFailed);
|
||||
ArgumentParsingError::DatabaseFailed(e) => {
|
||||
return Err(CommandError::DatabaseFailed(e));
|
||||
}
|
||||
e => {
|
||||
unreachable!("Unexpected error: {e}");
|
||||
@@ -141,17 +131,9 @@ impl Command<State, Extras> for IncidentsCommand {
|
||||
},
|
||||
};
|
||||
|
||||
let incidents = UserIncident::get_for_user(user.id, &ctx.rt.state.db)
|
||||
.await
|
||||
.map_err(|_| CommandError::DatabaseFailed)?;
|
||||
let message = get_incident_page(user.id, &ctx.rt.state.db, 0).await?;
|
||||
|
||||
ctx.send()
|
||||
.content(format!(
|
||||
"Great! The user fetched from the DB is {}",
|
||||
user.name
|
||||
))
|
||||
.await
|
||||
.bye();
|
||||
ctx.send_from_builder(message).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
use time::UtcDateTime;
|
||||
use twilight_model::channel::message::Embed;
|
||||
use twilight_util::builder::embed::{
|
||||
EmbedAuthorBuilder, EmbedBuilder, EmbedFieldBuilder, ImageSource,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
constants::{COLOR_RED, COLOR_YELLOW},
|
||||
database::models::{UserIncident, UserIncidentType},
|
||||
utils::ellipse,
|
||||
};
|
||||
|
||||
fn format_timestamp(timestamp: UtcDateTime) -> String {
|
||||
format!(
|
||||
"<t:{}> (<t:{}:R>)",
|
||||
timestamp.unix_timestamp(),
|
||||
timestamp.unix_timestamp()
|
||||
)
|
||||
}
|
||||
|
||||
pub fn incident_embed(incident: &UserIncident) -> Embed {
|
||||
if let Some(message) = &incident.message {
|
||||
let mut builder = EmbedBuilder::new()
|
||||
.color(match incident.r#type {
|
||||
UserIncidentType::Ban => COLOR_RED,
|
||||
UserIncidentType::Warning => COLOR_YELLOW,
|
||||
})
|
||||
.author(
|
||||
EmbedAuthorBuilder::new(&message.author_name)
|
||||
.icon_url(ImageSource::url(&message.author_avatar_url).unwrap()),
|
||||
)
|
||||
.title("Message Incident")
|
||||
.thumbnail(ImageSource::url(message.author_avatar_url.clone()).unwrap())
|
||||
.field(EmbedFieldBuilder::new("Message", ellipse(message.content.clone(), 100)).build())
|
||||
.field(EmbedFieldBuilder::new("Author Name", message.author_name.clone()).build())
|
||||
.field(
|
||||
EmbedFieldBuilder::new(
|
||||
"Author Avatar",
|
||||
format!("[Click to open avatar](<{}>)", &message.author_avatar_url),
|
||||
)
|
||||
.build(),
|
||||
)
|
||||
.field(
|
||||
EmbedFieldBuilder::new("Created At", format_timestamp(incident.created_at)).build(),
|
||||
)
|
||||
.field(EmbedFieldBuilder::new(
|
||||
"Created By",
|
||||
incident
|
||||
.created_by_id
|
||||
.map(|id| format!("<@{id}>"))
|
||||
.unwrap_or("Automod".into()),
|
||||
));
|
||||
|
||||
if incident.r#type == UserIncidentType::Ban {
|
||||
builder = builder.field(
|
||||
EmbedFieldBuilder::new(
|
||||
"Expires At",
|
||||
incident
|
||||
.expires_at
|
||||
.map(format_timestamp)
|
||||
.unwrap_or("*Never*".into()),
|
||||
)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
|
||||
builder.build()
|
||||
} else {
|
||||
let mut builder = EmbedBuilder::new()
|
||||
.color(match incident.r#type {
|
||||
UserIncidentType::Ban => COLOR_RED,
|
||||
UserIncidentType::Warning => COLOR_YELLOW,
|
||||
})
|
||||
.title("Manual Incident")
|
||||
.field(EmbedFieldBuilder::new("Reason", incident.reason.clone()))
|
||||
.field(
|
||||
EmbedFieldBuilder::new("Created At", format_timestamp(incident.created_at)).build(),
|
||||
)
|
||||
.field(EmbedFieldBuilder::new(
|
||||
"Created By",
|
||||
incident
|
||||
.created_by_id
|
||||
.map(|id| format!("<@{id}>"))
|
||||
.unwrap_or("Automod".into()),
|
||||
));
|
||||
|
||||
if incident.r#type == UserIncidentType::Ban {
|
||||
builder = builder.field(
|
||||
EmbedFieldBuilder::new(
|
||||
"Expires At",
|
||||
incident
|
||||
.expires_at
|
||||
.map(format_timestamp)
|
||||
.unwrap_or("*Never*".into()),
|
||||
)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
|
||||
builder.build()
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,68 @@
|
||||
use scylla::client::session::Session;
|
||||
|
||||
use crate::{
|
||||
database::models::{DatabaseError, UserIncident},
|
||||
extras::Extras,
|
||||
framework::messages::MessageBuilder,
|
||||
groups::moderation::{
|
||||
components::{ArrowIncidentButton, CounterButton, DeleteIncidentButton},
|
||||
embeds::incident_embed,
|
||||
},
|
||||
state::State,
|
||||
};
|
||||
|
||||
pub mod arguments;
|
||||
pub mod commands;
|
||||
pub mod components;
|
||||
pub(super) mod embeds;
|
||||
pub mod permissions;
|
||||
|
||||
pub(super) async fn get_incident_page(
|
||||
user_id: u64,
|
||||
session: &Session,
|
||||
page_index: usize,
|
||||
) -> Result<MessageBuilder<State, Extras>, DatabaseError> {
|
||||
let incidents = UserIncident::get_for_user(user_id, session).await?;
|
||||
|
||||
if let Some(incident) = incidents.get(page_index) {
|
||||
Ok(MessageBuilder::new()
|
||||
.embed(incident_embed(incident))
|
||||
.component(
|
||||
ArrowIncidentButton {
|
||||
arrow: '<',
|
||||
user_id,
|
||||
go_to: if page_index > 0 {
|
||||
Some(page_index - 1)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
},
|
||||
1,
|
||||
)
|
||||
.component(
|
||||
CounterButton {
|
||||
current: page_index + 1,
|
||||
total: incidents.len(),
|
||||
},
|
||||
1,
|
||||
)
|
||||
.component(
|
||||
ArrowIncidentButton {
|
||||
arrow: '>',
|
||||
user_id,
|
||||
go_to: incidents.get(page_index + 1).map(|_| page_index + 1),
|
||||
},
|
||||
1,
|
||||
)
|
||||
.component(
|
||||
DeleteIncidentButton {
|
||||
incident_id: incident.incident_id,
|
||||
incident_index: page_index,
|
||||
user_id,
|
||||
},
|
||||
1,
|
||||
))
|
||||
} else {
|
||||
Ok(MessageBuilder::new().content("No incidents found for this user."))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,14 +21,12 @@ impl Permission<State, Extras> for BotOwnerPermission {
|
||||
) -> Result<(), CommandError> {
|
||||
ctx.send()
|
||||
.content("⛔ Not enough aura to run this command.")
|
||||
.await
|
||||
.map_err(|_| CommandError::MessageSendingFailed)?;
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BotModeratorPermission;
|
||||
|
||||
@@ -45,7 +43,7 @@ impl Permission<State, Extras> for BotModeratorPermission {
|
||||
ctx.send()
|
||||
.content("⛔ You're not a Payphone moderator.\n-# If you need to check your incidents, contact a Payphone moderator in the official server.")
|
||||
.await
|
||||
.map_err(|_| CommandError::MessageSendingFailed)?;
|
||||
?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user