Skip to content

Commit

Permalink
Fixed bug where trigger had to be typed twice
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill13579 committed Apr 27, 2020
1 parent 54a486e commit 298902d
Showing 1 changed file with 43 additions and 34 deletions.
77 changes: 43 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl EventHandler for Handler {
let states = self.states.clone();
let db = self.db.clone();
let statusMap = self.statusMap.clone();
let mut channel_id = msg.channel_id;

let guard = db.lock().unwrap();
let original_user = format!("u{}", msg.author.id.0.to_string());
Expand All @@ -118,49 +119,57 @@ impl EventHandler for Handler {
let var_val = regex::Regex::new("<(.*?)>").unwrap();

thread::spawn(move || {
let mut msg = rx.recv().unwrap_or_else(|e| {
process::exit(1);
});
loop {
match &states[i] {
State::Trigger(req, t) => {
msg = rx.recv().unwrap_or_else(|e| {
let mut msg = rx.recv().unwrap_or_else(|e| {
process::exit(1);
});
let mut required = vec![];
for m in var_def.captures_iter(&req.original()) {
required.push(m.get(1).unwrap().as_str().to_owned());
}
if required.len() > 0 {
while msg.author.id.0 == ctx.cache.read().user.id.0 {
msg = rx.recv().unwrap_or_else(|e| {
process::exit(1);
});
}
let mut new_pat = Regex::new(&var_def.replace_all(&regex::escape(&req.original()), "(.*?)").into_owned().to_string());
let new_pat = match new_pat {
Ok(p) => p,
Err(e) => {
return;
let mut success = false;
while !success {
while msg.author.id.0 == ctx.cache.read().user.id.0 {
msg = rx.recv().unwrap_or_else(|e| {
process::exit(1);
});
}

let mut new_pat = Regex::new(&var_def.replace_all(&regex::escape(&req.original()), "(.*?)").into_owned().to_string());
let new_pat = match new_pat {
Ok(p) => p,
Err(e) => {
return;
}
};

let guard = db.lock().unwrap();
let mut number_of_matches = 0;
for (c, n) in new_pat.captures_iter(&msg.content).zip(required.iter()) {
number_of_matches += 1;
let c = c.get(1).unwrap().as_str().to_owned();
guard.execute(
&format!(
"INSERT OR IGNORE INTO {} VALUES (?, ?)"
, original_user),
params![n, &c],
).unwrap();
guard.execute(
&format!(
"UPDATE {} SET val=? WHERE id=?"
, original_user),
params![&c, n],
).unwrap();
}
std::mem::drop(guard);

if required.len() == number_of_matches {
success = true;
}
};
let guard = db.lock().unwrap();
for (c, n) in new_pat.captures_iter(&msg.content).zip(required.iter()) {
let c = c.get(1).unwrap().as_str().to_owned();
guard.execute(
&format!(
"INSERT OR IGNORE INTO {} VALUES (?, ?)"
, original_user),
params![n, &c],
).unwrap();
guard.execute(
&format!(
"UPDATE {} SET val=? WHERE id=?"
, original_user),
params![&c, n],
).unwrap();
}
std::mem::drop(guard);
} else {
while (msg.author.id.0 == ctx.cache.read().user.id.0) || !req.check(&msg.content) {
msg = rx.recv().unwrap_or_else(|e| {
Expand Down Expand Up @@ -188,13 +197,13 @@ impl EventHandler for Handler {
replace_with
});
std::mem::drop(guard);
if let Err(why) = msg.channel_id.say(&ctx.http, &s) {
if let Err(why) = channel_id.say(&ctx.http, &s) {
eprintln!("Error sending reply: {:?}", why);
}
timeout = *t;
},
State::DisplayImage(u, t) => {
let res = msg.channel_id.send_message(&ctx.http, |m| {
let res = channel_id.send_message(&ctx.http, |m| {
m.add_file(AttachmentType::Path(Path::new(u)));
m
});
Expand All @@ -213,7 +222,7 @@ impl EventHandler for Handler {
},
State::End => {
let entry = &mut statusMap.lock().unwrap();
let entry = entry.entry(msg.channel_id.0).or_insert(None);
let entry = entry.entry(channel_id.0).or_insert(None);
*entry = None;
break;
}
Expand Down

0 comments on commit 298902d

Please sign in to comment.