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"
|
name = "tenorv2"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"dotenv",
|
||||||
"form_urlencoded",
|
"form_urlencoded",
|
||||||
"json",
|
"json",
|
||||||
"regex",
|
"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::async_trait;
|
||||||
use serenity::prelude::GatewayIntents;
|
use serenity::prelude::GatewayIntents;
|
||||||
use serenity::client::Context;
|
use serenity::client::Context;
|
||||||
|
@ -6,17 +14,13 @@ use serenity::model::gateway::Ready;
|
||||||
use serenity::all::{EventHandler, GuildId, Message};
|
use serenity::all::{EventHandler, GuildId, Message};
|
||||||
use serenity::Client;
|
use serenity::Client;
|
||||||
|
|
||||||
|
use dotenv::dotenv;
|
||||||
|
|
||||||
use tokio_cron_scheduler::{JobScheduler, Job};
|
use tokio_cron_scheduler::{JobScheduler, Job};
|
||||||
use util::security::dotenv_var;
|
|
||||||
|
|
||||||
mod message_handler;
|
mod message_handler;
|
||||||
use message_handler::handle;
|
use message_handler::handle;
|
||||||
|
|
||||||
use std::future::Future;
|
|
||||||
use std::pin::Pin;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
mod commands;
|
mod commands;
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
|
@ -24,12 +28,10 @@ mod other;
|
||||||
use other::notice;
|
use other::notice;
|
||||||
|
|
||||||
mod types;
|
mod types;
|
||||||
|
use types::Error;
|
||||||
|
|
||||||
struct Handler;
|
struct Handler;
|
||||||
|
|
||||||
use commands::say::say;
|
|
||||||
use types::Error;
|
|
||||||
|
|
||||||
async fn on_error(error: poise::FrameworkError<'_, (), Error>) {
|
async fn on_error(error: poise::FrameworkError<'_, (), Error>) {
|
||||||
match error {
|
match error {
|
||||||
poise::FrameworkError::Setup { error, .. } => panic!("Failed to start bot: {:?}", error),
|
poise::FrameworkError::Setup { error, .. } => panic!("Failed to start bot: {:?}", error),
|
||||||
|
@ -76,9 +78,11 @@ impl EventHandler for Handler {
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
|
|
||||||
|
dotenv().ok();
|
||||||
|
|
||||||
// create poise framework for registering commands
|
// create poise framework for registering commands
|
||||||
let options = poise::FrameworkOptions {
|
let options: poise::FrameworkOptions<(), Box<dyn error::Error + Send + Sync>> = poise::FrameworkOptions {
|
||||||
commands: vec![say()],
|
commands: vec![],
|
||||||
prefix_options: poise::PrefixFrameworkOptions {
|
prefix_options: poise::PrefixFrameworkOptions {
|
||||||
prefix: Some("/".into()),
|
prefix: Some("/".into()),
|
||||||
edit_tracker: Some(Arc::new(poise::EditTracker::for_timespan(
|
edit_tracker: Some(Arc::new(poise::EditTracker::for_timespan(
|
||||||
|
@ -95,7 +99,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
..Default::default()
|
..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")?
|
.context("DEBUG_GUILD_ID not found in env")?
|
||||||
.parse::<u64>().unwrap();
|
.parse::<u64>().unwrap();
|
||||||
|
|
||||||
|
@ -114,7 +118,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
#[cfg(feature="DEBUG")]
|
#[cfg(feature="DEBUG")]
|
||||||
let token_str = "DEBUGTOKEN";
|
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;
|
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 sqlx::{Connection, FromRow, SqliteConnection};
|
||||||
|
|
||||||
use crate::util::security::dotenv_var;
|
|
||||||
use crate::util::debug::send_error;
|
use crate::util::debug::send_error;
|
||||||
use crate::util::utilities;
|
use crate::util::utilities;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::{env, sync::Arc};
|
||||||
|
|
||||||
use tenorv2::{tenor, tenor_builder::Tenor, tenor_types::{ContentFilter, MediaFilter}};
|
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<()> {
|
async fn celebrate_birthday(guild_id: GuildId, user_id: UserId, nick: &str, http: Arc<Http>) -> anyhow::Result<()> {
|
||||||
use anyhow::Context;
|
|
||||||
|
|
||||||
const LIMIT: u8 = 20;
|
const LIMIT: u8 = 20;
|
||||||
|
|
||||||
let tenor_response = Tenor::new()
|
let tenor_response = Tenor::new()?
|
||||||
.key(dotenv_var("TENORV2").context("TENORV2 key not found in the .env")?)
|
|
||||||
.random(true)
|
.random(true)
|
||||||
.contentfilter(ContentFilter::low)
|
.contentfilter(ContentFilter::low)
|
||||||
.add_media_filter(MediaFilter::gif)
|
.add_media_filter(MediaFilter::gif)
|
||||||
|
.locale("sk".to_string())
|
||||||
.search("vsetko najlepsie").await?;
|
.search("vsetko najlepsie").await?;
|
||||||
|
|
||||||
let index = rand::random::<usize>() % LIMIT as usize;
|
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 month = local.month();
|
||||||
let year = local.year();
|
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?;
|
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")] {
|
#[cfg(feature="RELEASE")] {
|
||||||
use serenity::all::ChannelId;
|
use serenity::all::ChannelId;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
use super::security::dotenv_var;
|
use std::env;
|
||||||
use anyhow::Context;
|
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())
|
match ChannelId::new(channel_id.parse::<u64>().unwrap())
|
||||||
.say(_http, msg).await {
|
.say(_http, msg).await {
|
||||||
Ok(_) => { return Ok(()); }
|
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<()> {
|
pub async fn hello(http: Arc<Http>) -> anyhow::Result<()> {
|
||||||
use serenity::all::ChannelId;
|
use serenity::all::ChannelId;
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
|
use std::env;
|
||||||
use super::security::dotenv_var;
|
|
||||||
|
|
||||||
let messages = [
|
let messages = [
|
||||||
"AAAAAAAAAAAAAAAAAAAA",
|
"AAAAAAAAAAAAAAAAAAAA",
|
||||||
|
@ -41,7 +40,7 @@ pub async fn hello(http: Arc<Http>) -> anyhow::Result<()> {
|
||||||
|
|
||||||
let num = rand::random::<usize>() % messages.len();
|
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());
|
let channel = ChannelId::new(channel_id.parse::<u64>().unwrap());
|
||||||
if let Err(why) = channel.say(http, messages[num]).await {
|
if let Err(why) = channel.say(http, messages[num]).await {
|
||||||
print!("Error sending message: {:?}", why);
|
print!("Error sending message: {:?}", why);
|
||||||
|
|
|
@ -1,3 +1,2 @@
|
||||||
pub mod security;
|
|
||||||
pub mod debug;
|
pub mod debug;
|
||||||
pub mod utilities;
|
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