diff --git a/src/main.rs b/src/main.rs index 3e0868a..259098b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,25 +6,27 @@ use dotenv::dotenv; use models::Notice; use serenity::{ all::{ - Colour, Context, CreateEmbed, CreateEmbedFooter, CreateMessage, EventHandler, - GatewayIntents, Message, Ready, Timestamp, + Colour, Context, CreateAttachment, CreateEmbed, CreateEmbedFooter, CreateMessage, + EventHandler, GatewayIntents, Message, Ready, Timestamp, }, async_trait, Client, }; +use crate::models::get_line_color; + struct Handler; #[async_trait] impl EventHandler for Handler { async fn message(&self, ctx: Context, msg: Message) { - let mut new = Notice { + let new = Notice { affected_lines: vec!(String::from("10")), dest_1: String::from("Hannover/Wettbergen"), dest_2: String::from("Hannover/Misburg"), headline: String::from("Linie 7 fährt erst bzw. bis Haltestelle In den Sieben Stücken"), detail: String::from("Die Stadtbahnlinie 7 fährt während der Baumaßnahme erst ab beziehungsweise bis zur Haltestelle In den Sieben Stücken. Dort besteht die Umsteigemöglichkeit vom bzw. in den Schienenersatzverkehr (SEV) und die Stadtbahnlinien 3 und 13. An der Haltestelle Buchholz/Betriebshof ist kein Zustieg bzw. Ausstieg möglich."), - from: None, - to: None, + from: 12, + to: 30, last_modified: String::from("2026-07-24T13:37:00Z"), }; if msg.content == "!hello" { @@ -40,37 +42,24 @@ impl EventHandler for Handler { )); let timestamp: Timestamp = new.last_modified.parse().expect("Invalid timestamp!"); - let color = new.get_line_color(); - new.get_validity_period(); - let embed = if new.from.is_none() || new.to.is_none() { - CreateEmbed::new() - .title(new.headline) - .description(new.detail) - .color(Colour::from_rgb(color.0, color.1, color.2)) - .timestamp(timestamp) - .field( - "Fahrtrichtungen", - format!("{} & {}", &new.dest_1, &new.dest_2), - false, - ) - .footer(footer) - } else { - CreateEmbed::new() - .title(new.headline) - .description(new.detail) - .color(Colour::from_rgb(color.0, color.1, color.2)) - .timestamp(timestamp) - .fields(vec![ - ("Start", new.from.unwrap().to_string(), true), - ("Ende", new.to.unwrap().to_string(), true), - ]) - .field( - "Fahrtrichtungen", - format!("{} & {}", new.dest_1, new.dest_2), - false, - ) - .footer(footer) - }; + let color = + get_line_color(new.affected_lines.first().expect("No single line affected")); + + let embed = CreateEmbed::new() + .title(new.headline) + .description(new.detail) + .color(Colour::from_rgb(color.0, color.1, color.2)) + .timestamp(timestamp) + .fields(vec![ + ("Start", new.from.to_string(), true), + ("Ende", new.to.to_string(), true), + ]) + .field( + "Fahrtrichtungen", + format!("{} & {}", new.dest_1, new.dest_2), + false, + ) + .footer(footer); // let embed = CreateEmbed::new() // .title("This is a title") diff --git a/src/models.rs b/src/models.rs index a80fcf1..389cfb8 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,62 +1,29 @@ -use chrono::NaiveDateTime; -use regex::Regex; - pub struct Notice { pub affected_lines: Vec, pub dest_1: String, pub dest_2: String, pub headline: String, pub detail: String, - pub from: Option, - pub to: Option, + pub from: u32, + pub to: u32, pub last_modified: String, } -impl Notice { - pub fn get_line_color(&self) -> (u8, u8, u8) { - match self - .affected_lines - .first() - .expect("Does not affect any line") - .as_str() - { - // Rot: Linien 1, 2, 8 - "1" | "2" | "8" => (238, 66, 36), +pub fn get_line_color(line_number: &str) -> (u8, u8, u8) { + match line_number { + // Rot: Linien 1, 2, 8 + "1" | "2" | "8" => (238, 66, 36), - // Gelb (Die besten Linien): Linien 4, 5, 6, 11 - "4" | "5" | "6" | "11" => (248, 166, 28), + // Gelb (Die besten Linien): Linien 4, 5, 6, 11 + "4" | "5" | "6" | "11" => (248, 166, 28), - // Grün: Linien 9, 10, 12, 17 - "10" | "12" | "17" => (110, 191, 69), + // Grün: Linien 9, 10, 12, 17 + "10" | "12" | "17" => (110, 191, 69), - // Blau: Linien 3, 7, 9, 13 - "3" | "7" | "9" | "13" => (0, 114, 188), + // Blau: Linien 3, 7, 9, 13 + "3" | "7" | "9" | "13" => (0, 114, 188), - // Fallback (z.B. für das Standard-Blau aus deiner API) - _ => (55, 101, 174), - } - } - - pub fn get_validity_period(&mut self) { - let re = Regex::new( - r"Gültig ab (\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}) bis (\d{2}\.\d{2}\.\d{4} \d{2}:\d{2})", - ) - .unwrap(); - - if let Some(caps) = re.captures(&self.detail) { - let valid_from = caps.get(1).unwrap().as_str(); - let valid_to = caps.get(1).unwrap().as_str(); - let format = "%d.%m.%Y %H:%M"; - - if let (Ok(start_dt), Ok(end_dt)) = ( - NaiveDateTime::parse_from_str(valid_from, format), - NaiveDateTime::parse_from_str(valid_to, format), - ) { - self.from = Some(start_dt); - self.to = Some(end_dt); - } - } else { - println!("No validity period found in the text."); - } + // Fallback (z.B. für das Standard-Blau aus deiner API) + _ => (55, 101, 174), } }