use crate::config::RespondTo;
pub(crate) fn is_respond_to_command(text: &str) -> bool {
text.trim()
.strip_prefix("/respond_to")
.is_some_and(|rest| rest.is_empty() || rest.starts_with(char::is_whitespace))
}
pub(crate) fn parse_mode(arg: &str) -> Option<RespondTo> {
match arg.to_lowercase().as_str() {
"all" => Some(RespondTo::All),
"mention" | "mentions" => Some(RespondTo::Mention),
"auto" => Some(RespondTo::Auto),
"dm_only" => Some(RespondTo::DmOnly),
_ => None,
}
}
pub(crate) fn respond_to_discord_channel(
text: &str,
is_owner: bool,
channel_id: &str,
effective: &RespondTo,
write: impl FnOnce(&str, &str) -> Result<(), String>,
) -> Option<String> {
let rest = text.trim().strip_prefix("/respond_to")?;
if !rest.is_empty() && !rest.starts_with(char::is_whitespace) {
return None;
}
if !is_owner {
return Some(RESTRICTED.to_string());
}
let arg = rest.trim();
if arg.is_empty() {
return Some(format!(
"đĸ Respond-to mode for this Discord channel: **{}**.\n\
Switch it with `/respond_to <all|dm_only|mention|auto>`, or set `[channels.discord.channels.{channel_id}] respond_to`.",
mode_label(effective)
));
}
let Some(mode) = parse_mode(arg) else {
return Some(format!(
"â Unknown mode \"{arg}\". Use: `/respond_to <all|dm_only|mention|auto>`"
));
};
let label = mode_label(&mode);
match write(channel_id, label) {
Ok(()) => Some(format!(
"â
Respond-to mode for this Discord channel switched to **{label}**."
)),
Err(e) => Some(format!("â Could not save the respond mode: {e}")),
}
}
const RESTRICTED: &str = "đ `/respond_to` is restricted to the bot owner.";
pub(crate) fn mode_label(mode: &RespondTo) -> &'static str {
match mode {
RespondTo::All => "all",
RespondTo::DmOnly => "dm_only",
RespondTo::Mention => "mention",
RespondTo::Auto => "auto",
}
}
pub(crate) fn respond_to_outside_telegram(
text: &str,
is_owner: bool,
platform: &str,
mode: Option<&RespondTo>,
) -> Option<String> {
let rest = text.trim().strip_prefix("/respond_to")?;
if !rest.is_empty() && !rest.starts_with(char::is_whitespace) {
return None;
}
if !is_owner {
return Some(RESTRICTED.to_string());
}
let key = platform.to_lowercase();
let Some(mode) = mode else {
return Some(format!(
"âšī¸ {platform} has no respond mode: it is set by `response_policy` under `[channels.{key}]`."
));
};
if rest.trim().is_empty() {
return Some(format!(
"đĸ Respond-to mode for {platform} (all channels): **{}**.\n\
Set it in `[channels.{key}] respond_to`. Per-channel modes are tracked in #2014.",
mode_label(mode)
));
}
Some(format!(
"âšī¸ `/respond_to` does not change {platform} yet: per-channel switching is tracked in #2014. \
Edit `[channels.{key}] respond_to` instead. Nothing was written."
))
}