fixed lang & finalized it (still experimental)
This commit is contained in:
+1
-1
@@ -8,7 +8,7 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
base64 = "0.22.1"
|
base64 = "0.22.1"
|
||||||
clap = { version = "4.5.28", features = ["derive"] }
|
clap = { version = "4.5.28", features = ["derive"] }
|
||||||
dynfmt = "0.1.5"
|
dynfmt = { version = "0.1.5", features = ["curly"] }
|
||||||
flate2 = "1.1.2"
|
flate2 = "1.1.2"
|
||||||
futures = "0.3.31"
|
futures = "0.3.31"
|
||||||
poise = "0.6.1"
|
poise = "0.6.1"
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
pub mod guild_invite;
|
pub mod guild_invite;
|
||||||
|
pub mod lang;
|
||||||
pub mod leave_guild;
|
pub mod leave_guild;
|
||||||
pub mod main_cmd;
|
pub mod main_cmd;
|
||||||
pub mod ping;
|
pub mod ping;
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
use crate::{Context, Error, messages::send_msg};
|
||||||
|
|
||||||
|
pub async fn cmd(ctx: Context<'_>, path: String) -> Result<(), Error> {
|
||||||
|
send_msg(ctx, ctx.data().lang.get(path.as_str(), &[]), true, true).await;
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
use crate::{Context, Error, cmds::debug::{guild_invite, leave_guild, ping, save, stop, view_guilds, whoami}};
|
use crate::{Context, Error, cmds::debug::{guild_invite, lang, leave_guild, ping, save, stop, view_guilds, whoami}};
|
||||||
|
|
||||||
|
|
||||||
#[derive(poise::ChoiceParameter, PartialEq)]
|
#[derive(poise::ChoiceParameter, PartialEq)]
|
||||||
pub enum Subcommands {
|
pub enum Subcommands {
|
||||||
GuildInvite,
|
GuildInvite,
|
||||||
|
Lang,
|
||||||
LeaveGuild,
|
LeaveGuild,
|
||||||
Ping,
|
Ping,
|
||||||
//ReloadCfg,
|
//ReloadCfg,
|
||||||
@@ -31,9 +32,11 @@ pub async fn cmd(
|
|||||||
) -> Result<(), Error>
|
) -> Result<(), Error>
|
||||||
{
|
{
|
||||||
let u64_arg_u: u64 = u64_arg.unwrap_or("0".to_string()).as_str().parse()?;
|
let u64_arg_u: u64 = u64_arg.unwrap_or("0".to_string()).as_str().parse()?;
|
||||||
|
let str_arg_u: String = string_arg.clone().unwrap_or("".to_string());
|
||||||
|
|
||||||
match subcommand {
|
match subcommand {
|
||||||
Subcommands::GuildInvite => guild_invite::cmd(ctx, u64_arg_u).await?,
|
Subcommands::GuildInvite => guild_invite::cmd(ctx, u64_arg_u).await?,
|
||||||
|
Subcommands::Lang => lang::cmd(ctx, str_arg_u).await?,
|
||||||
Subcommands::LeaveGuild => leave_guild::cmd(ctx, u64_arg_u).await?,
|
Subcommands::LeaveGuild => leave_guild::cmd(ctx, u64_arg_u).await?,
|
||||||
Subcommands::Ping => ping::cmd(ctx).await?,
|
Subcommands::Ping => ping::cmd(ctx).await?,
|
||||||
//Subcommands::ReloadCfg => reload_cfg::cmd(ctx).await?,
|
//Subcommands::ReloadCfg => reload_cfg::cmd(ctx).await?,
|
||||||
|
|||||||
+1
-1
@@ -8,7 +8,7 @@ pub struct AssistantEnv {
|
|||||||
|
|
||||||
impl AssistantEnv {
|
impl AssistantEnv {
|
||||||
pub fn new(test: bool) -> Self {
|
pub fn new(test: bool) -> Self {
|
||||||
let token_name = if test { "ASSISTANT_TOKEN" }
|
let token_name = if !test { "ASSISTANT_TOKEN" }
|
||||||
else { "ASSISTANT_TOKEN_TEST" };
|
else { "ASSISTANT_TOKEN_TEST" };
|
||||||
|
|
||||||
return AssistantEnv {
|
return AssistantEnv {
|
||||||
|
|||||||
+31
-19
@@ -1,9 +1,17 @@
|
|||||||
use std::path::PathBuf;
|
use std::{fs, path::PathBuf};
|
||||||
|
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use dynfmt::{Format, NoopFormat};
|
use dynfmt::{Format, SimpleCurlyFormat};
|
||||||
|
|
||||||
use crate::Error;
|
use crate::{Error, rs_warnln};
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum LangErrorType {
|
||||||
|
Fallback,
|
||||||
|
ShortIndex,
|
||||||
|
KeyNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct Lang {
|
pub struct Lang {
|
||||||
@@ -12,21 +20,23 @@ pub struct Lang {
|
|||||||
|
|
||||||
|
|
||||||
impl From<Value> for Lang {
|
impl From<Value> for Lang {
|
||||||
fn from(value: Value) -> Self {
|
fn from(value: Value) -> Self
|
||||||
return Lang { data: value };
|
{ return Lang { data: value }; }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Lang {
|
impl Lang {
|
||||||
pub fn from_file(filepath: PathBuf) -> Result<Self, Error> {
|
pub fn from_file(filepath: PathBuf) -> Result<Self, Error> {
|
||||||
if !filepath.exists() { return Err(Error::from("LANG filepath not found!")); }
|
if !filepath.exists() { return Err(Error::from("LANG filepath not found!")); }
|
||||||
|
|
||||||
|
let file_contents = fs::read_to_string(filepath).unwrap();
|
||||||
|
let json = serde_json::from_str(&file_contents).unwrap();
|
||||||
|
|
||||||
return Ok(Lang { data: Value::Null });
|
return Ok(Lang { data: json });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn get(&self, path: &'static str, args: &[String]) -> String {
|
pub fn get(&self, path: &str, args: &[String]) -> String {
|
||||||
let path_arr: Vec<&str> = path.split(".").collect();
|
let path_arr: Vec<&str> = path.split(".").collect();
|
||||||
return self.get_from_arr(path_arr, args);
|
return self.get_from_arr(path_arr, args);
|
||||||
}
|
}
|
||||||
@@ -35,29 +45,31 @@ impl Lang {
|
|||||||
pub fn get_from_arr(&self, path: Vec<&str>, args: &[String]) -> String {
|
pub fn get_from_arr(&self, path: Vec<&str>, args: &[String]) -> String {
|
||||||
let str_path = path.join(".");
|
let str_path = path.join(".");
|
||||||
let mut search: &Value = &self.data;
|
let mut search: &Value = &self.data;
|
||||||
|
|
||||||
for i in &path {
|
for i in &path {
|
||||||
let r = search.get(i);
|
let Some(r) = search.get(i)
|
||||||
|
else { rs_warnln!("LANG warning ({:?})! ({})", LangErrorType::KeyNotFound, str_path); return str_path; };
|
||||||
|
|
||||||
if let Some(some) = r {
|
let str_r = r.as_str();
|
||||||
let str_r = search.as_str();
|
|
||||||
|
|
||||||
if let Some(string) = str_r
|
if let Some(string) = str_r
|
||||||
{ return Lang::format_str(string, args, str_path); }
|
{ return Lang::format_str(string, args, str_path); }
|
||||||
else { search = some; }
|
else { search = r; }
|
||||||
}
|
|
||||||
else { return str_path; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rs_warnln!("LANG warning ({:?})! ({})", LangErrorType::ShortIndex, str_path);
|
||||||
return str_path;
|
return str_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn format_str(string: &str, args: &[String], fallback: String) -> String {
|
fn format_str(string: &str, args: &[String], fallback: String) -> String {
|
||||||
let cow = NoopFormat.format(string, args);
|
let cow = SimpleCurlyFormat.format(string, args);
|
||||||
|
|
||||||
if let Ok(ok) = cow
|
if let Ok(ok) = cow
|
||||||
{ return ok.to_string(); }
|
{ return ok.to_string(); }
|
||||||
else { return fallback; }
|
else {
|
||||||
|
rs_warnln!("LANG warning ({:?})! ({})", LangErrorType::Fallback, fallback);
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+5
-2
@@ -68,8 +68,9 @@ async fn main() {
|
|||||||
let run_py = !cfg_arr.iter().any(|val| val.as_str() == Some("re"));
|
let run_py = !cfg_arr.iter().any(|val| val.as_str() == Some("re"));
|
||||||
|
|
||||||
if !run_py { rs_println!("[IMPORTANT] You have disabled the \"re\" commands in the CFG. The app will not run the Python code and the websockets to save resources!"); }
|
if !run_py { rs_println!("[IMPORTANT] You have disabled the \"re\" commands in the CFG. The app will not run the Python code and the websockets to save resources!"); }
|
||||||
|
|
||||||
if !data.args.nosched { start_schedules(data.args.test).await; }
|
let nosched = data.args.nosched;
|
||||||
|
let test = data.args.test;
|
||||||
|
|
||||||
let python = start_py(
|
let python = start_py(
|
||||||
data.args.clone(),
|
data.args.clone(),
|
||||||
@@ -78,6 +79,8 @@ async fn main() {
|
|||||||
run_py
|
run_py
|
||||||
);
|
);
|
||||||
let rust = start_rs(data, run_py);
|
let rust = start_rs(data, run_py);
|
||||||
|
|
||||||
|
if !nosched { start_schedules(test).await; }
|
||||||
|
|
||||||
rust.join().unwrap();
|
rust.join().unwrap();
|
||||||
python.join().unwrap();
|
python.join().unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user