From 97b1d8e26b508dd79298ea6895a4be95ee586c80 Mon Sep 17 00:00:00 2001 From: Djkato Date: Fri, 7 Jul 2023 12:16:25 +0200 Subject: [PATCH 1/3] rework for anyhow --- Cargo.toml | 3 +- src/main.rs | 88 ++++++++++++++++++++++++++++++++++------------------- 2 files changed, 59 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e35d6c1..1bed005 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,10 +6,11 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.71" cron = "0.12.0" dotenv = "0.15.0" mongodb = "2.6.0" poise = "0.5.5" serenity = { version = "0.11.6", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "http"] } serenity_utils = "0.7.0" -tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] } \ No newline at end of file +tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] } diff --git a/src/main.rs b/src/main.rs index ec20e06..c5698a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,12 @@ use std::sync::Arc; use poise::serenity_prelude::GuildChannel; use serenity::async_trait; +use serenity::http::{self, Http}; use serenity::model::channel::Message; use serenity::model::gateway::Ready; use serenity::model::id::ChannelId; use serenity::prelude::*; use util::security::dotenv_var; -use serenity::http::{self, Http}; mod util; @@ -26,48 +26,74 @@ impl EventHandler for Handler { async fn ready(&self, ctx: Context, ready: Ready) { println!("{} is connected!", ready.user.name); // if (ready.user.name != "MOOver Debug") { - let messages = ["AAAAAAAAAAAAAAAAAAAA", "Henlooo", "Good day y'all!", - "May have crashed...", "MOOOooo", "Heyyyyy!", "I'm baaaaack!", - "Whom'st have summoned the ancient one?"]; - - // } - let opt_token = dotenv_var("TOKEN"); - if opt_token.is_none() { - return; - } + let messages = [ + "AAAAAAAAAAAAAAAAAAAA", + "Henlooo", + "Good day y'all!", + "May have crashed...", + "MOOOooo", + "Heyyyyy!", + "I'm baaaaack!", + "Whom'st have summoned the ancient one?", + ]; - let token = opt_token.unwrap(); + // } let channel_result = ctx.http.get_channel(780439236867653635).await; let channel = channel_result.unwrap(); // let channel = await http.get_channel(780439236867653635); // GuildChannel::say(&self, http, content) // self.message(ctx, new_message) // C = ; - - // const debug_channel = + + // const debug_channel = + } +} +struct MyStruct { + niečo: String, +} +impl MyStruct { + fn add(&mut self) { + self.niečo.push(char::from_digit(2, 2).unwrap()); + } +} +trait Countable { + fn count(&self) -> usize; +} +impl Countable for MyStruct { + fn count(&self) -> usize { + self.niečo.len() + } +} +fn smt(var: Box) { + var.count(); +} +#[tokio::main] +async fn main() -> anyhow::Result<()> { + let mut n = MyStruct { + niečo: "aaa".to_string(), + }; + n.add(); + loop { + //Keeps trying to reconnect, if errors occur print to console and retry + match connect().await { + Ok(r) => return Ok(()), + Err(e) => println!("FAILED TO CONNECT!!! {e}\nRetrying soon..."), + } } } - - -#[tokio::main] -async fn main() { - let opt_token = dotenv_var("TOKEN"); - if opt_token.is_none() { - return; - } - - let token = opt_token.unwrap(); - +async fn connect() -> anyhow::Result<()> { + use anyhow::Context; + let token = dotenv_var("TOKEN").context("No TOKEN in env")?; 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 client"); - if let Err(why) = client.start().await { - println!("Client error: {:?}", why); - } + let mut client = Client::builder(&token, intents) + .event_handler(Handler) + .await + .context("Failed to build client")?; + + client.start().await?; + Ok(()) } From f105e5e74667fcb8d90bbf2ea534c5495b6f39f0 Mon Sep 17 00:00:00 2001 From: Ladislav Hano <524934@mail.muni.cz> Date: Fri, 7 Jul 2023 13:44:07 +0200 Subject: [PATCH 2/3] Ready to merge --- src/main.rs | 43 ++++++------------------------------------- src/sample.rs | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 37 deletions(-) create mode 100644 src/sample.rs diff --git a/src/main.rs b/src/main.rs index c5698a7..9f21376 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,8 +38,9 @@ impl EventHandler for Handler { ]; // } - let channel_result = ctx.http.get_channel(780439236867653635).await; - let channel = channel_result.unwrap(); + + let channel = ctx.http.get_channel(780439236867653635).await.unwrap(); + // let channel = await http.get_channel(780439236867653635); // GuildChannel::say(&self, http, content) // self.message(ctx, new_message) @@ -48,52 +49,20 @@ impl EventHandler for Handler { // const debug_channel = } } -struct MyStruct { - niečo: String, -} -impl MyStruct { - fn add(&mut self) { - self.niečo.push(char::from_digit(2, 2).unwrap()); - } -} -trait Countable { - fn count(&self) -> usize; -} -impl Countable for MyStruct { - fn count(&self) -> usize { - self.niečo.len() - } -} -fn smt(var: Box) { - var.count(); -} + #[tokio::main] async fn main() -> anyhow::Result<()> { - let mut n = MyStruct { - niečo: "aaa".to_string(), - }; - n.add(); - loop { - //Keeps trying to reconnect, if errors occur print to console and retry - match connect().await { - Ok(r) => return Ok(()), - Err(e) => println!("FAILED TO CONNECT!!! {e}\nRetrying soon..."), - } - } -} - -async fn connect() -> anyhow::Result<()> { use anyhow::Context; let token = dotenv_var("TOKEN").context("No TOKEN in env")?; let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::DIRECT_MESSAGES | GatewayIntents::MESSAGE_CONTENT; - + let mut client = Client::builder(&token, intents) .event_handler(Handler) .await .context("Failed to build client")?; - + client.start().await?; Ok(()) } diff --git a/src/sample.rs b/src/sample.rs new file mode 100644 index 0000000..f5c9e80 --- /dev/null +++ b/src/sample.rs @@ -0,0 +1,19 @@ +struct MyStruct { + niečo: String, +} +impl MyStruct { + fn add(&mut self) { + self.niečo.push(char::from_digit(2, 2).unwrap()); + } +} +trait Countable { + fn count(&self) -> usize; +} +impl Countable for MyStruct { + fn count(&self) -> usize { + self.niečo.len() + } +} +fn smt(var: Box) { + var.count(); +} \ No newline at end of file From 0591f97956c9a2a119c429b21bf081dbcbae759d Mon Sep 17 00:00:00 2001 From: Ladislav Hano <524934@mail.muni.cz> Date: Fri, 7 Jul 2023 13:44:21 +0200 Subject: [PATCH 3/3] hm --- src/main.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9f21376..e9522dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,12 +41,6 @@ impl EventHandler for Handler { let channel = ctx.http.get_channel(780439236867653635).await.unwrap(); - // let channel = await http.get_channel(780439236867653635); - // GuildChannel::say(&self, http, content) - // self.message(ctx, new_message) - // C = ; - - // const debug_channel = } }