fixed some stuff

This commit is contained in:
shibedrill 2024-04-22 16:55:26 -04:00
parent 8262e8e09a
commit 054ab7f437
3 changed files with 21 additions and 13 deletions

View File

@ -36,12 +36,14 @@ pub async fn add_channel(
) -> Result<(), Error> { ) -> Result<(), Error> {
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 = { let channel_id = { u64::from(channel_ok.id()) };
u64::from(channel_ok.id())
};
config.channels.push(channel_id); config.channels.push(channel_id);
config.store().unwrap(); config.store().unwrap();
ctx.say(format!("Successfully added <#{}> to the channel registry.", channel_id)).await?; ctx.say(format!(
"Successfully added <#{}> to the channel registry.",
channel_id
))
.await?;
info!("Executed command `add_channel` successfully"); info!("Executed command `add_channel` successfully");
} else { } else {
ctx.say("Channel with supplied ID was not found.").await?; ctx.say("Channel with supplied ID was not found.").await?;
@ -53,7 +55,11 @@ 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;
ctx.say(format!("Current channel IDs in registry: {:#?}", config.channels)).await?; ctx.say(format!(
"Current channel IDs in registry: {:#?}",
config.channels
))
.await?;
info!("Executed command `list_channels` successfully"); info!("Executed command `list_channels` successfully");
Ok(()) Ok(())
} }

View File

@ -1,5 +1,4 @@
use std::sync::Arc;
use std::sync::{Arc};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use dotenv::dotenv; use dotenv::dotenv;
@ -13,12 +12,15 @@ extern crate log;
use serde::*; use serde::*;
mod command; mod command;
use crate::{command::{fun::*, util::*}, settings::SettingsManager}; use crate::{
command::{fun::*, util::*},
settings::SettingsManager,
};
mod settings; mod settings;
struct Data { struct Data {
config_manager: Arc<Mutex<SettingsManager<Settings>>> config_manager: Arc<Mutex<SettingsManager<Settings>>>,
} // User data, which is stored and accessible in all command invocations } // User data, which is stored and accessible in all command invocations
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>;

View File

@ -36,7 +36,7 @@ impl<T: Serialize + for<'a> Deserialize<'a>> SettingsManager<T> {
/// Serialize settings structure to the stored path. Returns None if /// Serialize settings structure to the stored path. Returns None if
/// unsuccessful. /// unsuccessful.
pub fn store(&self) -> Option<()> { pub fn store(&self) -> Option<()> {
let data = serde_json::to_string(&self.internal).ok()?; let data = serde_json::to_string_pretty(&self.internal).ok()?;
let mut file = std::fs::File::create(&self.path).ok()?; let mut file = std::fs::File::create(&self.path).ok()?;
let _ = file.write(data.as_bytes()); let _ = file.write(data.as_bytes());
Some(()) Some(())