Fix minor parsing issues

This commit is contained in:
lemonsh 2022-07-17 20:20:13 +02:00
parent 329f6d6dae
commit 2a0e805719
3 changed files with 11 additions and 10 deletions

View file

@ -6,7 +6,7 @@ use std::collections::HashMap;
use tokio::sync::Mutex; use tokio::sync::Mutex;
fn dissect<'a>(prefix: &str, str: &'a str) -> Option<(&'a str, Option<&'a str>)> { fn dissect<'a>(prefix: &str, str: &'a str) -> Option<(&'a str, Option<&'a str>)> {
let str = str.trim().strip_prefix(prefix)?; let str = str.strip_prefix(prefix)?;
if let Some(o) = str.find(' ') { if let Some(o) = str.find(' ') {
Some((&str[..o], Some(&str[o + 1..]))) Some((&str[..o], Some(&str[o + 1..])))
} else { } else {
@ -71,6 +71,7 @@ impl<SF: Fn(String, String) -> anyhow::Result<()>> Bot<SF> {
author: &str, author: &str,
content: &str, content: &str,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let content = content.trim();
if let Some((command, remainder)) = dissect(&self.prefix, content) { if let Some((command, remainder)) = dissect(&self.prefix, content) {
if let Some(handler) = self.commands.get(command) { if let Some(handler) = self.commands.get(command) {
let msg = Context { let msg = Context {

View file

@ -7,7 +7,10 @@ pub struct LastMsg;
impl Command for LastMsg { impl Command for LastMsg {
async fn execute(&mut self, msg: Context<'_>) -> anyhow::Result<String> { async fn execute(&mut self, msg: Context<'_>) -> anyhow::Result<String> {
let nick = msg.content.unwrap_or(msg.author); let nick = msg.content.unwrap_or(msg.author);
let lastmsg = msg.history.read().await; Ok(format!(
Ok(format!("{}: {:?}", nick, lastmsg.get(nick))) "{}: {:?}",
nick,
msg.history.last_msgs(nick, usize::MAX).await
))
} }
} }

View file

@ -26,19 +26,16 @@ impl Command for Grab {
if author == msg.author { if author == msg.author {
return Ok("You can't grab yourself.".into()); return Ok("You can't grab yourself.".into());
} }
let message = msg let messages = msg.history.last_msgs(author, count).await;
.history if let Some(messages) = messages {
.last_msgs(author, count) let message = messages.join(" | ");
.await
.map(|v| v.join(" | "));
if let Some(message) = message {
msg.db msg.db
.add_quote(Quote { .add_quote(Quote {
author: author.into(), author: author.into(),
quote: message, quote: message,
}) })
.await?; .await?;
Ok("Quote added ({} messages).".into()) Ok(format!("Quote added ({} messages).", messages.len()))
} else { } else {
Ok("No previous messages to grab.".into()) Ok("No previous messages to grab.".into())
} }