diff --git a/Cargo.lock b/Cargo.lock index d2d7c18..c63ac13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2454,6 +2454,7 @@ dependencies = [ name = "tenorv2" version = "0.1.0" dependencies = [ + "dotenv", "form_urlencoded", "json", "regex", diff --git a/src/main.rs b/src/main.rs index bf2381e..de3c551 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,12 @@ -use poise::samples::{register_globally, register_in_guild}; +use std::future::Future; +use std::pin::Pin; +use std::sync::Arc; +use std::time::Duration; +use std::error; +use std::env; + +use poise::samples::register_in_guild; + use serenity::async_trait; use serenity::prelude::GatewayIntents; use serenity::client::Context; @@ -6,17 +14,13 @@ use serenity::model::gateway::Ready; use serenity::all::{EventHandler, GuildId, Message}; use serenity::Client; +use dotenv::dotenv; + use tokio_cron_scheduler::{JobScheduler, Job}; -use util::security::dotenv_var; mod message_handler; use message_handler::handle; -use std::future::Future; -use std::pin::Pin; -use std::sync::Arc; -use std::time::Duration; - mod commands; mod util; @@ -24,12 +28,10 @@ mod other; use other::notice; mod types; +use types::Error; struct Handler; -use commands::say::say; -use types::Error; - async fn on_error(error: poise::FrameworkError<'_, (), Error>) { match error { poise::FrameworkError::Setup { error, .. } => panic!("Failed to start bot: {:?}", error), @@ -76,9 +78,11 @@ impl EventHandler for Handler { async fn main() -> anyhow::Result<()> { use anyhow::Context; + dotenv().ok(); + // create poise framework for registering commands - let options = poise::FrameworkOptions { - commands: vec![say()], + let options: poise::FrameworkOptions<(), Box> = poise::FrameworkOptions { + commands: vec![], prefix_options: poise::PrefixFrameworkOptions { prefix: Some("/".into()), edit_tracker: Some(Arc::new(poise::EditTracker::for_timespan( @@ -95,7 +99,7 @@ async fn main() -> anyhow::Result<()> { ..Default::default() }; - let debug_guild_id = dotenv_var("DEBUG_GUILD_ID") + let debug_guild_id = env::var("DEBUG_GUILD_ID") .context("DEBUG_GUILD_ID not found in env")? .parse::().unwrap(); @@ -114,7 +118,7 @@ async fn main() -> anyhow::Result<()> { #[cfg(feature="DEBUG")] let token_str = "DEBUGTOKEN"; - let token = dotenv_var(token_str).context("TOKEN not found in env")?; + let token = env::var(token_str).context("TOKEN not found in env")?; let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT; diff --git a/src/other/notice.rs b/src/other/notice.rs index 5e52bf4..e7fde2e 100644 --- a/src/other/notice.rs +++ b/src/other/notice.rs @@ -6,11 +6,10 @@ use serenity::{all::{GuildId, UserId}, builder::{CreateEmbed, CreateMessage}, cl use sqlx::{Connection, FromRow, SqliteConnection}; -use crate::util::security::dotenv_var; use crate::util::debug::send_error; use crate::util::utilities; -use std::sync::Arc; +use std::{env, sync::Arc}; use tenorv2::{tenor, tenor_builder::Tenor, tenor_types::{ContentFilter, MediaFilter}}; @@ -48,15 +47,13 @@ async fn announce_event(guild_id: GuildId, name: &str, special_message: &str, ht } async fn celebrate_birthday(guild_id: GuildId, user_id: UserId, nick: &str, http: Arc) -> anyhow::Result<()> { - use anyhow::Context; - const LIMIT: u8 = 20; - let tenor_response = Tenor::new() - .key(dotenv_var("TENORV2").context("TENORV2 key not found in the .env")?) + let tenor_response = Tenor::new()? .random(true) .contentfilter(ContentFilter::low) .add_media_filter(MediaFilter::gif) + .locale("sk".to_string()) .search("vsetko najlepsie").await?; let index = rand::random::() % LIMIT as usize; @@ -109,7 +106,7 @@ async fn notice(http: Arc) -> anyhow::Result<()> { let month = local.month(); let year = local.year(); - let db_path = dotenv_var("DATABASE_URL").context("DATABASE_URL not found in env")?; + let db_path = env::var("DATABASE_URL").context("DATABASE_URL not found in env")?; let mut db = SqliteConnection::connect(db_path.as_str()).await?; diff --git a/src/util/debug.rs b/src/util/debug.rs index 3782029..1df4c69 100644 --- a/src/util/debug.rs +++ b/src/util/debug.rs @@ -8,10 +8,10 @@ pub async fn send_error(_http: Arc, msg: String) -> anyhow::Result<()> { #[cfg(feature="RELEASE")] { use serenity::all::ChannelId; use std::process::exit; - use super::security::dotenv_var; + use std::env; use anyhow::Context; - let channel_id: String = dotenv_var("DEBUG_CHANNEL_ID").context("DEBUG_CHANNEL_ID not found in env file")?; + let channel_id: String = env::var("DEBUG_CHANNEL_ID").context("DEBUG_CHANNEL_ID not found in env file")?; match ChannelId::new(channel_id.parse::().unwrap()) .say(_http, msg).await { Ok(_) => { return Ok(()); } @@ -25,8 +25,7 @@ pub async fn send_error(_http: Arc, msg: String) -> anyhow::Result<()> { pub async fn hello(http: Arc) -> anyhow::Result<()> { use serenity::all::ChannelId; use anyhow::Context; - - use super::security::dotenv_var; + use std::env; let messages = [ "AAAAAAAAAAAAAAAAAAAA", @@ -41,7 +40,7 @@ pub async fn hello(http: Arc) -> anyhow::Result<()> { let num = rand::random::() % messages.len(); - let channel_id: String = dotenv_var("DEBUG_CHANNEL_ID").context("DEBUG_CHANNEL_ID not found in env file")?; + let channel_id: String = env::var("DEBUG_CHANNEL_ID").context("DEBUG_CHANNEL_ID not found in env file")?; let channel = ChannelId::new(channel_id.parse::().unwrap()); if let Err(why) = channel.say(http, messages[num]).await { print!("Error sending message: {:?}", why); diff --git a/src/util/mod.rs b/src/util/mod.rs index 74e5d6d..d3d2689 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,3 +1,2 @@ -pub mod security; pub mod debug; pub mod utilities; \ No newline at end of file diff --git a/src/util/security.rs b/src/util/security.rs deleted file mode 100644 index 80f2829..0000000 --- a/src/util/security.rs +++ /dev/null @@ -1,11 +0,0 @@ -use dotenv::dotenv; -use std::env; - -pub fn dotenv_var(key: &str) -> Option { - dotenv().ok(); - - match env::var(key) { - Ok(val) => return Some(val), - Err(_) => None - } -} \ No newline at end of file