Add prefix customization command
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::{
|
||||
constants::DEFAULT_PREFIX,
|
||||
database::models::Server,
|
||||
discord::sanitizing::SanitizeDiscordMarkdown,
|
||||
framework::commands::{
|
||||
Command, CommandError,
|
||||
arguments::{Argument, GreedyString, ParseArguments},
|
||||
context::CommandContext,
|
||||
},
|
||||
state::State,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PrefixCommand;
|
||||
|
||||
#[async_trait]
|
||||
impl Command<State> for PrefixCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"prefix"
|
||||
}
|
||||
|
||||
fn aliases(&self) -> &'static [&'static str] {
|
||||
&["p"]
|
||||
}
|
||||
|
||||
fn description(&self) -> &'static str {
|
||||
"Sets the command prefix for the server."
|
||||
}
|
||||
|
||||
fn arguments(&self) -> &'static [Argument] {
|
||||
&[Argument {
|
||||
name: "prefix",
|
||||
is_optional: true,
|
||||
is_greedy: true,
|
||||
}]
|
||||
}
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
ctx: CommandContext<'_, State>,
|
||||
args: &str,
|
||||
) -> Result<(), CommandError> {
|
||||
let prefix: GreedyString = ParseArguments::parse(args).unwrap();
|
||||
let mut prefix = prefix.trim();
|
||||
|
||||
let mut server = Server::get_or_create_by_id(
|
||||
ctx.event
|
||||
.guild_id
|
||||
.ok_or(CommandError::ServerCommandRunInDMs)?
|
||||
.get(),
|
||||
&ctx.rt.state.db,
|
||||
&ctx.rt.http,
|
||||
)
|
||||
.await
|
||||
.inspect_err(|e| log::error!("Failed to get or create server: {e}"))
|
||||
.map_err(|_| CommandError::DatabaseQueryFailed)?;
|
||||
|
||||
if prefix.is_empty() {
|
||||
prefix = DEFAULT_PREFIX;
|
||||
}
|
||||
|
||||
server.prefix = prefix.to_string();
|
||||
server
|
||||
.upsert(&ctx.rt.state.db)
|
||||
.await
|
||||
.map_err(|_| CommandError::DatabaseQueryFailed)?;
|
||||
|
||||
ctx.send()
|
||||
.content(format!(
|
||||
"✅ Prefix updated! Run `{}help` to see the new prefix.",
|
||||
prefix.sanitize(false)
|
||||
))
|
||||
.await
|
||||
.map_err(|e| {
|
||||
log::error!("Failed to send prefix update message: {e}");
|
||||
CommandError::MessageSendingFailed
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Adding, updating, and removing servers from the database.
|
||||
//! Syncing servers with the database and server configuration commands.
|
||||
//!
|
||||
//! This sub-module is responsible for registering servers in the database, updating them when they
|
||||
//! update, and removing them when they get deleted or Payphone is kicked.
|
||||
|
||||
pub mod commands;
|
||||
pub mod events;
|
||||
|
||||
Reference in New Issue
Block a user