-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commands: add fun module starting with an xkcd command.
- Loading branch information
1 parent
7ac042b
commit f7bc928
Showing
6 changed files
with
75 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod xkcd; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use poise::command; | ||
use reqwest::StatusCode; | ||
use serde::Deserialize; | ||
use serenity::all::{CreateActionRow, CreateButton, CreateEmbed, CreateEmbedFooter}; | ||
|
||
use crate::{data::ReqwestContainer, Context, Error}; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct XkcdComic { | ||
num: u16, // the numeric ID of the xkcd comic. | ||
alt: String, // the caption of the xkcd comic. | ||
img: String, // the image URL of the xkcd comic. | ||
title: String, // the title of the xkcd comic. | ||
} | ||
|
||
/// Retrieves the latest or a specific comic from xkcd. | ||
#[command(slash_command)] | ||
pub async fn xkcd(ctx: Context<'_>, #[description = "The specific comic no. to retrieve."] number: Option<u16>) -> Result<(), Error> { | ||
let comic = match number { | ||
None => "https://xkcd.com/info.0.json".to_string(), | ||
Some(number) => format!("https://xkcd.com/{number}/info.0.json").to_string(), | ||
}; | ||
|
||
let client = ctx.serenity_context().data.read().await.get::<ReqwestContainer>().cloned().unwrap(); | ||
let request = client.get(comic).send().await?; | ||
if request.status() == StatusCode::NOT_FOUND { | ||
ctx.reply("You did not provide a valid comic id.").await?; | ||
return Ok(()); | ||
} | ||
|
||
let response: XkcdComic = request.json().await?; | ||
let num = response.num; | ||
let page = format!("https://xkcd.com/{num}/"); | ||
let wiki = format!("https://explainxkcd.com/wiki/index.php/{num}"); | ||
|
||
let embed = CreateEmbed::new() | ||
.title(&response.title) | ||
.color(0xfafafa) | ||
.description(&response.alt) | ||
.image(&response.img) | ||
.footer(CreateEmbedFooter::new(format!("xkcd comic no. {num}"))); | ||
|
||
let links = CreateActionRow::Buttons(vec![CreateButton::new_link(page).label("View on xkcd"), CreateButton::new_link(wiki).label("View wiki")]); | ||
ctx.send(poise::CreateReply::default().embed(embed).components(vec![links])).await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod fun; | ||
pub mod utilities; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/// The user agent used for the reqwest client. | ||
pub const REQWEST_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,8 @@ | ||
use aspotify::Client as SpotifyClient; | ||
use reqwest::Client as ReqwestClient; | ||
use serenity::{client::bridge::gateway::ShardManager, prelude::TypeMapKey}; | ||
use std::sync::Arc; | ||
use tokio::sync::Mutex; | ||
use serenity::prelude::TypeMapKey; | ||
|
||
use crate::config::ConfigurationData; | ||
|
||
pub struct ShardManagerContainer; | ||
pub struct ConfigContainer; | ||
pub struct ReqwestContainer; | ||
pub struct SpotifyContainer; | ||
|
||
impl TypeMapKey for ShardManagerContainer { | ||
type Value = Arc<Mutex<ShardManager>>; | ||
} | ||
|
||
impl TypeMapKey for ConfigContainer { | ||
type Value = ConfigurationData; | ||
} | ||
|
||
impl TypeMapKey for ReqwestContainer { | ||
type Value = ReqwestClient; | ||
} | ||
|
||
impl TypeMapKey for SpotifyContainer { | ||
type Value = SpotifyClient; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters