Feat. Bot initialisation code along with message handling code

This commit is contained in:
Eoghan Conlon 2024-12-25 00:43:17 +00:00
parent 7375fac2f9
commit 1da36628bc

View file

@ -1,5 +1,29 @@
use dotenv::dotenv; 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(); dotenv().ok();
let token = std::env::var("TOKEN").expect("TOKEN must be set"); 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:?}");
}
} }