diff --git a/.env b/.env new file mode 100644 index 0000000..978bef7 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DISCORD_TOKEN=MTIzMDE2NjI2NTM3NzU5MTM5Ng.GVqsyl.ZvP2Kigd5VgP4eITdGqAGYfDw58YeXJYSJg4-A \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e7a11a9..492fde7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,60 @@ -fn main() { - println!("Hello, world!"); +use std::env; + +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, msg: Message) { + if msg.content.starts_with("!") { + let command_with_exclam: &str = msg.content.strip_prefix("!").unwrap(); + + if command_with_exclam == "ping" { + if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await { + println!("Error sending message: {why:?}"); + } + } else { + println!("unknown command {command_with_exclam:?}"); + } + } + if ["hello", "hi", "bonjour", "hola", "halo"] + .iter() + .any(|&s| msg.content.to_lowercase().contains(s)) + { + if let Err(why) = msg.react(ctx, '👋').await { + println!("Error reacting message {why:?}"); + } + } + } + // async fn interaction_create(&self, ctx: Context, interaction: Interaction){ + + // } } + +#[tokio::main] +async fn main() { + // Login with a bot token from the environment + let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment"); + // Set gateway intents, which decides what events the bot will be notified about + let intents = GatewayIntents::GUILD_MESSAGES + | GatewayIntents::DIRECT_MESSAGES + | GatewayIntents::MESSAGE_CONTENT; + + // Create a new instance of the Client, logging in as a bot. + let mut client = Client::builder(&token, intents) + .event_handler(Handler) + .await + .expect("Err creating client"); + + // Start listening for events by starting a single shard + if let Err(why) = client.start().await { + println!("Client error: {why:?}"); + } +} + +// fn main(){ +// println!("helloworldf4a64fra984"); +// }