From 1da36628bcb5ef47162ab41ef084c4828be83c7c Mon Sep 17 00:00:00 2001 From: Eoghan Conlon Date: Wed, 25 Dec 2024 00:43:17 +0000 Subject: [PATCH] Feat. Bot initialisation code along with message handling code --- src/main.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 04884c1..fda6a3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,29 @@ use dotenv::dotenv; -fn main() { + +use serenity::async_trait; +use serenity::model::channel::Message; +use serenity::prelude::*; + +struct Handler; + +#[async_trait] +impl EventHandler for Handler { + async fn message(&self, ctx: Context, new_message: Message) { + if new_message.content == "!ping" { + if let Err(why) = new_message.channel_id.say(&ctx.http, "Pong!").await { + println!("Error sending message: {why:?}"); + } + } + } +} + +#[tokio::main] +async fn main() { dotenv().ok(); let token = std::env::var("TOKEN").expect("TOKEN must be set"); + let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::DIRECT_MESSAGES | GatewayIntents::MESSAGE_CONTENT; + let mut client = Client::builder(&token, intents).event_handler(Handler).await.expect("Error creating bot."); + if let Err(why) = client.start().await { + println!("Client error: {why:?}"); + } }