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> {
if let Some(channel_ok) = channel {
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.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");
} else {
ctx.say("Channel with supplied ID was not found.").await?;
@ -53,7 +55,11 @@ 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;
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");
Ok(())
}
}

View File

@ -1,5 +1,4 @@
use std::sync::{Arc};
use std::sync::Arc;
use tokio::sync::Mutex;
use dotenv::dotenv;
@ -13,12 +12,15 @@ extern crate log;
use serde::*;
mod command;
use crate::{command::{fun::*, util::*}, settings::SettingsManager};
use crate::{
command::{fun::*, util::*},
settings::SettingsManager,
};
mod settings;
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
type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;
@ -54,7 +56,7 @@ async fn main() {
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {config_manager})
Ok(Data { config_manager })
})
})
.build();

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
/// unsuccessful.
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 _ = file.write(data.as_bytes());
Some(())
@ -63,4 +63,4 @@ impl<T: Serialize + for<'a> Deserialize<'a>> DerefMut for SettingsManager<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.internal
}
}
}