Compare commits

...

2 Commits

Author SHA1 Message Date
16bb6a48a2
More efficient method for KeywordSection::load() 2023-04-19 20:53:16 -04:00
516c2529e2
Add readme.md 2023-04-19 20:33:17 -04:00
3 changed files with 15 additions and 21 deletions

8
README.md Normal file
View File

@ -0,0 +1,8 @@
# Scam Police
A rust bot for Matrix designed to call out scams
# Build
`cargo build --release`
# Run
On first run, you will need to login. To do this, run `cargo run --release [MXID]` where MXID is the Matrix ID of the user the bot will run on. The only currently supported login flow is Password. (SSO planned)

View File

@ -122,7 +122,7 @@ impl Config {
.as_object()
.unwrap()
.into_iter()
.map(|a| (a.0.to_owned(), KeywordSection::load(a.1)))
.map(|a| (a.0.to_owned(), KeywordSection::load(a.1).unwrap()))
.collect();
Self {

View File

@ -1,7 +1,8 @@
use serde_json::Value;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct KeywordSection {
pub threshold: u64,
pub required: Vec<String>,
@ -9,25 +10,10 @@ pub struct KeywordSection {
}
impl KeywordSection {
pub fn load(json: &Value) -> Self {
let threshold: u64 = json["threshold"].as_u64().unwrap();
let required: Vec<String> = json["requiredKeywords"]
.as_array()
.unwrap()
.into_iter()
.map(|a| a.as_str().unwrap().to_string())
.collect();
let keywords: Vec<String> = json["keywords"]
.as_array()
.unwrap()
.into_iter()
.map(|a| a.as_str().unwrap().to_string())
.collect();
Self {
threshold,
required,
keywords,
pub fn load(json: &Value) -> Option<Self> {
match serde_json::from_value(json.to_owned()) {
Ok(j) => Some(j),
Err(e) => { eprintln!("{e}"); None }
}
}