Add prefix getter for custom server prefixes and Payphone mentions

This commit is contained in:
2025-08-14 17:22:33 -03:00
parent 2b4c3d0b8b
commit f4a282447a
+31 -6
View File
@@ -107,6 +107,7 @@ use std::{
sync::mpsc, sync::mpsc,
}; };
use async_trait::async_trait;
use axum::{Router, response::Redirect, routing}; use axum::{Router, response::Redirect, routing};
use clap::Parser; use clap::Parser;
use notify::{Event, EventKind, Watcher}; use notify::{Event, EventKind, Watcher};
@@ -115,9 +116,10 @@ use tower_http::services::ServeDir;
use twilight_gateway::Intents; use twilight_gateway::Intents;
use crate::{ use crate::{
cli::{Args, Commands}, cli::{Args, Commands}, constants::DEFAULT_PREFIX, database::models::Server, framework::{
framework::bot::Bot, bot::{Bot, PrefixGetter},
groups::{ runtime::Runtime,
}, groups::{
about::{commands::HelpCommand, components::HelpGoBackButton}, about::{commands::HelpCommand, components::HelpGoBackButton},
calls::{ calls::{
commands::{CallCommand, HangupCommand, SkipCommand}, commands::{CallCommand, HangupCommand, SkipCommand},
@@ -125,8 +127,7 @@ use crate::{
}, },
gateway::events::{ReadyHandler, ReconnectHandler}, gateway::events::{ReadyHandler, ReconnectHandler},
servers::events::{ServerCreateHandler, ServerDeleteHandler, ServerUpdateHandler}, servers::events::{ServerCreateHandler, ServerDeleteHandler, ServerUpdateHandler},
}, }, state::State
state::State,
}; };
mod cli; mod cli;
@@ -166,6 +167,30 @@ pub async fn main() {
} }
} }
struct ServerPrefixOrPayphoneMention;
#[async_trait]
impl PrefixGetter<State> for ServerPrefixOrPayphoneMention {
async fn get(
&self,
rt: &Runtime<State>,
server_id: Option<u64>,
_user_id: Option<u64>,
) -> Vec<String> {
let mention = format!("<@{}>", rt.app.bot.as_ref().unwrap().id);
let default_prefixes = vec![DEFAULT_PREFIX.to_string(), mention.clone()];
if let Some(server_id) = server_id {
if let Ok(Some(server)) = Server::get_by_id(server_id, &rt.state.db).await {
return vec![server.prefix, mention];
}
}
default_prefixes
}
}
/// Starts a Payphone shard. /// Starts a Payphone shard.
/// ///
/// TODO: Support multiple shards. /// TODO: Support multiple shards.
@@ -184,7 +209,7 @@ async fn start(token: String, database_addr: String) {
let state = State { db }; let state = State { db };
let bot = Bot::new(state, ["p."]) let bot = Bot::new(state, ServerPrefixOrPayphoneMention)
.intent(Intents::GUILDS) .intent(Intents::GUILDS)
.intent(Intents::GUILD_MESSAGES) .intent(Intents::GUILD_MESSAGES)
.intent(Intents::MESSAGE_CONTENT) .intent(Intents::MESSAGE_CONTENT)