From a4a9353e1c8f902b7d7b3cf74e3e5b129c214330 Mon Sep 17 00:00:00 2001 From: seth Date: Mon, 10 Jul 2023 00:18:36 -0400 Subject: start using poise --- src/utils.rs | 113 +++++++++-------------------------------------------------- 1 file changed, 17 insertions(+), 96 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index c2a2031..8773b14 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,11 +1,9 @@ -use crate::consts::{LORE, RESPONSES}; -use bottomify::bottom::{decode_string, encode_string}; -use include_dir::{include_dir, Dir}; -use rand::seq::SliceRandom; -use std::collections::HashMap; -use std::vec; +use crate::consts::*; +use crate::Error; -const FILES: Dir = include_dir!("src/copypastas"); +use once_cell::sync::Lazy; +use poise::serenity_prelude::GuildId; +use rand::seq::SliceRandom; pub fn parse_snowflake_from_env T>(key: &str, f: F) -> Option { std::env::var(key).ok().and_then(|v| v.parse().map(&f).ok()) @@ -21,23 +19,13 @@ pub fn parse_snowflakes_from_env T>(key: &str, f: F) -> Option< /* * chooses a random element from an array */ -fn random_choice(arr: [&str; N]) -> String { +pub fn random_choice(arr: [&str; N]) -> Result { let mut rng = rand::thread_rng(); - let resp = arr.choose(&mut rng).expect("couldn't choose random value!"); - (*resp).to_string() -} - -/* - * pub functions to get random elements - * from our consts - */ - -pub fn get_random_response() -> String { - random_choice(RESPONSES) -} - -pub fn get_random_lore() -> String { - random_choice(LORE) + if let Some(resp) = arr.choose(&mut rng) { + Ok((*resp).to_string()) + } else { + Err(Into::into("couldn't choose from arr!")) + } } // waiting for `round_char_boundary` to stabilize @@ -54,79 +42,12 @@ pub fn floor_char_boundary(s: &str, index: usize) -> usize { lower_bound + new_index.unwrap() } } -// waiting for `int_roundings` to stabilize -fn div_ceil(a: usize, b: usize) -> usize { - (a + b - 1) / b -} - -/* - * splits a message into multiple parts so that - * it can fit discord's character limit - */ -fn split_msg(mut msg: String) -> Vec { - const CHAR_LIMIT: usize = 2000; - let mut msgs = Vec::with_capacity(div_ceil(msg.len(), CHAR_LIMIT)); - - while msg.len() > CHAR_LIMIT { - msgs.push(msg.split_off(floor_char_boundary(&msg, CHAR_LIMIT))); - } - msgs -} - -/* - * gets a random copypasta from include/ - */ -pub fn get_copypasta(name: &str) -> Vec { - let mut files: HashMap<&str, &str> = HashMap::new(); - - for file in FILES.files() { - let name = file.path().file_stem().unwrap().to_str().unwrap(); - let contents = file.contents_utf8().unwrap(); +pub fn is_guild_allowed(gid: GuildId) -> bool { + static ALLOWED_GUILDS: Lazy> = Lazy::new(|| { + parse_snowflakes_from_env("ALLOWED_GUILDS", GuildId) + .unwrap_or_else(|| vec![TEAWIE_GUILD, GuildId(1091969030694375444)]) + }); - // refer to files by their name w/o extension - files.insert(name, contents); - } - - if files.contains_key(&name) { - let reply = files[name].to_string(); - split_msg(reply) - } else { - vec![format!("couldn't find {name:?} in files")] - } -} - -/* - * encodes a message into bottom - */ -pub fn bottom_encode(msg: &str) -> String { - encode_string(&msg) -} - -/* - * decodes a bottom string into english - */ -pub fn bottom_decode(msg: &str) -> String { - let decoded = decode_string(&msg); - match decoded { - Ok(ret) => ret, - Err(why) => { - println!("couldn't decode {msg:?}! ({why:?})"); - "couldn't decode that! sowwy 🥺".to_string() - } - } -} - -/* - * converts celsius to fahrenheit - */ -pub fn celsius_to_fahrenheit(c: f64) -> f64 { - (c * (9.0 / 5.0)) + 32.0 -} - -/* - * converts fahrenheit to celsius - */ -pub fn fahrenheit_to_celsius(f: f64) -> f64 { - (f - 32.0) * (5.0 / 9.0) + ALLOWED_GUILDS.contains(&gid) } -- cgit v1.2.3