3.1, new APIs

This commit is contained in:
Xyon 2023-08-30 18:44:39 +01:00
parent 57b79e7a5e
commit 88c5a1b1a0
Signed by: xyon
GPG Key ID: DD18155D6B18078D
8 changed files with 129 additions and 14 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "manifold" name = "manifold"
version = "3.0.0" version = "3.1.0"
authors = ["Lucy Bladen <admin@lbladen.uk>"] authors = ["Lucy Bladen <admin@lbladen.uk>"]
edition = "2021" edition = "2021"

26
src/commands/joke.rs Normal file
View File

@ -0,0 +1,26 @@
use crate::error::{ManifoldError, ManifoldResult};
use crate::{ManifoldContext, ManifoldData};
use crate::models::dadjoke::{DadJoke, DadJokeWrapper};
#[poise::command(slash_command, prefix_command, aliases("dad"))]
async fn dad_joke(ctx: ManifoldContext<'_>) -> ManifoldResult<()> {
debug!("Processing tip");
if let Some(tank) = ctx.data().bot_config.services.get("dad_jokes") {
let tip: &DadJoke = &tank.next_or_fill::<DadJoke, DadJokeWrapper>().await?;
debug!("Tip: {:?}", tip);
ctx.send(|f| f.content(format!("{}", tip)).reply(true)).await?;
} else {
error!("No such fuel tank dad_jokes");
ctx.send(|f| f.content("Can't find any dad jokes. Maybe your dad isn't funny.".to_string()).reply(true)).await?;
}
Ok(())
}
pub fn commands() -> [poise::Command<ManifoldData, ManifoldError>; 1] {
[dad_joke()]
}

View File

@ -2,6 +2,8 @@ mod admin;
mod animal; mod animal;
mod convert; mod convert;
mod core; mod core;
mod joke;
mod nasa;
mod weather; mod weather;
use crate::ManifoldCommand; use crate::ManifoldCommand;
@ -11,6 +13,8 @@ pub fn collect_commands(injected: Vec<ManifoldCommand>) -> Vec<ManifoldCommand>
.chain(admin::commands()) .chain(admin::commands())
.chain(animal::commands()) .chain(animal::commands())
.chain(convert::commands()) .chain(convert::commands())
.chain(joke::commands())
.chain(nasa::commands())
.chain(weather::commands()) .chain(weather::commands())
.chain(injected) .chain(injected)
.collect() .collect()

35
src/commands/nasa.rs Normal file
View File

@ -0,0 +1,35 @@
use crate::error::{ManifoldError, ManifoldResult};
use crate::{ManifoldContext, ManifoldData};
use crate::models::nasa::*;
#[poise::command(slash_command, prefix_command, aliases("apod"))]
async fn nasa_apod(ctx: ManifoldContext<'_>) -> ManifoldResult<()> {
debug!("Processing tip");
if let Some(tank) = ctx.data().bot_config.services.get("nasa_apod") {
let photo: &NasaAstroPhoto = &tank.next_or_fill::<NasaAstroPhoto, NasaAstroPhoto>().await?;
debug!("Tip: {:?}", photo);
ctx.send(|f| {
f.content(format!("{}", photo))
.reply(true)
.embed(|e| {
e.title(&photo.title);
e.description(&photo.explanation);
e.image(&photo.hdurl);
e
})
}).await?;
} else {
error!("No such fuel tank nasa_apod");
ctx.send(|f| f.content("The NASA Astronomy Photo of the Day was missing. Try looking up instead.".to_string()).reply(true)).await?;
}
Ok(())
}
pub fn commands() -> [poise::Command<ManifoldData, ManifoldError>; 1] {
[nasa_apod()]
}

29
src/models/dadjoke.rs Normal file
View File

@ -0,0 +1,29 @@
use std::fmt::{Display, Formatter};
use crate::models::fueltank::Pump;
#[derive(Serialize, Deserialize, Debug)]
pub struct DadJoke {
pub id: String,
pub joke: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DadJokeWrapper {
pub results: Vec<DadJoke>
}
impl Display for DadJoke {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.joke)
}
}
impl Pump<DadJoke> for DadJokeWrapper {
fn pump(&mut self) -> Option<DadJoke> {
self.results.pop()
}
fn len(&self) -> usize {
self.results.len()
}
}

View File

@ -4,7 +4,7 @@ use std::path::PathBuf;
use dirs::home_dir; use dirs::home_dir;
use std::io::{SeekFrom, Seek}; use std::io::{SeekFrom, Seek};
use reqwest::Client; use reqwest::Client;
use reqwest::header::CONTENT_TYPE; use reqwest::header::{ACCEPT, CONTENT_TYPE};
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
@ -32,15 +32,6 @@ pub enum TankMode {
} }
impl FuelTank { impl FuelTank {
pub fn new(source_uri: String, cache_name: Option<String>, api_key: Option<String>, cache_mode: TankMode) -> Self {
Self {
source_uri,
cache_name,
api_key,
cache_mode,
}
}
pub async fn next_or_fill<T: Debug, U: Debug>(&self) -> ManifoldResult<T> pub async fn next_or_fill<T: Debug, U: Debug>(&self) -> ManifoldResult<T>
where T: DeserializeOwned, where T: DeserializeOwned,
U: Serialize, U: Serialize,
@ -99,10 +90,13 @@ impl FuelTank {
let result: U; let result: U;
let mut client = Client::new().get(&self.source_uri) let mut client = Client::new().get(&self.source_uri)
.header(CONTENT_TYPE, "application/json"); .header(CONTENT_TYPE, "application/json")
.header(ACCEPT, "application/json");
if let Some(api_key) = &self.api_key { if let Some(api_key) = &self.api_key {
client = client.header("X-API-KEY", api_key); client = client.header("X-API-KEY", api_key);
} }
result = client.send().await?.json().await?; result = client.send().await?.json().await?;
debug!("Result: {:?}", &result); debug!("Result: {:?}", &result);

View File

@ -1,5 +1,7 @@
pub mod animalpics; pub mod animalpics;
pub mod fueltank; pub mod dadjoke;
pub mod frogtip; pub mod frogtip;
pub mod fueltank;
pub mod nasa;
pub mod user; pub mod user;
pub mod weather; pub mod weather;

25
src/models/nasa.rs Normal file
View File

@ -0,0 +1,25 @@
use std::fmt::{Display, Formatter};
use crate::models::fueltank::Pump;
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct NasaAstroPhoto {
pub explanation: String,
pub hdurl: String,
pub title: String
}
impl Display for NasaAstroPhoto {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.hdurl)
}
}
impl Pump<NasaAstroPhoto> for NasaAstroPhoto {
fn pump(&mut self) -> Option<NasaAstroPhoto> {
Some(self.clone())
}
fn len(&self) -> usize {
1
}
}