@@ -8,9 +8,11 @@ use regex::{Regex, RegexBuilder};
88use serde:: { Deserialize , Serialize } ;
99use sqlx:: Type ;
1010use std:: collections:: HashMap ;
11+ use std:: fs:: read;
1112use std:: pin:: Pin ;
1213use std:: sync:: Arc ;
1314use std:: time:: Instant ;
15+ use tokio:: sync:: RwLock ;
1416// ChatMessage
1517
1618#[ allow( dead_code) ]
@@ -105,18 +107,17 @@ static TEXT_COMMAND_CALLBACK: TriggerCallback = |app_state, trigger_id, _chat_me
105107// we also can't just create an emtpy version of ourselves, and fill the rest in later, because then our users would try to modify non existing commands
106108#[ derive( Default ) ]
107109pub struct CommandExecutorService {
108- triggers : Vec < CommandTrigger > ,
110+ triggers : RwLock < Vec < CommandTrigger > > ,
109111 cooldown_service : CooldownService ,
110112}
111113
112114impl CommandExecutorService {
113- pub ( crate ) fn remove_command ( & self , command_id : & TriggerId ) {
114- //TODO
115- // self.triggers.retain(|c| c.id.as_ref() != command_id);
115+ pub ( crate ) async fn remove_command ( & self , command_id : & TriggerId ) {
116+ self . triggers . write ( ) . await . retain ( |c| c. id . as_ref ( ) != command_id) ;
116117 }
117118
118- pub ( crate ) fn upsert_command ( & self , command : & Command ) -> Result < ( ) , regex:: Error > {
119- self . remove_command ( command. id . as_ref ( ) ) ;
119+ pub ( crate ) async fn upsert_command ( & self , command : & Command ) -> Result < ( ) , regex:: Error > {
120+ self . remove_command ( command. id . as_ref ( ) ) . await ;
120121 let global_cooldown = match command. global_cooldown_type {
121122 CooldownType :: SECONDS => ChatCooldown :: SECONDS ( command. global_cooldown_amount ) ,
122123 CooldownType :: MESSAGES => ChatCooldown :: MESSAGES ( command. global_cooldown_amount )
@@ -125,19 +126,18 @@ impl CommandExecutorService {
125126 CooldownType :: SECONDS => ChatCooldown :: SECONDS ( command. user_cooldown_amount ) ,
126127 CooldownType :: MESSAGES => ChatCooldown :: MESSAGES ( command. user_cooldown_amount )
127128 } ;
128- //TODO
129- // self.triggers.push(CommandTrigger {
130- // id: command.id.clone().into_boxed_str(),
131- // global_cooldown,
132- // user_cooldown,
133- // permission: command.permission,
134- // patterns: Self::convert_patterns(command.patterns.as_ref())?,
135- // callback: TEXT_COMMAND_CALLBACK
136- // });
129+ self . triggers . write ( ) . await . push ( CommandTrigger {
130+ id : command. id . clone ( ) . into_boxed_str ( ) ,
131+ global_cooldown,
132+ user_cooldown,
133+ permission : command. permission ,
134+ patterns : Self :: convert_patterns ( command. patterns . as_ref ( ) ) ?,
135+ callback : TEXT_COMMAND_CALLBACK
136+ } ) ;
137137 Ok ( ( ) )
138138 }
139139
140- pub ( crate ) fn refresh_patterns ( & self , prod_db : & ProdDB , trigger_id : & TriggerId ) {
140+ pub ( crate ) async fn refresh_patterns ( & self , prod_db : & ProdDB , trigger_id : & TriggerId ) {
141141 todo ! ( )
142142 }
143143
@@ -156,7 +156,7 @@ impl CommandExecutorService {
156156 }
157157
158158 pub async fn process_chat_message ( & self , app_state : Arc < FullState > , message : ChatMessage ) {
159- for trigger in self . triggers . iter ( ) {
159+ for trigger in self . triggers . read ( ) . await . iter ( ) {
160160 self . execute_trigger_if_matching ( app_state. clone ( ) , trigger, message. clone ( ) ) . await
161161 }
162162 }
0 commit comments