From 57693d39ed2b50de9cc8cf69e24a553a0f4bfbf8 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Wed, 6 Jul 2022 22:31:33 +0200 Subject: [PATCH] refactor: `load_configuration` --- src/main.rs | 17 +++++++++-------- submodules/serenity | 1 + 2 files changed, 10 insertions(+), 8 deletions(-) create mode 160000 submodules/serenity diff --git a/src/main.rs b/src/main.rs index e667614..ebe25e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,10 @@ fn contains_match(strings: &Vec, text: &String) -> bool { strings.iter().any(|regex| Regex::new(regex).unwrap().is_match(&text)) } +fn load_configuration() -> BotConfiguration { + BotConfiguration::load().expect("Failed to load configuration") +} + #[async_trait] impl EventHandler for Handler { async fn interaction_create(&self, ctx: Context, interaction: Interaction) { @@ -72,8 +76,7 @@ impl EventHandler for Handler { if permission_granted { trace!("{:?} reloading configuration.", command.user); - let new_config = - BotConfiguration::load().expect("Could not load configuration."); + let new_config = load_configuration(); configuration.administrators = new_config.administrators; configuration.message_responders = new_config.message_responders; @@ -105,12 +108,11 @@ impl EventHandler for Handler { } async fn message(&self, ctx: Context, msg: Message) { + trace!("Received message: {}", msg.content); if msg.guild_id.is_none() || msg.author.bot { return; } - trace!("Received message: {}", msg.content); - if let Some(response) = get_configuration_lock(&ctx).await.read().await.message_responders.iter().find( |&responder| { @@ -156,6 +158,7 @@ impl EventHandler for Handler { let configuration_lock = get_configuration_lock(&ctx).await; let configuration = configuration_lock.read().await; + if let Some(introducer) = &configuration.thread_introductions.iter().find(|introducer| { introducer.channels.iter().any(|channel_id| *channel_id == thread.parent_id.unwrap().0) }) { @@ -182,7 +185,7 @@ async fn main() { .map(|()| log::set_max_level(LevelFilter::Warn)) .expect("Could not set logger."); - let configuration = BotConfiguration::load().expect("Failed to load configuration"); + let configuration = load_configuration(); let mut client = Client::builder( &configuration.discord_authorization_token, @@ -192,9 +195,7 @@ async fn main() { .await .expect("Failed to create client"); - client.data.write().await.insert::(Arc::new(RwLock::new( - BotConfiguration::load().expect("Failed to load configuration"), - ))); + client.data.write().await.insert::(Arc::new(RwLock::new(configuration))); if let Err(why) = client.start().await { error!("{:?}", why); diff --git a/submodules/serenity b/submodules/serenity new file mode 160000 index 0000000..c5cc876 --- /dev/null +++ b/submodules/serenity @@ -0,0 +1 @@ +Subproject commit c5cc876484232383c8cd1a7ce71c60624b86a75a