From 501ba27e1cd52741988113ef47ee0fad7d0a5799 Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Tue, 6 Aug 2024 16:32:35 -0400 Subject: fixup unknown command, document commands --- src/util/mod.rs | 36 ++++++++++++++++++++++++++++++++---- src/util/rest_vec.rs | 5 +++-- 2 files changed, 35 insertions(+), 6 deletions(-) (limited to 'src/util') diff --git a/src/util/mod.rs b/src/util/mod.rs index a0105ac..f362ff7 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -3,7 +3,10 @@ use std::process::Stdio; use chrono::Duration; use lazy_static::lazy_static; use log::debug; -use poise::CreateReply; +use poise::{ + CreateReply, + FrameworkError, +}; use regex::{ Match, Regex, @@ -35,7 +38,7 @@ use crate::{ mod rest_vec; -pub use rest_vec::RestVec; +pub use rest_vec::*; pub async fn currently_playing(ctx: PoiseContext<'_>) -> bool { let (_sb, call) = songbird(ctx).await.expect("no songbird"); @@ -61,9 +64,9 @@ pub async fn users_listening(ctx: &Context) -> Result { } #[inline] -pub fn msg(ctx: PoiseContext<'_>) -> Option<&Message> { +pub fn msg(ctx: poise::Context<'_, U, E>) -> Option<&Message> { match ctx { - PoiseContext::Prefix(poise::PrefixContext { + poise::Context::Prefix(poise::PrefixContext { msg, .. }) => Some(msg), @@ -71,6 +74,31 @@ pub fn msg(ctx: PoiseContext<'_>) -> Option<&Message> { } } +#[inline] +pub fn err_msg<'a, U, E>(err: &'a FrameworkError) -> Option<&'a Message> { + use FrameworkError::*; + + if let Some(ctx) = err.ctx() { + return msg(ctx); + } + + match *err { + UnknownCommand { + msg, + .. + } + | NonCommandMessage { + msg, + .. + } + | DynamicPrefix { + msg, + .. + } => Some(msg), + _ => None, + } +} + #[inline] pub fn tts(ctx: PoiseContext<'_>) -> Option { msg(ctx).map(|msg| msg.tts) diff --git a/src/util/rest_vec.rs b/src/util/rest_vec.rs index 82889cd..31b783b 100644 --- a/src/util/rest_vec.rs +++ b/src/util/rest_vec.rs @@ -1,15 +1,16 @@ +use std::error::Error; + use serenity::all::{ Context, Message, }; -use std::error::Error; /// Pop a whitespace-separated word from the front of the arguments. Supports quotes and quote /// escaping. /// /// Leading whitespace will be trimmed; trailing whitespace is not consumed. // From https://github.com/serenity-rs/poise/blob/current/src/prefix_argument/mod.rs -fn pop_string(args: &str) -> Result<(&str, String), poise::TooFewArguments> { +pub fn pop_string(args: &str) -> Result<(&str, String), poise::TooFewArguments> { // TODO: consider changing the behavior to parse quotes literally if they're in the middle // of the string: // - `"hello world"` => `hello world` -- cgit v1.3.1