refactor: better scope eta messages const

This commit is contained in:
seth 2024-01-27 23:10:00 -05:00
parent 72e171b960
commit 2b3d81cfa4
No known key found for this signature in database
GPG key ID: D31BD0D494BBEE86
3 changed files with 23 additions and 37 deletions

View file

@ -13,22 +13,3 @@ pub static COLORS: Lazy<HashMap<&str, Color>> = Lazy::new(|| {
// TODO purple & pink :D // TODO purple & pink :D
]) ])
}); });
pub const ETA_MESSAGES: [&str; 16] = [
"Sometime",
"Some day",
"Not far",
"The future",
"Never",
"Perhaps tomorrow?",
"There are no ETAs",
"No",
"Nah",
"Yes",
"Yas",
"Next month",
"Next year",
"Next week",
"In Prism Launcher 2.0.0",
"At the appropriate juncture, in due course, in the fullness of time",
];

View file

@ -1,12 +1,30 @@
use crate::{consts, utils};
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use poise::serenity_prelude::{Context, Message}; use poise::serenity_prelude::{Context, Message};
use rand::seq::SliceRandom;
use regex::Regex; use regex::Regex;
static ETA_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\beta\b").unwrap()); static ETA_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\beta\b").unwrap());
pub const ETA_MESSAGES: [&str; 16] = [
"Sometime",
"Some day",
"Not far",
"The future",
"Never",
"Perhaps tomorrow?",
"There are no ETAs",
"No",
"Nah",
"Yes",
"Yas",
"Next month",
"Next year",
"Next week",
"In Prism Launcher 2.0.0",
"At the appropriate juncture, in due course, in the fullness of time",
];
pub async fn handle(ctx: &Context, message: &Message) -> Result<()> { pub async fn handle(ctx: &Context, message: &Message) -> Result<()> {
if !ETA_REGEX.is_match(&message.content) { if !ETA_REGEX.is_match(&message.content) {
return Ok(()); return Ok(());
@ -14,7 +32,9 @@ pub async fn handle(ctx: &Context, message: &Message) -> Result<()> {
let response = format!( let response = format!(
"{} <:pofat:1031701005559144458>", "{} <:pofat:1031701005559144458>",
utils::random_choice(consts::ETA_MESSAGES)? ETA_MESSAGES
.choose(&mut rand::thread_rng())
.unwrap_or(&"sometime")
); );
message.reply(ctx, response).await?; message.reply(ctx, response).await?;

View file

@ -1,18 +1,3 @@
use color_eyre::eyre::{eyre, Result};
use rand::seq::SliceRandom;
mod resolve_message; mod resolve_message;
pub use resolve_message::resolve as resolve_message; pub use resolve_message::resolve as resolve_message;
/*
* chooses a random element from an array
*/
pub fn random_choice<const N: usize>(arr: [&str; N]) -> Result<String> {
let mut rng = rand::thread_rng();
let resp = arr
.choose(&mut rng)
.ok_or_else(|| eyre!("Couldn't choose random object from array:\n{arr:#?}!"))?;
Ok((*resp).to_string())
}