i kinda forgot to commit. Anyways, i made the config into toml, edited readme, and... refactored code...
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{data::{get_mutex_data, read_cfg_data}, lang, messages::send_msg, websocket::send_cmd_json, Context, Error};
|
||||
use crate::{data::{get_toml_mutex, read_cfg_data}, lang, messages::send_msg, websocket::send_cmd_json, Context, Error};
|
||||
|
||||
|
||||
#[poise::command(
|
||||
@@ -17,14 +17,14 @@ pub async fn cmd(
|
||||
) -> Result<(), Error>
|
||||
{
|
||||
read_cfg_data(&ctx.data(), false).await;
|
||||
let d = get_mutex_data(&ctx.data().cfg).await?;
|
||||
let d_str = serde_json::to_string(&d)?;
|
||||
let d = get_toml_mutex(&ctx.data().cfg).await.unwrap();
|
||||
let d_str = toml::to_string(&d)?;
|
||||
let r = send_cmd_json("update_cfg", Some(json!([d_str])), true).await;
|
||||
|
||||
if r.is_some() && r.unwrap()["value"].as_bool().unwrap() {
|
||||
send_msg(
|
||||
ctx,
|
||||
lang!("dc_msg_reload_cfg_success", serde_json::to_string_pretty(&d).unwrap()),
|
||||
lang!("dc_msg_reload_cfg_success", toml::to_string_pretty(&d).unwrap()),
|
||||
true,
|
||||
true
|
||||
).await;
|
||||
|
||||
+23
-11
@@ -8,12 +8,15 @@ use crate::{errln, rs_println, Data, Error, CFG_DATA_RE, LANG};
|
||||
use crate::websocket::send_cmd_json;
|
||||
|
||||
|
||||
static DATA_PATH_DC: &str = "./data/dc_data.json";
|
||||
static PRESET_PATH_DC: &str = "./data/dc_data_preset.json";
|
||||
static DATA_PATH_RE: &str = "./data/re_data.json";
|
||||
static PRESET_PATH_RE: &str = "./data/re_data_preset.json";
|
||||
static DATA_PATH_CFG: &str = "./data/cfg.json";
|
||||
static PRESET_PATH_CFG: &str = "./data/cfg_default.json";
|
||||
static DATA_PATH_DC: &str = "./data/db/dc_data.json";
|
||||
static PRESET_PATH_DC: &str = "./data/defaults/dc_data_preset.json";
|
||||
|
||||
static DATA_PATH_RE: &str = "./data/db/re_data.json";
|
||||
static PRESET_PATH_RE: &str = "./data/defaults/re_data_preset.json";
|
||||
|
||||
static DATA_PATH_CFG: &str = "./cfg/cfg.toml";
|
||||
static PRESET_PATH_CFG: &str = "./data/defaults/cfg_default.toml";
|
||||
|
||||
static DATA_PATH_LANG: &str = "./data/lang/";
|
||||
|
||||
pub static DC_POSTS_CHANNEL_KEY: &str = "re_posts_channel";
|
||||
@@ -117,15 +120,15 @@ pub async fn read_cfg_data(data: &Data, wipe: bool) {
|
||||
if !Path::new(DATA_PATH_CFG).exists() || wipe {
|
||||
rs_println!(
|
||||
"{} creating new from preset...",
|
||||
if !wipe { "cfg.json not found," } else { "[WIPE] (cfg.json)" }
|
||||
if !wipe { "cfg.toml not found," } else { "[WIPE] (cfg.toml)" }
|
||||
);
|
||||
generate_cfg_data();
|
||||
}
|
||||
|
||||
let str_data = fs::read_to_string(DATA_PATH_CFG).unwrap();
|
||||
let json_data = serde_json::from_str(&str_data).unwrap();
|
||||
let json_data: toml::Value = str_data.parse().unwrap();
|
||||
let mut cfg_data = data.cfg.lock().await;
|
||||
*cfg_data = json_data;
|
||||
*cfg_data = Some(json_data);
|
||||
|
||||
send_cmd_json("update_cfg", Some(json!([str_data])), true).await;
|
||||
}
|
||||
@@ -133,9 +136,9 @@ pub async fn read_cfg_data(data: &Data, wipe: bool) {
|
||||
|
||||
fn generate_cfg_data() {
|
||||
let preset_str = fs::read_to_string(PRESET_PATH_CFG).unwrap();
|
||||
let preset_json: Value = serde_json::from_str(&preset_str).unwrap();
|
||||
let preset_json: toml::Value = preset_str.parse().unwrap();
|
||||
|
||||
let json_str = serde_json::to_string_pretty(&preset_json).unwrap();
|
||||
let json_str = toml::to_string_pretty(&preset_json).unwrap();
|
||||
|
||||
let mut file = fs::File::create(DATA_PATH_CFG).unwrap();
|
||||
file.write_all(json_str.as_bytes()).unwrap();
|
||||
@@ -200,6 +203,15 @@ pub async fn get_mutex_data(data: &Mutex<Option<Value>>) -> Result<Value, Error>
|
||||
}
|
||||
|
||||
|
||||
pub async fn get_toml_mutex(data: &Mutex<Option<toml::Value>>) -> Result<toml::Value, Error> {
|
||||
let data_lock = data.lock().await;
|
||||
return match data_lock.as_ref() {
|
||||
Some(data) => Ok(data.clone()),
|
||||
None => Err("Cannot get mutex data: The data is corrupted!".into()),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
pub fn load_lang_data(lang: String) {
|
||||
let full_path = format!("{}{}.json", DATA_PATH_LANG, lang);
|
||||
|
||||
|
||||
+2
-4
@@ -19,11 +19,9 @@ pub fn event_handler<'a>(
|
||||
data_about_bot.user.id
|
||||
);
|
||||
|
||||
let file_text = std::fs::read_to_string("./data/status.txt").unwrap();
|
||||
let file_text = std::fs::read_to_string("./cfg/status.txt").unwrap();
|
||||
let custom_activity = ActivityData::custom(file_text);
|
||||
// TODO: make custom rich presence
|
||||
//let playing_activity
|
||||
|
||||
|
||||
ctx.online();
|
||||
ctx.set_activity(Some(custom_activity));
|
||||
}
|
||||
|
||||
+30
-29
@@ -4,12 +4,12 @@ use poise::serenity_prelude::UserId;
|
||||
use poise::serenity_prelude as serenity;
|
||||
use poise::serenity_prelude::Client;
|
||||
|
||||
use crate::{cmds, data::{self, get_mutex_data}, events, re_cmds, rs_println, Args, Cmd, Data};
|
||||
use crate::{cmds, data::{self, get_toml_mutex}, events, re_cmds, rs_println, Args, Cmd, Data};
|
||||
|
||||
|
||||
pub async fn gen_data(args: Args, owners: Vec<u64>) -> Data {
|
||||
let ball_classic_str = std::fs::read_to_string("./data/8-ball_classic.txt").unwrap();
|
||||
let ball_quirk_str = std::fs::read_to_string("./data/8-ball_quirky.txt").unwrap();
|
||||
let ball_classic_str = std::fs::read_to_string("./cfg/8-ball_classic.txt").unwrap();
|
||||
let ball_quirk_str = std::fs::read_to_string("./cfg/8-ball_quirky.txt").unwrap();
|
||||
|
||||
let ball_classic: Vec<String> = ball_classic_str.lines().map(String::from).collect();
|
||||
let ball_quirk: Vec<String> = ball_quirk_str .lines().map(String::from).collect();
|
||||
@@ -76,38 +76,39 @@ pub async fn gen_bot(data: Data, args: Args) -> Client {
|
||||
|
||||
|
||||
async fn make_cmd_vec(data: &Data) -> Vec<Cmd> {
|
||||
let mut cmds = vec![];
|
||||
let cfg = get_mutex_data(&data.cfg).await.unwrap();
|
||||
|
||||
// GENERIC
|
||||
cmds.extend([
|
||||
let mut cmds = vec![
|
||||
// GENERIC
|
||||
cmds::help::cmd(),
|
||||
cmds::ping::cmd(),
|
||||
cmds::eight_ball::cmd(),
|
||||
// REDDIT
|
||||
re_cmds::add::cmd(),
|
||||
re_cmds::approve::cmd(),
|
||||
re_cmds::get::cmd(),
|
||||
re_cmds::remove::cmd(),
|
||||
re_cmds::top::cmd(),
|
||||
re_cmds::update::cmd(),
|
||||
re_cmds::vote::cmd(),
|
||||
re_cmds::shorturl::cmd(),
|
||||
// [ADMIN / OWNER]
|
||||
cmds::embed::cmd(),
|
||||
cmds::send::cmd(),
|
||||
cmds::stop::cmd(),
|
||||
cmds::eight_ball::cmd(),
|
||||
cmds::add_server::cmd()
|
||||
]);
|
||||
cmds::add_server::cmd(),
|
||||
cmds::reload_cfg::cmd(),
|
||||
// REDDIT [ADMIN / OWNER]
|
||||
re_cmds::admin_bind::cmd()
|
||||
];
|
||||
let cfg = get_toml_mutex(&data.cfg).await.unwrap();
|
||||
|
||||
// REDDIT
|
||||
if cfg["reddit"]["RESTART_enabled"].as_bool().unwrap() {
|
||||
cmds.extend([
|
||||
re_cmds::add::cmd(),
|
||||
re_cmds::approve::cmd(),
|
||||
re_cmds::get::cmd(),
|
||||
re_cmds::remove::cmd(),
|
||||
re_cmds::top::cmd(),
|
||||
re_cmds::update::cmd(),
|
||||
re_cmds::vote::cmd(),
|
||||
re_cmds::shorturl::cmd(),
|
||||
re_cmds::admin_bind::cmd(),
|
||||
]);
|
||||
}
|
||||
let disabled = cfg["commands"]["disabled_categories"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter_map(|v| v.as_str())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
cmds.extend([
|
||||
cmds::reload_cfg::cmd()
|
||||
]);
|
||||
cmds.retain(|cmd| !disabled.contains(&cmd.category.as_ref().unwrap().as_str()));
|
||||
|
||||
return cmds
|
||||
return cmds;
|
||||
}
|
||||
+1
-1
@@ -86,7 +86,7 @@ struct Data {
|
||||
ball_prompts: [Vec<String>; 2],
|
||||
reddit_data: Mutex<Option<Value>>,
|
||||
discord_data: Mutex<Option<Value>>,
|
||||
cfg: Mutex<Option<Value>>,
|
||||
cfg: Mutex<Option<toml::Value>>,
|
||||
bk_mods: Vec<u64>,
|
||||
args: Args,
|
||||
}
|
||||
|
||||
+2
-2
@@ -3,7 +3,7 @@ import asyncpraw as praw
|
||||
import os
|
||||
from typing import Final
|
||||
from macros import *
|
||||
import json
|
||||
import toml
|
||||
|
||||
|
||||
RE_DATA_POSTS: Final[str] = "posts"
|
||||
@@ -56,7 +56,7 @@ class Bot:
|
||||
return False
|
||||
|
||||
async def update_cfg_str(self, new_cfg: str) -> bool:
|
||||
json_cfg = json.loads(new_cfg)
|
||||
json_cfg = toml.loads(new_cfg)
|
||||
self.sr = await self.r.subreddit(json_cfg[CFG_DATA_RE]["subreddits"])
|
||||
self.fetch_limit = json_cfg[CFG_DATA_RE]["fetch_limit"]
|
||||
return True
|
||||
|
||||
+12
-8
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
import toml
|
||||
import json
|
||||
import time
|
||||
|
||||
@@ -6,7 +7,10 @@ import bot as botPy
|
||||
from macros import *
|
||||
|
||||
|
||||
DATA_PATH = os.path.abspath(os.path.join(os.path.join(os.getcwd(), "data")))
|
||||
DATA_PATH = os.path.join(os.path.join(os.getcwd(), "data"))
|
||||
DB_PATH = os.path.join(DATA_PATH, "db")
|
||||
DEFAULT_PATH = os.path.join(DATA_PATH, "default")
|
||||
CFG_PATH = os.path.join(os.path.join(os.getcwd(), "cfg"))
|
||||
|
||||
|
||||
class PostData:
|
||||
@@ -77,7 +81,7 @@ class PostData:
|
||||
|
||||
|
||||
def read_data(bot: botPy.Bot) -> bool:
|
||||
r_path = os.path.join(DATA_PATH, "re_data.json")
|
||||
r_path = os.path.join(DB_PATH, "re_data.json")
|
||||
|
||||
if os.path.isfile(r_path):
|
||||
bot.data_f = open(r_path, "r+")
|
||||
@@ -113,23 +117,23 @@ def write_data(bot: botPy.Bot) -> bool:
|
||||
|
||||
|
||||
async def read_cfg(bot: botPy.Bot) -> bool:
|
||||
r_path = os.path.join(DATA_PATH, "cfg.json")
|
||||
r_path = os.path.join(CFG_PATH, "cfg.toml")
|
||||
|
||||
if os.path.isfile(r_path):
|
||||
bot.data_f = open(r_path, "r+")
|
||||
|
||||
else:
|
||||
py_print("cfg.json not found, creating new from preset...")
|
||||
with open(os.path.join(DATA_PATH, "cfg_default.json", "r")) as f:
|
||||
data_preset_json = json.load(f)
|
||||
py_print("cfg.toml not found, creating new from preset...")
|
||||
with open(os.path.join(DEFAULT_PATH, "cfg_default.toml", "r")) as f:
|
||||
data_preset_json = toml.load(f)
|
||||
|
||||
with open(r_path, "w") as f:
|
||||
json.dump(data_preset_json, f, indent = 2)
|
||||
toml.dump(data_preset_json, f, indent = 2)
|
||||
|
||||
bot.data_f = open(r_path, "r+")
|
||||
|
||||
data_str = bot.data_f.read()
|
||||
json_data = json.loads(data_str)
|
||||
json_data = toml.loads(data_str)
|
||||
await bot.update_cfg(json_data)
|
||||
|
||||
return True
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use regex::Regex;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{data::get_mutex_data, messages::{make_post_embed, make_removed_embed, send_embed}, Context, Error};
|
||||
use crate::{data::get_toml_mutex, messages::{make_post_embed, make_removed_embed, send_embed}, Context, Error};
|
||||
|
||||
pub fn is_bk_mod(mod_list: Vec<u64>, uid: u64) -> bool {
|
||||
return mod_list.contains(&uid);
|
||||
@@ -35,7 +35,7 @@ pub async fn send_embed_for_removed(ctx: Context<'_>, url: &str, post: &Value) {
|
||||
|
||||
|
||||
pub async fn get_readable_subreddits(ctx: Context<'_>) -> Result<String, Error> {
|
||||
let d = get_mutex_data(&ctx.data().cfg).await?;
|
||||
let d = get_toml_mutex(&ctx.data().cfg).await.unwrap();
|
||||
let sr = d["reddit"]["subreddits"].as_str().ok_or("Item of key \"subreddit\" is not a string type.\nTrace: `get_readable_subreddits -> let sr = ...`")?;
|
||||
let split: Vec<&str> = sr.split("+").collect();
|
||||
let join = split.join(", r/");
|
||||
|
||||
@@ -5,6 +5,7 @@ use crate::{lang, messages::send_msg, re_cmds::generic_fns::to_shorturl, Context
|
||||
slash_command,
|
||||
prefix_command,
|
||||
rename = "re_shorturl",
|
||||
category = "re",
|
||||
required_bot_permissions = "SEND_MESSAGES | VIEW_CHANNEL"
|
||||
)]
|
||||
/// Convert a long reddit URL to a short one. The bot ONLY uses shortURLs when asking for one.
|
||||
|
||||
Reference in New Issue
Block a user