blob: c75601f5b82699364032ba65c6c71c07fba7244c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
use crate::consts::*;
use bottomify::bottom::{decode_string, encode_string};
use include_dir::{include_dir, Dir};
use rand::seq::SliceRandom;
use std::collections::HashMap;
use std::vec;
const CHAR_LIMIT: usize = 2000;
const FILES: Dir = include_dir!("src/include");
/*
* chooses a random element from an array
*/
async fn random_choice<const N: usize>(arr: [&str; N]) -> String {
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 async fn get_random_response() -> String {
random_choice(RESPONSES).await
}
pub async fn get_random_lore() -> String {
random_choice(LORE).await
}
/*
* splits a message into multiple parts so that
* it can fit discord's character limit
*/
fn split_msg(msg: &String) -> Vec<String> {
if msg.len() > CHAR_LIMIT {
let split = msg[CHAR_LIMIT..].to_string();
let add = split_msg(&split);
let mut ret = vec![msg[..CHAR_LIMIT].to_string()];
for v in add {
ret.push(v);
}
return ret;
}
vec![msg.to_string()]
}
/*
* gets a random copypasta from include/
*/
pub async fn get_copypasta(name: &str) -> Vec<String> {
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();
// refer to files by their name w/o extension
files.insert(name, contents);
}
if files.contains_key(&name) {
let reply = files[name];
// split message if it's too big
if reply.len() > CHAR_LIMIT {
return split_msg(&reply.to_string());
}
return vec![reply.to_string()];
}
let err = format!("couldn't find {:?} in files", name);
vec![err]
}
/*
* encodes a message into bottom
*/
pub async fn bottom_encode(msg: &str) -> String {
encode_string(&msg)
}
/*
* decodes a bottom string into english
*/
pub async 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()
}
}
}
|