Compare commits
No commits in common. "bb745c9595f502c5ecc86a2faeee26a4864056d2" and "2878cbc5956be19c9d04312de6c657f11351821e" have entirely different histories.
bb745c9595
...
2878cbc595
2 changed files with 39 additions and 83 deletions
39
src/main.rs
39
src/main.rs
|
|
@ -6,25 +6,27 @@ use dotenv::dotenv;
|
||||||
use models::Notice;
|
use models::Notice;
|
||||||
use serenity::{
|
use serenity::{
|
||||||
all::{
|
all::{
|
||||||
Colour, Context, CreateEmbed, CreateEmbedFooter, CreateMessage, EventHandler,
|
Colour, Context, CreateAttachment, CreateEmbed, CreateEmbedFooter, CreateMessage,
|
||||||
GatewayIntents, Message, Ready, Timestamp,
|
EventHandler, GatewayIntents, Message, Ready, Timestamp,
|
||||||
},
|
},
|
||||||
async_trait, Client,
|
async_trait, Client,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::models::get_line_color;
|
||||||
|
|
||||||
struct Handler;
|
struct Handler;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl EventHandler for Handler {
|
impl EventHandler for Handler {
|
||||||
async fn message(&self, ctx: Context, msg: Message) {
|
async fn message(&self, ctx: Context, msg: Message) {
|
||||||
let mut new = Notice {
|
let new = Notice {
|
||||||
affected_lines: vec!(String::from("10")),
|
affected_lines: vec!(String::from("10")),
|
||||||
dest_1: String::from("Hannover/Wettbergen"),
|
dest_1: String::from("Hannover/Wettbergen"),
|
||||||
dest_2: String::from("Hannover/Misburg"),
|
dest_2: String::from("Hannover/Misburg"),
|
||||||
headline: String::from("Linie 7 fährt erst bzw. bis Haltestelle In den Sieben Stücken"),
|
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."),
|
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,
|
from: 12,
|
||||||
to: None,
|
to: 30,
|
||||||
last_modified: String::from("2026-07-24T13:37:00Z"),
|
last_modified: String::from("2026-07-24T13:37:00Z"),
|
||||||
};
|
};
|
||||||
if msg.content == "!hello" {
|
if msg.content == "!hello" {
|
||||||
|
|
@ -40,37 +42,24 @@ impl EventHandler for Handler {
|
||||||
));
|
));
|
||||||
|
|
||||||
let timestamp: Timestamp = new.last_modified.parse().expect("Invalid timestamp!");
|
let timestamp: Timestamp = new.last_modified.parse().expect("Invalid timestamp!");
|
||||||
let color = new.get_line_color();
|
let color =
|
||||||
new.get_validity_period();
|
get_line_color(new.affected_lines.first().expect("No single line affected"));
|
||||||
let embed = if new.from.is_none() || new.to.is_none() {
|
|
||||||
CreateEmbed::new()
|
let embed = 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)
|
.title(new.headline)
|
||||||
.description(new.detail)
|
.description(new.detail)
|
||||||
.color(Colour::from_rgb(color.0, color.1, color.2))
|
.color(Colour::from_rgb(color.0, color.1, color.2))
|
||||||
.timestamp(timestamp)
|
.timestamp(timestamp)
|
||||||
.fields(vec![
|
.fields(vec![
|
||||||
("Start", new.from.unwrap().to_string(), true),
|
("Start", new.from.to_string(), true),
|
||||||
("Ende", new.to.unwrap().to_string(), true),
|
("Ende", new.to.to_string(), true),
|
||||||
])
|
])
|
||||||
.field(
|
.field(
|
||||||
"Fahrtrichtungen",
|
"Fahrtrichtungen",
|
||||||
format!("{} & {}", new.dest_1, new.dest_2),
|
format!("{} & {}", new.dest_1, new.dest_2),
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
.footer(footer)
|
.footer(footer);
|
||||||
};
|
|
||||||
|
|
||||||
// let embed = CreateEmbed::new()
|
// let embed = CreateEmbed::new()
|
||||||
// .title("This is a title")
|
// .title("This is a title")
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,16 @@
|
||||||
use chrono::NaiveDateTime;
|
|
||||||
use regex::Regex;
|
|
||||||
|
|
||||||
pub struct Notice {
|
pub struct Notice {
|
||||||
pub affected_lines: Vec<String>,
|
pub affected_lines: Vec<String>,
|
||||||
pub dest_1: String,
|
pub dest_1: String,
|
||||||
pub dest_2: String,
|
pub dest_2: String,
|
||||||
pub headline: String,
|
pub headline: String,
|
||||||
pub detail: String,
|
pub detail: String,
|
||||||
pub from: Option<NaiveDateTime>,
|
pub from: u32,
|
||||||
pub to: Option<NaiveDateTime>,
|
pub to: u32,
|
||||||
pub last_modified: String,
|
pub last_modified: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Notice {
|
pub fn get_line_color(line_number: &str) -> (u8, u8, u8) {
|
||||||
pub fn get_line_color(&self) -> (u8, u8, u8) {
|
match line_number {
|
||||||
match self
|
|
||||||
.affected_lines
|
|
||||||
.first()
|
|
||||||
.expect("Does not affect any line")
|
|
||||||
.as_str()
|
|
||||||
{
|
|
||||||
// Rot: Linien 1, 2, 8
|
// Rot: Linien 1, 2, 8
|
||||||
"1" | "2" | "8" => (238, 66, 36),
|
"1" | "2" | "8" => (238, 66, 36),
|
||||||
|
|
||||||
|
|
@ -36,27 +27,3 @@ impl Notice {
|
||||||
_ => (55, 101, 174),
|
_ => (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.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue