This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Ax333l <main@axelen.xyz>
- Loading branch information
Showing
14 changed files
with
354 additions
and
34 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 |
---|---|---|
@@ -1,4 +1,11 @@ | ||
# The Discord authorization token for the bot, requires the MESSAGE_CONTENT intent | ||
DISCORD_AUTHORIZATION_TOKEN= | ||
# The connection string to the MongoDB database | ||
MONGODB_URI='' | ||
MONGODB_URI='' | ||
|
||
# The api server for the poll command | ||
API_SERVER='' | ||
# The client id for the api | ||
API_CLIENT_ID='' | ||
# The client secret for the api | ||
API_CLIENT_SECRET='' |
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
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,62 @@ | ||
use reqwest::header::HeaderMap; | ||
use reqwest::Client; | ||
use serde::de::DeserializeOwned; | ||
|
||
use super::model::auth::Authentication; | ||
|
||
use super::routing::Endpoint; | ||
|
||
pub struct Api { | ||
pub client: Client, | ||
pub server: reqwest::Url, | ||
pub client_id: String, | ||
pub client_secret: String, | ||
} | ||
|
||
struct RequestInfo<'a> { | ||
headers: Option<HeaderMap>, | ||
route: Endpoint<'a>, | ||
} | ||
|
||
impl Api { | ||
pub fn new(server: reqwest::Url, client_id: String, client_secret: String) -> Self { | ||
let client = Client::builder() | ||
.build() | ||
.expect("Cannot build reqwest::Client"); | ||
|
||
Api { | ||
client, | ||
server, | ||
client_id, | ||
client_secret, | ||
} | ||
} | ||
|
||
async fn fire<T: DeserializeOwned>(&self, request_info: &RequestInfo<'_>) -> Result<T, reqwest::Error> { | ||
let client = &self.client; | ||
let mut req = request_info.route.to_request(&self.server); | ||
|
||
if let Some(headers) = &request_info.headers { | ||
*req.headers_mut() = headers.clone(); | ||
} | ||
|
||
client.execute(req).await?.json::<T>().await | ||
} | ||
|
||
pub async fn authenticate( | ||
&self, | ||
discord_id_hash: &str, | ||
) -> Result<Authentication, reqwest::Error> { | ||
let route = Endpoint::Authenticate { | ||
id: &self.client_id, | ||
secret: &self.client_secret, | ||
discord_id_hash, | ||
}; | ||
self | ||
.fire(&RequestInfo { | ||
headers: None, | ||
route, | ||
}) | ||
.await | ||
} | ||
} |
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,3 @@ | ||
pub mod client; | ||
pub mod model; | ||
mod routing; |
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,6 @@ | ||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize)] | ||
pub struct Authentication { | ||
pub access_token: String, | ||
} |
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 auth; |
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,28 @@ | ||
use reqwest::{Body, Method, Request}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(untagged)] | ||
pub enum Endpoint<'a> { | ||
Authenticate { | ||
id: &'a str, | ||
secret: &'a str, | ||
discord_id_hash: &'a str, | ||
}, | ||
} | ||
|
||
macro_rules! route { | ||
($self:ident, $server:ident, $endpoint:literal, $method:ident) => {{ | ||
let mut req = Request::new(Method::$method, $server.join($endpoint).unwrap()); | ||
*req.body_mut() = Some(Body::from(serde_json::to_vec($self).unwrap())); | ||
req | ||
}}; | ||
} | ||
|
||
impl Endpoint<'_> { | ||
pub fn to_request(&self, server: &reqwest::Url) -> Request { | ||
match self { | ||
Self::Authenticate { .. } => route!(self, server, "/auth/", POST), | ||
} | ||
} | ||
} |
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
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
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,53 @@ | ||
use chrono::{Duration, Utc}; | ||
use poise::serenity_prelude::{ | ||
ComponentType, | ||
MessageComponentInteraction, | ||
MessageComponentInteractionData, | ||
}; | ||
|
||
use super::*; | ||
use crate::utils; | ||
pub async fn interaction_create( | ||
ctx: &serenity::Context, | ||
interaction: &serenity::Interaction, | ||
) -> Result<(), crate::serenity::SerenityError> { | ||
if let serenity::Interaction::MessageComponent(MessageComponentInteraction { | ||
data: | ||
MessageComponentInteractionData { | ||
component_type: ComponentType::Button, | ||
custom_id, | ||
.. | ||
}, | ||
.. | ||
}) = interaction | ||
{ | ||
if custom_id.starts_with("poll") { | ||
handle_poll(ctx, interaction, custom_id).await? | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn handle_poll( | ||
ctx: &serenity::Context, | ||
interaction: &serenity::Interaction, | ||
custom_id: &str, | ||
) -> Result<(), crate::serenity::SerenityError> { | ||
fn parse<T>(str: &str) -> T | ||
where | ||
<T as std::str::FromStr>::Err: std::fmt::Debug, | ||
T: std::str::FromStr, | ||
{ | ||
str.parse::<T>().unwrap() | ||
} | ||
|
||
let poll: Vec<_> = custom_id.split(':').collect::<Vec<_>>(); | ||
|
||
let poll_id = parse::<u64>(poll[1]); | ||
let min_age = parse::<i64>(poll[2]); | ||
|
||
let min_join_date = serenity::Timestamp::from(Utc::now() - Duration::days(min_age)); | ||
|
||
utils::poll::handle_poll(ctx, interaction, poll_id, min_join_date).await | ||
} |
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
Oops, something went wrong.