mirror of
https://codeberg.org/JasterV/gofish_bot.git
synced 2026-04-26 18:10:09 +00:00
refactor
This commit is contained in:
parent
dcc79bdbf0
commit
7703dd1c8e
4 changed files with 26 additions and 28 deletions
9
Cargo.lock
generated
9
Cargo.lock
generated
|
|
@ -34,6 +34,12 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "async_once"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "58f8f7a882f8fffee0bdbb0580033b01613b50781e03c02713da0599ed90b595"
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
|
|
@ -423,11 +429,12 @@ name = "gofish"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async_once",
|
||||
"dotenv",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"mobc",
|
||||
"mobc-redis",
|
||||
"once_cell",
|
||||
"pretty_env_logger",
|
||||
"teloxide",
|
||||
"thiserror",
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ thiserror = "1.0.x"
|
|||
anyhow = "1.0.x"
|
||||
pretty_env_logger = "0.4.0"
|
||||
tokio = { version = "1.3.0", features = ["rt-multi-thread", "macros"] }
|
||||
once_cell = "1.8.x"
|
||||
async_once = "0.2.1"
|
||||
lazy_static = "1.4.x"
|
||||
mobc-redis = "0.7.0"
|
||||
mobc = "0.7.3"
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
use crate::alias::{MobcCon, MobcPool};
|
||||
use crate::errors::MobcError::*;
|
||||
use anyhow::Result;
|
||||
use async_once::AsyncOnce;
|
||||
use lazy_static::lazy_static;
|
||||
use mobc::Pool;
|
||||
use mobc_redis::redis::{AsyncCommands, FromRedisValue, ToRedisArgs};
|
||||
use mobc_redis::{redis, RedisConnectionManager};
|
||||
|
|
@ -11,6 +13,13 @@ pub const CACHE_POOL_MAX_IDLE: u64 = 8;
|
|||
pub const CACHE_POOL_TIMEOUT_SECONDS: u64 = 1;
|
||||
pub const CACHE_POOL_EXPIRE_SECONDS: u64 = 60;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref POOL: AsyncOnce<MobcPool> = AsyncOnce::new(async {
|
||||
let url = std::env::var("REDIS_URL").expect("REDIS_URL not found");
|
||||
connect(&url).await.expect("Error connecting to redis")
|
||||
});
|
||||
}
|
||||
|
||||
pub async fn connect(url: &str) -> Result<MobcPool> {
|
||||
let client = redis::Client::open(url).map_err(RedisClientError)?;
|
||||
let manager = RedisConnectionManager::new(client);
|
||||
|
|
|
|||
33
src/main.rs
33
src/main.rs
|
|
@ -4,49 +4,30 @@ mod command;
|
|||
mod db;
|
||||
mod errors;
|
||||
|
||||
use crate::db::connect;
|
||||
use alias::{Cx, MobcPool};
|
||||
use alias::Cx;
|
||||
use anyhow::Result;
|
||||
use command::Command;
|
||||
use dotenv;
|
||||
use once_cell::sync::OnceCell;
|
||||
use teloxide::{prelude::*, types::Me};
|
||||
|
||||
pub static POOL: OnceCell<MobcPool> = OnceCell::new();
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
dotenv::dotenv().ok();
|
||||
teloxide::enable_logging!();
|
||||
let url = std::env::var("REDIS_URL").expect("REDIS_URL not found");
|
||||
run(&url).await;
|
||||
run().await;
|
||||
}
|
||||
|
||||
async fn run(redis_url: &str) {
|
||||
log::info!("Connecting to redis...");
|
||||
initialize_pool(redis_url).await;
|
||||
async fn run() {
|
||||
log::info!("Starting bot...");
|
||||
let (bot, bot_name) = initialize_bot().await;
|
||||
let bot = Bot::from_env().auto_send();
|
||||
let Me { user: bot_user, .. } = bot.get_me().await.unwrap();
|
||||
let bot_name = bot_user.username.expect("Bots must have usernames");
|
||||
log::info!("listening...");
|
||||
teloxide::commands_repl(bot, bot_name, execute).await;
|
||||
}
|
||||
|
||||
async fn initialize_bot() -> (AutoSend<Bot>, String) {
|
||||
let bot = Bot::from_env().auto_send();
|
||||
let Me { user: bot_user, .. } = bot.get_me().await.unwrap();
|
||||
let bot_name = bot_user.username.expect("Bots must have usernames");
|
||||
(bot, bot_name)
|
||||
}
|
||||
|
||||
async fn initialize_pool(url: &str) {
|
||||
let pool = connect(url).await.expect("Error connecting to redis");
|
||||
POOL.set(pool)
|
||||
.map_err(|_| ())
|
||||
.expect("Error initializing pool");
|
||||
}
|
||||
|
||||
async fn execute(cx: Cx, command: Command) -> Result<()> {
|
||||
let pool = POOL.get().unwrap();
|
||||
let pool = db::POOL.get().await;
|
||||
match command {
|
||||
Command::Help => actions::help(&cx).await?,
|
||||
_ => actions::say_hi(&cx).await?,
|
||||
|
|
|
|||
Loading…
Reference in a new issue