fixed some stuff
This commit is contained in:
parent
8262e8e09a
commit
054ab7f437
@ -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(())
|
||||||
}
|
}
|
||||||
|
12
src/main.rs
12
src/main.rs
@ -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>;
|
||||||
@ -54,7 +56,7 @@ async fn main() {
|
|||||||
.setup(|ctx, _ready, framework| {
|
.setup(|ctx, _ready, framework| {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
||||||
Ok(Data {config_manager})
|
Ok(Data { config_manager })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
|
@ -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(())
|
||||||
@ -63,4 +63,4 @@ impl<T: 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user