Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
updated youtube videos
Browse files Browse the repository at this point in the history
  • Loading branch information
y3ll0wlife committed Sep 15, 2023
1 parent e160954 commit de89f2b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
22 changes: 14 additions & 8 deletions src/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serenity::prelude::SerenityError;
use serenity::{http::Http, model::channel::Embed, model::webhook::Webhook, utils::Colour};
use std::env;

use crate::models::{Changelog, Client};
use crate::models::{AssetType, Changelog, Client};

pub async fn send_message(changelog: &Changelog, client: &Client) -> Result<(), SerenityError> {
let http = Http::new("token");
Expand All @@ -12,21 +12,27 @@ pub async fn send_message(changelog: &Changelog, client: &Client) -> Result<(),
let fail_safe_asset = String::from("https://discord.com/");
let asset = changelog.asset.as_ref().unwrap_or_else(|| &fail_safe_asset);

let image = match &changelog.asset_type {
AssetType::Image | AssetType::Unknown => asset.to_string(),
AssetType::YouTube => format!("https://i.ytimg.com/vi/{}/maxresdefault.jpg", &asset),
};

let url = match &changelog.asset_type {
AssetType::Image | AssetType::Unknown => asset.to_string(),
AssetType::YouTube => format!("https://youtu.be/{}", &asset),
};

let changelog_embed = Embed::fake(|e| {
e.colour(Colour::from_rgb(135, 134, 255))
.field("Changelog Id", &changelog.changelog_id, true)
.field("Entry Id", &changelog.entry_id, true)
.field("Asset Type", changelog.asset_type, true)
.field("Asset Type", changelog.asset_type.clone() as u8, true)
.description(format!(
"{}\n\n**__Asset Link__**\n{}",
&changelog.content, &asset
&changelog.content, url,
))
.footer(|f| f.text(format!("{} • {}", changelog.date, changelog.locale)))
.image(if changelog.asset_type == 1 {
&asset
} else {
""
})
.image(image)
.title(format!("Changelog on {:#?}", client))
});

Expand Down
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rusqlite::{Connection, Result};

use crate::{
discord::send_message,
models::{Changelog, ChangelogConfig, ChangelogDB},
models::{Changelog, ChangelogConfig, ChangelogDB, ChangelogReqwest},
utils::which_client,
};

Expand Down Expand Up @@ -69,11 +69,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
"https://cdn.discordapp.com/changelogs/{}/{}/en-US.json",
client_id, snowflake
);
let changelog = reqwest::get(&changelog_url)
let changelog_req = reqwest::get(&changelog_url)
.await?
.json::<Changelog>()
.json::<ChangelogReqwest>()
.await?;

let changelog = Changelog::convert_from_reqwest(changelog_req);

println!("[{:#?}] Found changelog: {}", client, changelog.date);
send_message(&changelog, &client).await?;

Expand All @@ -85,7 +87,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
&changelog.locale,
&changelog.date,
&changelog.asset,
&changelog.asset_type,
changelog.asset_type as u8,
&changelog.content,
),
)?;
Expand Down
43 changes: 41 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ pub enum Client {
Unknown = 99999,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AssetType {
YouTube = 0,
Image = 1,
Unknown = 9999,
}

#[derive(Debug, Clone)]
pub struct ChangelogDB {
pub changelog_id: String,
Expand All @@ -18,7 +25,6 @@ pub struct ChangelogDB {
pub asset_type: usize,
pub content: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Changelog {
#[serde(deserialize_with = "deserialize_string_from_number")]
Expand All @@ -28,7 +34,40 @@ pub struct Changelog {
pub locale: String,
pub date: String,
pub asset: Option<String>,
pub asset_type: usize,
pub asset_type: AssetType,
pub content: String,
}

impl Changelog {
pub fn convert_from_reqwest(changelog_reqwest: ChangelogReqwest) -> Changelog {
let asset_type = match changelog_reqwest.asset_type {
0 => AssetType::YouTube,
1 => AssetType::Image,
_ => AssetType::Unknown,
};

Changelog {
asset: changelog_reqwest.asset,
changelog_id: changelog_reqwest.changelog_id,
content: changelog_reqwest.content,
date: changelog_reqwest.date,
entry_id: changelog_reqwest.entry_id,
locale: changelog_reqwest.locale,
asset_type,
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangelogReqwest {
#[serde(deserialize_with = "deserialize_string_from_number")]
pub changelog_id: String,
#[serde(deserialize_with = "deserialize_string_from_number")]
pub entry_id: String,
pub locale: String,
pub date: String,
pub asset: Option<String>,
pub asset_type: u8,
pub content: String,
}

Expand Down

0 comments on commit de89f2b

Please sign in to comment.