This commit is contained in:
shibedrill 2024-04-22 16:51:03 -04:00
parent dc1311bb15
commit 8262e8e09a
6 changed files with 127 additions and 6 deletions

2
Cargo.lock generated
View File

@ -312,6 +312,8 @@ dependencies = [
"poise",
"pretty_env_logger",
"rand",
"serde",
"serde_json",
"tokio",
]

View File

@ -11,4 +11,6 @@ log = "0.4.21"
poise = "0.6.1"
pretty_env_logger = "0.5.0"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
rand = "0.8.5"
rand = "0.8.5"
serde = { version = "1.0.198", features = ["serde_derive"] }
serde_json = "1.0.116"

1
settings.json Normal file
View File

@ -0,0 +1 @@
{"channels":[1232064459737010247]}

View File

@ -27,3 +27,33 @@ pub async fn info(ctx: Context<'_>) -> Result<(), Error> {
info!("Executed command `info` successfully");
Ok(())
}
/// Add information to the shared settings
#[poise::command(slash_command)]
pub async fn add_channel(
ctx: Context<'_>,
#[description = "Selected channel"] channel: Option<serenity::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())
};
config.channels.push(channel_id);
config.store().unwrap();
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?;
error!("Failed to execute command `add_channel`.");
}
Ok(())
}
#[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?;
info!("Executed command `list_channels` successfully");
Ok(())
}

View File

@ -1,3 +1,7 @@
use std::sync::{Arc};
use tokio::sync::Mutex;
use dotenv::dotenv;
use poise::serenity_prelude as serenity;
@ -6,13 +10,24 @@ extern crate pretty_env_logger;
#[macro_use]
extern crate log;
mod command;
use crate::command::{fun::*, util::*};
use serde::*;
struct Data {} // User data, which is stored and accessible in all command invocations
mod command;
use crate::{command::{fun::*, util::*}, settings::SettingsManager};
mod settings;
struct Data {
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>;
#[derive(Debug, Serialize, Deserialize)]
struct Settings {
channels: Vec<u64>,
}
#[tokio::main]
async fn main() {
// Get secure env vars from .env file
@ -25,16 +40,21 @@ async fn main() {
// Initialize logging
pretty_env_logger::init();
// Configure persistent options
let config = Settings { channels: vec![] };
let config_manager = Arc::new(Mutex::new(SettingsManager::manage("settings.json", config)));
config_manager.lock().await.update();
// Set up framework
let framework = poise::Framework::builder()
.options(poise::FrameworkOptions {
commands: vec![age(), info(), bleat()],
commands: vec![age(), info(), bleat(), add_channel(), list_channels()],
..Default::default()
})
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {})
Ok(Data {config_manager})
})
})
.build();

66
src/settings.rs Normal file
View File

@ -0,0 +1,66 @@
#![allow(dead_code)]
use std::io::{Read, Write};
use std::ops::{Deref, DerefMut};
use serde::de::Deserialize;
use serde::ser::Serialize;
/// A utility structure to manage a settings structure.
pub struct SettingsManager<T: Serialize + for<'a> Deserialize<'a>> {
internal: T,
path: String,
}
impl<T: Serialize + for<'a> Deserialize<'a>> SettingsManager<T> {
/// Instantiate new self if the path contains a valid serialization of
/// the settings structure.
pub fn load(path: &str) -> Option<Self> {
let mut file = std::fs::File::open(path).ok()?;
let mut data = String::new();
file.read_to_string(&mut data).ok()?;
let settings = serde_json::from_str(&data).ok()?;
Some(Self {
internal: settings,
path: String::from(path),
})
}
/// Update the data stored in the settings.
pub fn update(&mut self) -> Option<()> {
let mut file = std::fs::File::open(self.path.clone()).ok()?;
let mut data = String::new();
file.read_to_string(&mut data).ok()?;
self.internal = serde_json::from_str(&data).ok()?;
Some(())
}
/// 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 mut file = std::fs::File::create(&self.path).ok()?;
let _ = file.write(data.as_bytes());
Some(())
}
/// Create a new manager, passing in the path, and a structure to manage.
/// We cannot initialize a settings manager without fully initialized settings.
pub fn manage(path: &str, intake: T) -> Self {
Self {
internal: intake,
path: path.to_string(),
}
}
}
impl<T: Serialize + for<'a> Deserialize<'a>> Deref for SettingsManager<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.internal
}
}
impl<T: Serialize + for<'a> Deserialize<'a>> DerefMut for SettingsManager<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.internal
}
}