dummy pingpong bot

This commit is contained in:
Ash Vollmer 2026-08-03 00:55:36 +02:00
parent 4e82d59ef4
commit f997bed2e2

View file

@ -1,25 +1,109 @@
mod models;
use std::env; use std::env;
use dotenv::dotenv; use dotenv::dotenv;
use models::Notice;
use serenity::{ use serenity::{
all::{Context, EventHandler, GatewayIntents, Message, GATEWAY_VERSION}, all::{
Colour, Context, CreateAttachment, CreateEmbed, CreateEmbedFooter, CreateMessage,
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) {
if msg.content == "!ping" { let new = Notice {
println!("found PING!"); affected_lines: vec!(String::from("10")),
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await { 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: 12,
to: 30,
last_modified: String::from("2026-07-24T13:37:00Z"),
};
if msg.content == "!hello" {
// The create message builder allows you to easily create embeds and messages using a
// builder syntax.
// This example will create a message that says "Hello, World!", with an embed that has
// a title, description, an image, three fields, and a footer.
let footer = CreateEmbedFooter::new("").text(format!(
"Betroffene Linien: {}",
new.affected_lines
.iter()
.fold(String::new(), |acc, a| acc + &format!("{}", a.to_string()))
));
let timestamp: Timestamp = new.last_modified.parse().expect("Invalid timestamp!");
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")
// .description("This is a description")
// .image("attachment://ferris_eyes.png")
// .fields(vec![
// ("This is the first field", "This is a field body", true),
// ("This is the second field", "Both fields are inline", true),
// ])
// .field(
// "This is the third field",
// "This is not an inline field",
// false,
// )
// .footer(footer)
// // Add a timestamp for the current time
// // This also accepts a rfc3339 Timestamp
// .timestamp(Timestamp::now());
let builder = CreateMessage::new().content("").embed(embed);
let msg = msg.channel_id.send_message(&ctx.http, builder).await;
if let Err(why) = msg {
println!("Error sending message: {why:?}"); println!("Error sending message: {why:?}");
} }
} }
} }
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
} }
// #[async_trait]
// impl EventHandler for Handler {
// async fn message(&self, ctx: Context, msg: Message) {
// if msg.content == "!ping" {
// println!("found PING!");
// if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await {
// println!("Error sending message: {why:?}");
// }
// }
// }
// }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
println!("Hello, world!"); println!("Hello, world!");