refactor: load_configuration

This commit is contained in:
oSumAtrIX 2022-07-06 22:31:33 +02:00
parent 8fb0ab8835
commit 57693d39ed
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
2 changed files with 10 additions and 8 deletions

View File

@ -38,6 +38,10 @@ fn contains_match(strings: &Vec<String>, text: &String) -> bool {
strings.iter().any(|regex| Regex::new(regex).unwrap().is_match(&text)) strings.iter().any(|regex| Regex::new(regex).unwrap().is_match(&text))
} }
fn load_configuration() -> BotConfiguration {
BotConfiguration::load().expect("Failed to load configuration")
}
#[async_trait] #[async_trait]
impl EventHandler for Handler { impl EventHandler for Handler {
async fn interaction_create(&self, ctx: Context, interaction: Interaction) { async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
@ -72,8 +76,7 @@ impl EventHandler for Handler {
if permission_granted { if permission_granted {
trace!("{:?} reloading configuration.", command.user); trace!("{:?} reloading configuration.", command.user);
let new_config = let new_config = load_configuration();
BotConfiguration::load().expect("Could not load configuration.");
configuration.administrators = new_config.administrators; configuration.administrators = new_config.administrators;
configuration.message_responders = new_config.message_responders; configuration.message_responders = new_config.message_responders;
@ -105,12 +108,11 @@ impl EventHandler for Handler {
} }
async fn message(&self, ctx: Context, msg: Message) { async fn message(&self, ctx: Context, msg: Message) {
trace!("Received message: {}", msg.content);
if msg.guild_id.is_none() || msg.author.bot { if msg.guild_id.is_none() || msg.author.bot {
return; return;
} }
trace!("Received message: {}", msg.content);
if let Some(response) = if let Some(response) =
get_configuration_lock(&ctx).await.read().await.message_responders.iter().find( get_configuration_lock(&ctx).await.read().await.message_responders.iter().find(
|&responder| { |&responder| {
@ -156,6 +158,7 @@ impl EventHandler for Handler {
let configuration_lock = get_configuration_lock(&ctx).await; let configuration_lock = get_configuration_lock(&ctx).await;
let configuration = configuration_lock.read().await; let configuration = configuration_lock.read().await;
if let Some(introducer) = &configuration.thread_introductions.iter().find(|introducer| { if let Some(introducer) = &configuration.thread_introductions.iter().find(|introducer| {
introducer.channels.iter().any(|channel_id| *channel_id == thread.parent_id.unwrap().0) 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)) .map(|()| log::set_max_level(LevelFilter::Warn))
.expect("Could not set logger."); .expect("Could not set logger.");
let configuration = BotConfiguration::load().expect("Failed to load configuration"); let configuration = load_configuration();
let mut client = Client::builder( let mut client = Client::builder(
&configuration.discord_authorization_token, &configuration.discord_authorization_token,
@ -192,9 +195,7 @@ async fn main() {
.await .await
.expect("Failed to create client"); .expect("Failed to create client");
client.data.write().await.insert::<Configuration>(Arc::new(RwLock::new( client.data.write().await.insert::<Configuration>(Arc::new(RwLock::new(configuration)));
BotConfiguration::load().expect("Failed to load configuration"),
)));
if let Err(why) = client.start().await { if let Err(why) = client.start().await {
error!("{:?}", why); error!("{:?}", why);

1
submodules/serenity Submodule

@ -0,0 +1 @@
Subproject commit c5cc876484232383c8cd1a7ce71c60624b86a75a