listener obj
This commit is contained in:
parent
2532cc5a3e
commit
ef344402ab
10 changed files with 390 additions and 17 deletions
src/botcore
|
@ -3,23 +3,30 @@
|
|||
use tokio::sync::{mpsc::UnboundedReceiver, Mutex};
|
||||
use twitch_irc::{login::StaticLoginCredentials, message::ServerMessage, SecureTCPTransport, TwitchIRCClient};
|
||||
use dotenv::dotenv;
|
||||
use std::env;
|
||||
use std::{env, sync::Arc};
|
||||
|
||||
use super::bot_objects::listener::Listener;
|
||||
|
||||
|
||||
|
||||
/// Twitch chat bot
|
||||
pub struct Bot {
|
||||
pub struct Bot
|
||||
{
|
||||
/// Prefix for commands
|
||||
_prefix: char,
|
||||
/// inbound chat msg stream
|
||||
incoming_msgs: Mutex<UnboundedReceiver<ServerMessage>>,
|
||||
/// outbound chat client msg stream
|
||||
client: TwitchIRCClient<SecureTCPTransport,StaticLoginCredentials>,
|
||||
pub client: TwitchIRCClient<SecureTCPTransport,StaticLoginCredentials>,
|
||||
/// joined channels
|
||||
botchannels: Vec<String>,
|
||||
/// listeners
|
||||
listeners: Vec<Listener>,
|
||||
}
|
||||
|
||||
|
||||
impl Bot {
|
||||
|
||||
impl Bot
|
||||
{
|
||||
/// Creates a new `Bot` using env variables
|
||||
///
|
||||
/// Be sure the following is defined in an `.env` file
|
||||
|
@ -79,6 +86,7 @@ impl Bot {
|
|||
incoming_msgs : Mutex::new(incoming_messages),
|
||||
client,
|
||||
botchannels : botchannels_all,
|
||||
listeners : vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -88,17 +96,35 @@ impl Bot {
|
|||
for chnl in &self.botchannels {
|
||||
self.client.join(chnl.to_owned()).unwrap();
|
||||
}
|
||||
|
||||
|
||||
let bot = Arc::new(self);
|
||||
|
||||
let join_handle = tokio::spawn(async move {
|
||||
let mut in_msgs_lock = self.incoming_msgs.lock().await;
|
||||
|
||||
let mut in_msgs_lock = bot.incoming_msgs.lock().await;
|
||||
|
||||
while let Some(message) = in_msgs_lock.recv().await {
|
||||
//sprintln!("Received message: {:?}", message);
|
||||
dbg!("Received message: {:?}", message);
|
||||
// dbg!("Received message: {:?}", message.clone());
|
||||
|
||||
for listener in &(*bot).listeners {
|
||||
|
||||
let a = listener.clone();
|
||||
if a.cond_triggered(bot.clone(),message.clone()) {
|
||||
|
||||
let _ = listener.execute_fn(bot.clone(),message.clone()).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
drop(in_msgs_lock);
|
||||
});
|
||||
|
||||
join_handle.await.unwrap();
|
||||
}
|
||||
|
||||
/// Loads a `Listener` into the bot
|
||||
pub fn load_listener(&mut self,l : Listener) {
|
||||
self.listeners.push(l);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue