use serenity::model::channel::Message;
use crate::db::{ChannelMessageRepository, Pool, models::ChannelMessage};
pub(crate) async fn record_sent(pool: Option<Pool>, msg: &Message, content: &str) {
let guild = msg.guild_id.map(|g| g.get().to_string());
let bot_id = msg.author.id.get();
record_row(
pool,
msg.channel_id.get(),
guild,
msg.id.get(),
Some(bot_id),
content,
)
.await;
}
pub(crate) async fn record_row(
pool: Option<Pool>,
channel_id: u64,
guild_name: Option<String>,
message_id: u64,
sender_id: Option<u64>,
content: &str,
) {
if content.trim().is_empty() {
return;
}
let Some(pool) = pool.or_else(|| crate::db::global_pool().cloned()) else {
tracing::warn!("discord outbox: no DB pool, outgoing message not recorded");
return;
};
let sender_id = sender_id
.map(|id| id.to_string())
.unwrap_or_else(|| "bot:opencrabs".to_string());
let cm = ChannelMessage::new(
"discord".to_string(),
channel_id.to_string(),
guild_name,
sender_id,
"OpenCrabs".to_string(),
content.to_string(),
"text".to_string(),
Some(message_id.to_string()),
);
let repo = ChannelMessageRepository::new(pool);
if let Err(e) = repo.insert(&cm).await {
tracing::warn!("discord outbox: failed to record message {message_id}: {e}");
}
}