fix: refactor dotenv variables
This commit is contained in:
parent
d3090c58de
commit
6d1d920385
6 changed files with 27 additions and 38 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2454,6 +2454,7 @@ dependencies = [
|
|||
name = "tenorv2"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"dotenv",
|
||||
"form_urlencoded",
|
||||
"json",
|
||||
"regex",
|
||||
|
|
32
src/main.rs
32
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<dyn error::Error + Send + Sync>> = 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::<u64>().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;
|
||||
|
||||
|
|
|
@ -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<Http>) -> 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::<usize>() % LIMIT as usize;
|
||||
|
@ -109,7 +106,7 @@ async fn notice(http: Arc<Http>) -> 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?;
|
||||
|
||||
|
|
|
@ -8,10 +8,10 @@ pub async fn send_error(_http: Arc<Http>, 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::<u64>().unwrap())
|
||||
.say(_http, msg).await {
|
||||
Ok(_) => { return Ok(()); }
|
||||
|
@ -25,8 +25,7 @@ pub async fn send_error(_http: Arc<Http>, msg: String) -> anyhow::Result<()> {
|
|||
pub async fn hello(http: Arc<Http>) -> 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<Http>) -> anyhow::Result<()> {
|
|||
|
||||
let num = rand::random::<usize>() % 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::<u64>().unwrap());
|
||||
if let Err(why) = channel.say(http, messages[num]).await {
|
||||
print!("Error sending message: {:?}", why);
|
||||
|
|
|
@ -1,3 +1,2 @@
|
|||
pub mod security;
|
||||
pub mod debug;
|
||||
pub mod utilities;
|
|
@ -1,11 +0,0 @@
|
|||
use dotenv::dotenv;
|
||||
use std::env;
|
||||
|
||||
pub fn dotenv_var(key: &str) -> Option<String> {
|
||||
dotenv().ok();
|
||||
|
||||
match env::var(key) {
|
||||
Ok(val) => return Some(val),
|
||||
Err(_) => None
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue