fixed MORE stuff

This commit is contained in:
shibedrill 2024-04-22 17:25:28 -04:00
parent 5f69e460cd
commit 1e775d8197
4 changed files with 102 additions and 11 deletions

View File

@ -1 +1,79 @@
{"channels":[1232064459737010247]} {
"channels": [
{
"id": "1030714032900030466",
"bitrate": null,
"parent_id": "1030714032900030464",
"guild_id": "1030714031880818729",
"type": 0,
"owner_id": null,
"last_message_id": "1232079362921332746",
"last_pin_timestamp": null,
"name": "general",
"permission_overwrites": [
{
"allow": "49152",
"deny": "0",
"id": "1030714031880818729",
"type": 0
}
],
"position": 0,
"topic": null,
"user_limit": null,
"nsfw": false,
"rate_limit_per_user": 0,
"rtc_region": null,
"video_quality_mode": null,
"message_count": null,
"member_count": null,
"thread_metadata": null,
"member": null,
"default_auto_archive_duration": null,
"permissions": null,
"flags": 0,
"total_message_sent": null,
"available_tags": [],
"applied_tags": [],
"default_reaction_emoji": null,
"default_thread_rate_limit_per_user": null,
"status": null,
"default_sort_order": null,
"default_forum_layout": null
},
{
"id": "1232064459737010247",
"bitrate": null,
"parent_id": "1030714032900030464",
"guild_id": "1030714031880818729",
"type": 0,
"owner_id": null,
"last_message_id": null,
"last_pin_timestamp": null,
"name": "asdf",
"permission_overwrites": [],
"position": 2,
"topic": null,
"user_limit": null,
"nsfw": false,
"rate_limit_per_user": 0,
"rtc_region": null,
"video_quality_mode": null,
"message_count": null,
"member_count": null,
"thread_metadata": null,
"member": null,
"default_auto_archive_duration": null,
"permissions": null,
"flags": 0,
"total_message_sent": null,
"available_tags": [],
"applied_tags": [],
"default_reaction_emoji": null,
"default_thread_rate_limit_per_user": null,
"status": null,
"default_sort_order": null,
"default_forum_layout": null
}
]
}

View File

@ -37,7 +37,7 @@ pub async fn add_channel(
if let Some(channel_ok) = channel { if let Some(channel_ok) = channel {
let config = &mut ctx.data().config_manager.lock().await; let config = &mut ctx.data().config_manager.lock().await;
let channel_id = { u64::from(channel_ok.id()) }; let channel_id = { u64::from(channel_ok.id()) };
config.channels.push(channel_id); config.channels.push(channel_ok);
config.store().unwrap(); config.store().unwrap();
ctx.say(format!( ctx.say(format!(
"Successfully added <#{}> to the channel registry.", "Successfully added <#{}> to the channel registry.",
@ -55,9 +55,14 @@ pub async fn add_channel(
#[poise::command(slash_command)] #[poise::command(slash_command)]
pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> { pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> {
let config = &mut ctx.data().config_manager.lock().await; let config = &mut ctx.data().config_manager.lock().await;
let mut channel_ids: Vec<u64> = vec![];
config
.channels
.iter()
.for_each(|c| channel_ids.push(u64::from(c.id())));
ctx.say(format!( ctx.say(format!(
"Current channel IDs in registry: {:#?}", "Current channel IDs in registry: {:#?}",
config.channels channel_ids
)) ))
.await?; .await?;
info!("Executed command `list_channels` successfully"); info!("Executed command `list_channels` successfully");

View File

@ -4,6 +4,7 @@ use tokio::sync::Mutex;
use dotenv::dotenv; use dotenv::dotenv;
use poise::serenity_prelude as serenity; use poise::serenity_prelude as serenity;
use serenity::Channel;
extern crate pretty_env_logger; extern crate pretty_env_logger;
#[macro_use] #[macro_use]
@ -25,11 +26,13 @@ struct Data {
type Error = Box<dyn std::error::Error + Send + Sync>; type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>; type Context<'a> = poise::Context<'a, Data, Error>;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize, Default)]
struct Settings { struct Settings {
channels: Vec<u64>, channels: Vec<Channel>,
} }
const SETTINGS_PATH: &str = "settings.json";
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// Get secure env vars from .env file // Get secure env vars from .env file
@ -44,8 +47,10 @@ async fn main() {
// Configure persistent options // Configure persistent options
let config_manager: Arc<Mutex<SettingsManager<Settings>>> = Arc::new(Mutex::new( let config_manager: Arc<Mutex<SettingsManager<Settings>>> = Arc::new(Mutex::new(
SettingsManager::load("settings.json").expect("Unable to load config!"), SettingsManager::load(SETTINGS_PATH)
.unwrap_or(SettingsManager::manage(SETTINGS_PATH, Settings::default())),
)); ));
config_manager.lock().await.store();
// Set up framework // Set up framework
let framework = poise::Framework::builder() let framework = poise::Framework::builder()

View File

@ -5,12 +5,12 @@ use serde::de::Deserialize;
use serde::ser::Serialize; use serde::ser::Serialize;
/// A utility structure to manage a settings structure. /// A utility structure to manage a settings structure.
pub struct SettingsManager<T: Serialize + for<'a> Deserialize<'a>> { pub struct SettingsManager<T: Default + Serialize + for<'a> Deserialize<'a>> {
internal: T, internal: T,
path: String, path: String,
} }
impl<T: Serialize + for<'a> Deserialize<'a>> SettingsManager<T> { impl<T: Default + Serialize + for<'a> Deserialize<'a>> SettingsManager<T> {
/// Instantiate new self if the path contains a valid serialization of /// Instantiate new self if the path contains a valid serialization of
/// the settings structure. /// the settings structure.
pub fn load(path: &str) -> Option<Self> { pub fn load(path: &str) -> Option<Self> {
@ -23,7 +23,10 @@ impl<T: Serialize + for<'a> Deserialize<'a>> SettingsManager<T> {
path: String::from(path), path: String::from(path),
}) })
} }
/// Update the data stored in the settings. /// Update the data stored in the settings, if it has been modified on the
/// disk but not in memory. Because this is a stupid method, it will most
/// likely go unused by most.
#[allow(dead_code)]
pub fn update(&mut self) -> Option<()> { pub fn update(&mut self) -> Option<()> {
let mut file = std::fs::File::open(self.path.clone()).ok()?; let mut file = std::fs::File::open(self.path.clone()).ok()?;
let mut data = String::new(); let mut data = String::new();
@ -49,7 +52,7 @@ impl<T: Serialize + for<'a> Deserialize<'a>> SettingsManager<T> {
} }
} }
impl<T: Serialize + for<'a> Deserialize<'a>> Deref for SettingsManager<T> { impl<T: Default + Serialize + for<'a> Deserialize<'a>> Deref for SettingsManager<T> {
type Target = T; type Target = T;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
@ -57,7 +60,7 @@ impl<T: Serialize + for<'a> Deserialize<'a>> Deref for SettingsManager<T> {
} }
} }
impl<T: Serialize + for<'a> Deserialize<'a>> DerefMut for SettingsManager<T> { impl<T: Default + Serialize + for<'a> Deserialize<'a>> DerefMut for SettingsManager<T> {
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.internal &mut self.internal
} }