From 64c2d46a1333c1fc0e6b79c58508632e69e667ec Mon Sep 17 00:00:00 2001 From: Eoghan Conlon Date: Tue, 24 Dec 2024 23:52:37 +0000 Subject: [PATCH 1/4] Feat. Adding dotenv to the project and adding the discord token via .env file --- .gitignore | 3 ++- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 7 +++++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 40d9aca..5e101cc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -/.idea \ No newline at end of file +/.idea +.env \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index f56dce9..da8b88d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -220,6 +220,7 @@ checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" name = "count_bot" version = "0.1.0" dependencies = [ + "dotenv", "serenity", "tokio", ] @@ -318,6 +319,12 @@ dependencies = [ "syn 2.0.91", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "encoding_rs" version = "0.8.35" diff --git a/Cargo.toml b/Cargo.toml index 916ccd8..45e4a89 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] +dotenv = "0.15.0" serenity = "0.12.4" tokio = { version = "1.42.0", features = ["full"] } diff --git a/src/main.rs b/src/main.rs index e7a11a9..c906f16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ -fn main() { - println!("Hello, world!"); +use dotenv::dotenv; +#[tokio::main] +async fn main() { + dotenv().ok(); + let token = std::env::var("TOKEN").expect("TOKEN must be set"); } -- 2.47.0 From 7cfd73cd9526c6ddc4bf54628b87bb86630c45a7 Mon Sep 17 00:00:00 2001 From: Eoghan Conlon Date: Wed, 25 Dec 2024 00:13:29 +0000 Subject: [PATCH 2/4] Fix. Removing tokio from the project as serenity already has async functionality --- Cargo.lock | 12 ------------ Cargo.toml | 1 - src/main.rs | 3 +-- 3 files changed, 1 insertion(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index da8b88d..4eb8473 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -222,7 +222,6 @@ version = "0.1.0" dependencies = [ "dotenv", "serenity", - "tokio", ] [[package]] @@ -1365,15 +1364,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" -[[package]] -name = "signal-hook-registry" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" -dependencies = [ - "libc", -] - [[package]] name = "skeptic" version = "0.13.7" @@ -1588,9 +1578,7 @@ dependencies = [ "bytes", "libc", "mio", - "parking_lot", "pin-project-lite", - "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.52.0", diff --git a/Cargo.toml b/Cargo.toml index 45e4a89..d121ea4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,3 @@ edition = "2021" [dependencies] dotenv = "0.15.0" serenity = "0.12.4" -tokio = { version = "1.42.0", features = ["full"] } diff --git a/src/main.rs b/src/main.rs index c906f16..04884c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ use dotenv::dotenv; -#[tokio::main] -async fn main() { +fn main() { dotenv().ok(); let token = std::env::var("TOKEN").expect("TOKEN must be set"); } -- 2.47.0 From 7375fac2f97370431999b204602d65253ea06232 Mon Sep 17 00:00:00 2001 From: Eoghan Conlon Date: Wed, 25 Dec 2024 00:15:59 +0000 Subject: [PATCH 3/4] Fix. Readding tokio based on the serenity documentation --- Cargo.lock | 1 + Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 4eb8473..b830943 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -222,6 +222,7 @@ version = "0.1.0" dependencies = [ "dotenv", "serenity", + "tokio", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index d121ea4..cadc310 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" [dependencies] dotenv = "0.15.0" serenity = "0.12.4" +tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] } -- 2.47.0 From 1da36628bcb5ef47162ab41ef084c4828be83c7c Mon Sep 17 00:00:00 2001 From: Eoghan Conlon Date: Wed, 25 Dec 2024 00:43:17 +0000 Subject: [PATCH 4/4] 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:?}"); + } } -- 2.47.0