This commit is contained in:
lemonsh 2022-07-28 13:21:17 +02:00
parent e3ac4e7b23
commit 19191bf8f8
5 changed files with 31 additions and 17 deletions

View file

@ -3,7 +3,7 @@ use crate::ExecutorConnection;
use async_trait::async_trait;
use fancy_regex::{Captures, Regex};
use std::collections::HashMap;
use tokio::sync::{Mutex, mpsc};
use tokio::sync::{mpsc, Mutex};
fn dissect<'a>(prefixes: &[String], str: &'a str) -> Option<(&'a str, Option<&'a str>)> {
for prefix in prefixes {
@ -12,7 +12,7 @@ fn dissect<'a>(prefixes: &[String], str: &'a str) -> Option<(&'a str, Option<&'a
Some((&str[..o], Some(&str[o + 1..])))
} else {
Some((str, None))
}
};
}
}
None
@ -108,7 +108,13 @@ impl<SF: Fn(String, String) -> anyhow::Result<()>> Bot<SF> {
Ok(())
}
pub async fn handle_message(&self, origin: String, author: String, content: String, _cancel_handle: mpsc::Sender<()>) {
pub async fn handle_message(
&self,
origin: String,
author: String,
content: String,
_cancel_handle: mpsc::Sender<()>,
) {
if let Err(e) = self.handle_message_inner(&origin, &author, &content).await {
let _err = (self.sendmsg)(origin.into(), format!("Error: {}", e));
}

View file

@ -1,4 +1,4 @@
use std::{net::SocketAddr, collections::HashMap};
use std::{collections::HashMap, net::SocketAddr};
use serde::Deserialize;
@ -8,7 +8,7 @@ pub struct UberConfig {
pub irc: IrcConfig,
pub spotify: Option<SpotifyConfig>,
pub bot: BotConfig,
pub web: Option<HttpConfig>
pub web: Option<HttpConfig>,
}
#[derive(Deserialize)]
@ -39,5 +39,5 @@ pub struct BotConfig {
#[derive(Deserialize)]
pub struct HttpConfig {
pub listen: SocketAddr,
pub webhooks: HashMap<String, String>
pub webhooks: HashMap<String, String>,
}

View file

@ -99,7 +99,10 @@ impl DbExecutor {
query: String,
limit: usize,
) -> rusqlite::Result<Vec<Quote>> {
let (quotes, oid) = self.yield_quotes_oid("select oid,quote,username from quotes where quote match ? order by oid asc limit ?", params![query, limit])?;
let (quotes, oid) = self.yield_quotes_oid(
"select oid,quote,username from quotes where quote match ? order by oid asc limit ?",
params![query, limit],
)?;
searches.insert(user, (query, oid));
Ok(quotes)
}

View file

@ -3,8 +3,8 @@
use fancy_regex::Regex;
use std::str::FromStr;
use std::sync::Arc;
use std::{thread, process};
use std::{env, fs};
use std::{process, thread};
use crate::bot::Bot;
use crate::commands::eval::Eval;
@ -22,19 +22,19 @@ use irc::client::{Client, ClientStream};
use irc::proto::{ChannelExt, Command, Prefix};
use rspotify::Credentials;
use tokio::select;
use tokio::sync::{broadcast, mpsc};
use tokio::sync::mpsc::unbounded_channel;
use tokio::sync::{broadcast, mpsc};
use tracing::Level;
use crate::config::UberConfig;
use crate::database::{DbExecutor, ExecutorConnection};
mod web;
mod bot;
mod commands;
mod config;
mod database;
mod history;
mod web;
#[cfg(unix)]
async fn terminate_signal() {
@ -112,7 +112,10 @@ async fn main() -> anyhow::Result<()> {
let http_task = cfg.web.map(|http| {
let http_ctx = ctx.subscribe();
let context = HttpContext { cfg: http, sendmsg: sf.clone() };
let context = HttpContext {
cfg: http,
sendmsg: sf.clone(),
};
tokio::spawn(async move {
if let Err(e) = web::run(context, http_ctx).await {
tracing::error!("Fatal error in web service: {}", e);
@ -185,10 +188,10 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}
async fn message_loop<SF>(
mut stream: ClientStream,
bot: Bot<SF>,
) -> anyhow::Result<()> where SF: Fn(String, String) -> anyhow::Result<()> + Send + Sync + 'static {
async fn message_loop<SF>(mut stream: ClientStream, bot: Bot<SF>) -> anyhow::Result<()>
where
SF: Fn(String, String) -> anyhow::Result<()> + Send + Sync + 'static,
{
let bot = Arc::new(bot);
let (cancelled_send, mut cancelled_recv) = mpsc::channel::<()>(1);
while let Some(message) = stream.next().await.transpose()? {
@ -201,7 +204,8 @@ async fn message_loop<SF>(
let bot = bot.clone();
let cancelled_send = cancelled_send.clone();
tokio::spawn(async move {
bot.handle_message(origin, author, content, cancelled_send).await;
bot.handle_message(origin, author, content, cancelled_send)
.await;
});
} else {
tracing::warn!("Couldn't get the author for a message");

View file

@ -1,9 +1,10 @@
use std::{convert::Infallible, sync::Arc};
use hyper::{
body::to_bytes,
header::HeaderValue,
service::{make_service_fn, service_fn},
Body, Request, Response, Server, StatusCode, body::to_bytes,
Body, Request, Response, Server, StatusCode,
};
use tokio::sync::broadcast;