Skip to content

Commit

Permalink
add dictionary, server config
Browse files Browse the repository at this point in the history
  • Loading branch information
mii443 committed Dec 7, 2022
1 parent 2a40e9e commit f93701a
Show file tree
Hide file tree
Showing 9 changed files with 429 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/commands/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use serenity::{
model::prelude::interaction::{
application_command::ApplicationCommandInteraction, MessageFlags,
model::prelude::{
component::ButtonStyle,
interaction::{application_command::ApplicationCommandInteraction, MessageFlags},
},
prelude::Context,
};
Expand Down Expand Up @@ -79,6 +80,13 @@ pub async fn config_command(
.placeholder("VOICEVOX Speakerを指定")
})
})
.create_action_row(|a| {
a.create_button(|f| {
f.label("サーバー設定")
.custom_id("TTS_CONFIG_SERVER")
.style(ButtonStyle::Primary)
})
})
})
.flags(MessageFlags::EPHEMERAL)
})
Expand Down
61 changes: 60 additions & 1 deletion src/database/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::tts::{
gcp_tts::structs::voice_selection_params::VoiceSelectionParams, tts_type::TTSType,
};

use super::user_config::UserConfig;
use super::{dictionary::Dictionary, server_config::ServerConfig, user_config::UserConfig};
use redis::Commands;

pub struct Database {
Expand All @@ -14,6 +14,24 @@ impl Database {
Self { client }
}

pub async fn get_server_config(
&mut self,
server_id: u64,
) -> redis::RedisResult<Option<ServerConfig>> {
if let Ok(mut connection) = self.client.get_connection() {
let config: String = connection
.get(format!("discord_server:{}", server_id))
.unwrap_or_default();

match serde_json::from_str(&config) {
Ok(config) => Ok(Some(config)),
Err(_) => Ok(None),
}
} else {
Ok(None)
}
}

pub async fn get_user_config(
&mut self,
user_id: u64,
Expand All @@ -32,6 +50,20 @@ impl Database {
}
}

pub async fn set_server_config(
&mut self,
server_id: u64,
config: ServerConfig,
) -> redis::RedisResult<()> {
let config = serde_json::to_string(&config).unwrap();
self.client
.get_connection()
.unwrap()
.set::<String, String, ()>(format!("discord_server:{}", server_id), config)
.unwrap();
Ok(())
}

pub async fn set_user_config(
&mut self,
user_id: u64,
Expand All @@ -46,6 +78,19 @@ impl Database {
Ok(())
}

pub async fn set_default_server_config(&mut self, server_id: u64) -> redis::RedisResult<()> {
let config = ServerConfig {
dictionary: Dictionary::new(),
};

self.client.get_connection().unwrap().set(
format!("discord_server:{}", server_id),
serde_json::to_string(&config).unwrap(),
)?;

Ok(())
}

pub async fn set_default_user_config(&mut self, user_id: u64) -> redis::RedisResult<()> {
let voice_selection = VoiceSelectionParams {
languageCode: String::from("ja-JP"),
Expand All @@ -69,6 +114,20 @@ impl Database {
Ok(())
}

pub async fn get_server_config_or_default(
&mut self,
server_id: u64,
) -> redis::RedisResult<Option<ServerConfig>> {
let config = self.get_server_config(server_id).await?;
match config {
Some(_) => Ok(config),
None => {
self.set_default_server_config(server_id).await?;
self.get_server_config(server_id).await
}
}
}

pub async fn get_user_config_or_default(
&mut self,
user_id: u64,
Expand Down
34 changes: 34 additions & 0 deletions src/database/dictionary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Rule {
pub id: String,
pub is_regex: bool,
pub rule: String,
pub to: String,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Dictionary {
pub rules: Vec<Rule>,
}

impl Dictionary {
pub fn new() -> Self {
let rules = vec![
Rule {
id: String::from("url"),
is_regex: true,
rule: String::from(r"(http://|https://){1}[\w\.\-/:\#\?=\&;%\~\+]+"),
to: String::from("URL"),
},
Rule {
id: String::from("code"),
is_regex: true,
rule: String::from(r"```(.|\n)*```"),
to: String::from("code"),
},
];
Self { rules }
}
}
2 changes: 2 additions & 0 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod database;
pub mod dictionary;
pub mod server_config;
pub mod user_config;
7 changes: 7 additions & 0 deletions src/database/server_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use super::dictionary::Dictionary;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ServerConfig {
pub dictionary: Dictionary,
}
Loading

0 comments on commit f93701a

Please sign in to comment.