Skip to content

Commit 43f7485

Browse files
committed
refactor(api): add Ctx to BotState to be able to use MessageBuilder in api endpoints
1 parent 8a0665e commit 43f7485

4 files changed

Lines changed: 51 additions & 11 deletions

File tree

rustmail/src/api/handler/externals/tickets/create.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub async fn handle_external_ticket_create(
3030

3131
let user_id = UserId::new(user_id_u64);
3232

33-
let (config, db_pool, bot_http, command_tx) = {
33+
let (mut config, db_pool, bot_http, command_tx) = {
3434
let state = bot_state.lock().await;
3535
let config = state
3636
.config
@@ -61,6 +61,8 @@ pub async fn handle_external_ticket_create(
6161
(config, db_pool, bot_http, command_tx)
6262
};
6363

64+
config.db_pool = Some(db_pool.clone());
65+
6466
println!(
6567
"API Key #{} creating ticket for Discord ID: {}",
6668
api_key.id, user_id_u64
@@ -187,14 +189,34 @@ pub async fn handle_external_ticket_create(
187189

188190
let open_thread_message = get_user_recap(user_id, &username, &member_join_date, &logs_info);
189191

190-
if let Err(e) = channel.id.say(&bot_http, open_thread_message).await {
191-
eprintln!("Failed to send welcome message to thread: {}", e);
192+
let ctx = {
193+
let state = bot_state.lock().await;
194+
let ctx_lock = state.bot_context.read().await;
195+
ctx_lock
196+
.as_ref()
197+
.ok_or((
198+
StatusCode::INTERNAL_SERVER_ERROR,
199+
"Bot context not available".to_string(),
200+
))?
201+
.clone()
202+
};
203+
204+
if let Err(e) = MessageBuilder::system_message(&ctx, &config)
205+
.to_channel(channel.id)
206+
.content(open_thread_message)
207+
.send(true)
208+
.await
209+
{
210+
eprintln!("Failed to send message to channel via MessageBuilder: {:?}", e);
192211
}
193212

194-
if let Ok(dm_channel) = user.create_dm_channel(&bot_http).await {
195-
if let Err(e) = dm_channel.say(&bot_http, &config.bot.welcome_message).await {
196-
eprintln!("Failed to send DM to user: {}", e);
197-
}
213+
if let Err(e) = MessageBuilder::system_message(&ctx, &config)
214+
.content(&config.bot.welcome_message)
215+
.to_user(user_id)
216+
.send(true)
217+
.await
218+
{
219+
eprintln!("Failed to send DM via MessageBuilder: {:?}", e);
198220
}
199221

200222
println!(

rustmail/src/bot.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub async fn init_bot_state() -> Arc<Mutex<BotState>> {
3535
db_pool: Some(pool),
3636
command_tx: command_tx.clone(),
3737
bot_http: None,
38+
bot_context: Arc::new(tokio::sync::RwLock::new(None)),
3839
};
3940

4041
Arc::new(Mutex::new(bot_state))
@@ -147,6 +148,7 @@ pub async fn run_bot(
147148
&config,
148149
registry.clone(),
149150
shutdown_rx.clone(),
151+
bot_state.clone(),
150152
))
151153
.event_handler(
152154
GuildMessagesHandler::new(

rustmail/src/handlers/ready_handler.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::prelude::commands::*;
33
use crate::prelude::config::*;
44
use crate::prelude::features::*;
55
use crate::prelude::modules::*;
6+
use crate::prelude::types::*;
67
use serenity::all::{ActivityData, CreateCommand, GuildId};
78
use serenity::futures::future::join_all;
89
use serenity::{
@@ -12,22 +13,29 @@ use serenity::{
1213
use sqlx::SqlitePool;
1314
use std::sync::Arc;
1415
use std::time::Duration;
15-
use tokio::sync::watch::Receiver;
16+
use tokio::sync::{watch::Receiver, Mutex};
1617
use tokio::time::interval;
1718

1819
#[derive(Clone)]
1920
pub struct ReadyHandler {
2021
pub config: Config,
2122
pub registry: Arc<CommandRegistry>,
2223
pub shutdown: Arc<Receiver<bool>>,
24+
pub bot_state: Arc<Mutex<BotState>>,
2325
}
2426

2527
impl ReadyHandler {
26-
pub fn new(config: &Config, registry: Arc<CommandRegistry>, shutdown: Receiver<bool>) -> Self {
28+
pub fn new(
29+
config: &Config,
30+
registry: Arc<CommandRegistry>,
31+
shutdown: Receiver<bool>,
32+
bot_state: Arc<Mutex<BotState>>,
33+
) -> Self {
2734
Self {
2835
config: config.clone(),
2936
registry,
3037
shutdown: Arc::new(shutdown),
38+
bot_state,
3139
}
3240
}
3341
}
@@ -36,6 +44,13 @@ impl ReadyHandler {
3644
impl EventHandler for ReadyHandler {
3745
async fn ready(&self, ctx: Context, ready: Ready) {
3846
println!("{} is online !", ready.user.name);
47+
48+
{
49+
let state = self.bot_state.lock().await;
50+
let mut ctx_lock = state.bot_context.write().await;
51+
*ctx_lock = Some(ctx.clone());
52+
}
53+
3954
let pool = match &self.config.db_pool {
4055
Some(pool) => pool,
4156
None => {

rustmail/src/types/bot.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::prelude::config::*;
2-
use serenity::all::Http;
2+
use serenity::all::{Context, Http};
33
use std::sync::Arc;
4-
use tokio::sync::watch::Sender;
4+
use tokio::sync::{watch::Sender, RwLock};
55
use tokio::task::JoinHandle;
66

77
pub enum BotStatus {
@@ -25,4 +25,5 @@ pub struct BotState {
2525
pub db_pool: Option<sqlx::SqlitePool>,
2626
pub command_tx: tokio::sync::mpsc::Sender<BotCommand>,
2727
pub bot_http: Option<Arc<Http>>,
28+
pub bot_context: Arc<RwLock<Option<Context>>>,
2829
}

0 commit comments

Comments
 (0)