From 0053ccbb31c3b87285bf38ee3eda3308c67ad707 Mon Sep 17 00:00:00 2001 From: Ashelyn Rose Date: Thu, 3 Oct 2024 03:18:26 -0600 Subject: Refactor bot into separate client + gateway --- src/system/bot/mod.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/system/bot/mod.rs (limited to 'src/system/bot/mod.rs') diff --git a/src/system/bot/mod.rs b/src/system/bot/mod.rs new file mode 100644 index 0000000..6bf8d78 --- /dev/null +++ b/src/system/bot/mod.rs @@ -0,0 +1,80 @@ +mod client; +mod gateway; + +use std::sync::Arc; +use tokio::sync::mpsc::Sender; +use tokio::sync::RwLock; +use twilight_http::error::Error as TwiError; + +pub use super::types::*; +pub use client::MessageDuplicateError; +use gateway::Gateway; +use client::Client; + +#[derive(Clone)] +pub struct BotConfig { + pub member_id: MemberId, + pub reference_user_id: UserId, + pub discord_token: String, + pub last_status: Status, + pub message_handler: Option>, + pub system_handler: Option>, +} + +pub struct Bot { + bot_conf: Arc>, + gateway: Gateway, + client: Client, +} + +impl Bot { + pub fn new( + member_id: MemberId, + config: &crate::config::Member, + reference_user_id: UserId, + ) -> Self { + let bot_conf = Arc::new(RwLock::new(BotConfig { + member_id, + reference_user_id, + discord_token: config.discord_token.clone(), + last_status: Status::Online, + message_handler: None, + system_handler: None, + })); + + Self { + gateway: Gateway::new(&config.discord_token, &bot_conf), + client: Client::new(&config.discord_token, &bot_conf), + bot_conf, + } + } + + pub async fn set_message_handler(&mut self, handler: Sender) { + self.bot_conf.write().await.message_handler = Some(handler); + } + + pub async fn set_system_handler(&mut self, handler: Sender) { + self.bot_conf.write().await.system_handler = Some(handler); + } + + pub async fn set_status(&self, status: Status) { + self.gateway.set_status(status).await; + } + + pub fn start(&self) { + self.gateway.start_listening() + } + + pub async fn refetch_message(&self, message_id: MessageId, channel_id: ChannelId) { + self.client.refetch_message(message_id, channel_id).await; + } + + pub async fn delete_message(&self, channel_id: ChannelId, message_id: MessageId) -> Result<(), TwiError> { + self.client.delete_message(channel_id, message_id).await + } + + pub async fn duplicate_message(&self, message_id: &TwiMessage, content: &str) -> Result { + self.client.duplicate_message(message_id, content).await + } +} + -- cgit 1.4.1