rework for anyhow

This commit is contained in:
Djkato 2023-07-07 12:16:25 +02:00
parent ea9d40b60e
commit 7e9f80df93
3 changed files with 66 additions and 32 deletions

7
Cargo.lock generated
View file

@ -43,6 +43,12 @@ dependencies = [
"libc",
]
[[package]]
name = "anyhow"
version = "1.0.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8"
[[package]]
name = "async-trait"
version = "0.1.70"
@ -956,6 +962,7 @@ dependencies = [
name = "moover_rust"
version = "0.1.0"
dependencies = [
"anyhow",
"cron",
"dotenv",
"mongodb",

View file

@ -6,6 +6,7 @@ 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"

View file

@ -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,17 +26,18 @@ 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 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 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);
@ -47,27 +48,52 @@ 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<dyn Countable>) {
var.count();
}
#[tokio::main]
async fn main() {
let opt_token = dotenv_var("TOKEN");
if opt_token.is_none() {
return;
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..."),
}
}
}
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");
.await
.context("Failed to build client")?;
if let Err(why) = client.start().await {
println!("Client error: {:?}", why);
}
client.start().await?;
Ok(())
}