Support help that is hidden in less explicit mode.
This commit is contained in:
parent
887b69340f
commit
e5e6c45e81
@ -164,6 +164,18 @@ impl DBTrans {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_session_model(self: &Self, session: &ListenerSession) -> DResult<Option<Session>> {
|
||||
match self.pg_trans()?
|
||||
.query_opt("SELECT details FROM sessions WHERE session = $1", &[&session.session])
|
||||
.await? {
|
||||
None => Ok(None),
|
||||
Some(row) =>
|
||||
Ok(Some(serde_json::from_value(
|
||||
row.get("details")
|
||||
)?))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn commit(mut self: Self) -> DResult<()> {
|
||||
let trans_opt = self.with_trans_mut(|t| std::mem::replace(t, None));
|
||||
for trans in trans_opt {
|
||||
|
@ -4,6 +4,7 @@ use crate::db::{DBTrans, DBPool};
|
||||
use ansi_macro::ansi;
|
||||
use phf::phf_map;
|
||||
use async_trait::async_trait;
|
||||
use crate::models::session::Session;
|
||||
|
||||
mod parsing;
|
||||
mod ignore;
|
||||
@ -11,6 +12,7 @@ mod help;
|
||||
|
||||
pub struct VerbContext<'l> {
|
||||
session: &'l ListenerSession,
|
||||
session_dat: &'l mut Session,
|
||||
trans: &'l DBTrans
|
||||
}
|
||||
|
||||
@ -48,8 +50,13 @@ static ALWAYS_AVAILABLE_COMMANDS: UserVerbRegistry = phf_map! {
|
||||
pub async fn handle(session: &ListenerSession, msg: &str, pool: &DBPool) -> DResult<()> {
|
||||
let (cmd, params) = parsing::parse_command_name(msg);
|
||||
let trans = pool.start_transaction().await?;
|
||||
let mut session_dat = match trans.get_session_model(session).await? {
|
||||
None => { return Ok(()) }
|
||||
Some(v) => v
|
||||
};
|
||||
let handler_opt = ALWAYS_AVAILABLE_COMMANDS.get(cmd);
|
||||
|
||||
let ctx = VerbContext { session, trans: &trans, session_dat: &mut session_dat };
|
||||
|
||||
match handler_opt {
|
||||
None => {
|
||||
trans.queue_for_session(session,
|
||||
@ -59,7 +66,7 @@ pub async fn handle(session: &ListenerSession, msg: &str, pool: &DBPool) -> DRes
|
||||
).await?;
|
||||
}
|
||||
Some(handler) => {
|
||||
match handler.handle(&VerbContext { session, trans: &trans }, cmd, params).await {
|
||||
match handler.handle(&ctx, cmd, params).await {
|
||||
Ok(()) => {}
|
||||
Err(UserError(err_msg)) => {
|
||||
trans.queue_for_session(session, &(err_msg + "\r\n")).await?;
|
||||
|
@ -19,13 +19,24 @@ static HELP_PAGES: phf::Map<&'static str, &'static str> = phf_map! {
|
||||
\t<bold>help register<reset> will tell you about the register command.")
|
||||
};
|
||||
|
||||
static EXPLICIT_HELP_PAGES: phf::Map<&'static str, &'static str> = phf_map! {
|
||||
"fuck" =>
|
||||
ansi!("Type <bold>fuck <lt>name><reset> to fuck someone. It only works if \
|
||||
they have consented.")
|
||||
};
|
||||
|
||||
pub struct Verb;
|
||||
#[async_trait]
|
||||
impl UserVerb for Verb {
|
||||
async fn handle(self: &Self, ctx: &VerbContext, _verb: &str, remaining: &str) -> UResult<()> {
|
||||
let help = HELP_PAGES.get(remaining).ok_or(
|
||||
let mut help = None;
|
||||
if !ctx.session_dat.less_explicit_mode {
|
||||
help = help.or_else(|| EXPLICIT_HELP_PAGES.get(remaining))
|
||||
}
|
||||
help = help.or_else(|| HELP_PAGES.get(remaining));
|
||||
let help_final = help.ok_or(
|
||||
UserError("No help available on that".to_string()))?;
|
||||
ctx.trans.queue_for_session(ctx.session, &(help.to_string() + "\r\n")).await?;
|
||||
ctx.trans.queue_for_session(ctx.session, &(help_final.to_string() + "\r\n")).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user