diff --git a/settings.json b/settings.json index 1662033..6b3d635 100644 --- a/settings.json +++ b/settings.json @@ -1 +1,79 @@ -{"channels":[1232064459737010247]} \ No newline at end of file +{ + "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 + } + ] +} \ No newline at end of file diff --git a/src/command/util.rs b/src/command/util.rs index db8d22f..6448363 100644 --- a/src/command/util.rs +++ b/src/command/util.rs @@ -37,7 +37,7 @@ pub async fn add_channel( if let Some(channel_ok) = channel { let config = &mut ctx.data().config_manager.lock().await; let channel_id = { u64::from(channel_ok.id()) }; - config.channels.push(channel_id); + config.channels.push(channel_ok); config.store().unwrap(); ctx.say(format!( "Successfully added <#{}> to the channel registry.", @@ -55,9 +55,14 @@ pub async fn add_channel( #[poise::command(slash_command)] pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> { let config = &mut ctx.data().config_manager.lock().await; + let mut channel_ids: Vec = vec![]; + config + .channels + .iter() + .for_each(|c| channel_ids.push(u64::from(c.id()))); ctx.say(format!( "Current channel IDs in registry: {:#?}", - config.channels + channel_ids )) .await?; info!("Executed command `list_channels` successfully"); diff --git a/src/main.rs b/src/main.rs index fba60af..123df3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use tokio::sync::Mutex; use dotenv::dotenv; use poise::serenity_prelude as serenity; +use serenity::Channel; extern crate pretty_env_logger; #[macro_use] @@ -25,11 +26,13 @@ struct Data { type Error = Box; type Context<'a> = poise::Context<'a, Data, Error>; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Default)] struct Settings { - channels: Vec, + channels: Vec, } +const SETTINGS_PATH: &str = "settings.json"; + #[tokio::main] async fn main() { // Get secure env vars from .env file @@ -44,8 +47,10 @@ async fn main() { // Configure persistent options let config_manager: Arc>> = 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 let framework = poise::Framework::builder() diff --git a/src/settings.rs b/src/settings.rs index c90bc4e..d0b8f66 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -5,12 +5,12 @@ use serde::de::Deserialize; use serde::ser::Serialize; /// A utility structure to manage a settings structure. -pub struct SettingsManager Deserialize<'a>> { +pub struct SettingsManager Deserialize<'a>> { internal: T, path: String, } -impl Deserialize<'a>> SettingsManager { +impl Deserialize<'a>> SettingsManager { /// Instantiate new self if the path contains a valid serialization of /// the settings structure. pub fn load(path: &str) -> Option { @@ -23,7 +23,10 @@ impl Deserialize<'a>> SettingsManager { 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<()> { let mut file = std::fs::File::open(self.path.clone()).ok()?; let mut data = String::new(); @@ -49,7 +52,7 @@ impl Deserialize<'a>> SettingsManager { } } -impl Deserialize<'a>> Deref for SettingsManager { +impl Deserialize<'a>> Deref for SettingsManager { type Target = T; fn deref(&self) -> &Self::Target { @@ -57,7 +60,7 @@ impl Deserialize<'a>> Deref for SettingsManager { } } -impl Deserialize<'a>> DerefMut for SettingsManager { +impl Deserialize<'a>> DerefMut for SettingsManager { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.internal }