fix database connection bug

This commit is contained in:
mii
2022-08-12 17:57:28 +09:00
parent 6aec4e4ea7
commit b36bee8be8
2 changed files with 7 additions and 8 deletions

View File

@ -4,16 +4,16 @@ use super::user_config::UserConfig;
use redis::Commands; use redis::Commands;
pub struct Database { pub struct Database {
pub connection: redis::Connection pub client: redis::Client
} }
impl Database { impl Database {
pub fn new(connection: redis::Connection) -> Self { pub fn new(client: redis::Client) -> Self {
Self { connection } Self { client }
} }
pub async fn get_user_config(&mut self, user_id: u64) -> redis::RedisResult<Option<UserConfig>> { pub async fn get_user_config(&mut self, user_id: u64) -> redis::RedisResult<Option<UserConfig>> {
let config: String = self.connection.get(format!("discord_user:{}", user_id)).unwrap_or_default(); let config: String = self.client.get_connection().unwrap().get(format!("discord_user:{}", user_id)).unwrap_or_default();
match serde_json::from_str(&config) { match serde_json::from_str(&config) {
Ok(config) => Ok(Some(config)), Ok(config) => Ok(Some(config)),
@ -23,7 +23,7 @@ impl Database {
pub async fn set_user_config(&mut self, user_id: u64, config: UserConfig) -> redis::RedisResult<()> { pub async fn set_user_config(&mut self, user_id: u64, config: UserConfig) -> redis::RedisResult<()> {
let config = serde_json::to_string(&config).unwrap(); let config = serde_json::to_string(&config).unwrap();
self.connection.set::<String, String, ()>(format!("discord_user:{}", user_id), config).unwrap(); self.client.get_connection().unwrap().set::<String, String, ()>(format!("discord_user:{}", user_id), config).unwrap();
Ok(()) Ok(())
} }
@ -42,7 +42,7 @@ impl Database {
voicevox_speaker: Some(1) voicevox_speaker: Some(1)
}; };
self.connection.set(format!("discord_user:{}", user_id), serde_json::to_string(&config).unwrap())?; self.client.get_connection().unwrap().set(format!("discord_user:{}", user_id), serde_json::to_string(&config).unwrap())?;
Ok(()) Ok(())
} }

View File

@ -61,8 +61,7 @@ async fn main() {
let database_client = { let database_client = {
let redis_client = redis::Client::open(config.redis_url).unwrap(); let redis_client = redis::Client::open(config.redis_url).unwrap();
let con = redis_client.get_connection().unwrap(); Database::new(redis_client)
Database::new(con)
}; };
// Create TTS storage // Create TTS storage