diff options
| author | Max Audron <audron@cocaine.farm> | 2021-10-16 13:49:23 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2021-10-16 13:53:22 +0200 |
| commit | 7cbe1e70e440c29375425182e661ea9ff7bd6300 (patch) | |
| tree | de36d71d9167eed333f9a216f2d1588941b528c0 /src/hooks | |
| parent | move bot initialization out of macro (diff) | |
rework configuration to allow extensibility by hooksconfig-rework
Diffstat (limited to '')
| -rw-r--r-- | src/hooks/mod.rs | 2 | ||||
| -rw-r--r-- | src/hooks/wolfram_alpha.rs | 38 |
2 files changed, 28 insertions, 12 deletions
diff --git a/src/hooks/mod.rs b/src/hooks/mod.rs index cdf3787..86d5697 100644 --- a/src/hooks/mod.rs +++ b/src/hooks/mod.rs @@ -36,7 +36,7 @@ pub fn sasl(bot: &crate::Bot, msg: Message) -> Result<()> { if text == "+" { let creds = Credentials::default() .with_username(bot.config.clone().user.username) - .with_password(bot.config.clone().user.password); + .with_password(bot.config.clone().user.password.unwrap()); let mut mechanism = Plain::from_credentials(creds)?; diff --git a/src/hooks/wolfram_alpha.rs b/src/hooks/wolfram_alpha.rs index 3faddbf..0fceb0b 100644 --- a/src/hooks/wolfram_alpha.rs +++ b/src/hooks/wolfram_alpha.rs @@ -3,12 +3,39 @@ use crate::util::{ web::{quote_plus, IsgdUrlShortener, UrlShortener}, }; use anyhow::{bail, Context, Error, Result}; +use figment::providers::Env; use futures::join; use irc::client::prelude::*; use macros::privmsg; use reqwest::{get, Url}; use serde::{Deserialize, Serialize}; +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct WolframAlpha { + wa_api_key: String, +} + +impl WolframAlpha { + pub fn new(bot: &crate::Bot) -> Result<WolframAlpha> { + bot.figment + .clone() + .merge(Env::prefixed("CATINATOR_")) + .extract() + .context("failed to extract wolfram alpha config") + } + + pub async fn wa(&self, bot: &crate::Bot, msg: Message) -> Result<()> { + privmsg!(msg, { + let content = get_input_query(text)?; + bot.send_privmsg( + msg.response_target() + .context("failed to get response target")?, + &wa_query(&content, Some(&self.wa_api_key), None).await?, + )?; + }) + } +} + #[derive(Serialize, Deserialize, Debug)] struct WaResponse { queryresult: QueryResult, @@ -147,17 +174,6 @@ fn get_input_query(text: &str) -> Result<String, Error> { Ok(content.to_string()) } -pub async fn wa(bot: &crate::Bot, msg: Message) -> Result<()> { - privmsg!(msg, { - let content = get_input_query(text)?; - bot.send_privmsg( - msg.response_target() - .context("failed to get response target")?, - &wa_query(&content, Some(&bot.config.settings.wa_api_key), None).await?, - )?; - }) -} - #[cfg(test)] mod tests { |
