Refactor: Extract discord attachment creation into its own function

main
Charlotte Som 2022-05-22 11:30:30 +01:00
parent 4fc8c15b07
commit 602a042bd5
1 changed files with 30 additions and 14 deletions

View File

@ -1,6 +1,6 @@
use phoebe::{ use phoebe::{
attachments::attachment_to_url, attachments::attachment_to_url,
mid_chat::{ChatMessage, ChatMessageEdit, ChatMessageReference, ChatReference}, mid_chat::{ChatAttachment, ChatMessage, ChatMessageEdit, ChatMessageReference, ChatReference},
prelude::*, prelude::*,
}; };
@ -72,6 +72,34 @@ async fn create_webhook_reply_embeds(
vec![] vec![]
} }
async fn create_discord_attachments(source: &'_ ChatMessage) -> Vec<AttachmentType<'_>> {
source
.attachments
.iter()
.map(|a| match a {
ChatAttachment::Online {
url,
media_type: Some(media_type),
} => {
if media_type.starts_with("image/") {
AttachmentType::Image(url)
} else {
todo!("Handle non-image online attachment")
}
}
ChatAttachment::Online { .. } => {
todo!("Handle online attachment with no media_type")
}
ChatAttachment::InMemory {
file_name, data, ..
} => AttachmentType::Bytes {
filename: file_name.clone(),
data: data.into(),
},
})
.collect()
}
pub async fn send_discord_message( pub async fn send_discord_message(
discord: &mut DiscordService, discord: &mut DiscordService,
source: &ChatMessage, source: &ChatMessage,
@ -95,19 +123,7 @@ pub async fn send_discord_message(
None None
}; };
let files = source let files = create_discord_attachments(source).await;
.attachments
.iter()
.map(|a| match a {
phoebe::mid_chat::ChatAttachment::Online { url, .. } => AttachmentType::Image(url),
phoebe::mid_chat::ChatAttachment::InMemory {
file_name, data, ..
} => AttachmentType::Bytes {
filename: file_name.clone(),
data: data.into(),
},
})
.collect::<Vec<_>>();
if let Some(webhook) = get_or_create_webhook_for_channel(&mut *discord, &channel_id).await { if let Some(webhook) = get_or_create_webhook_for_channel(&mut *discord, &channel_id).await {
let reply_embeds = if let Some((channel, message)) = discord_reply { let reply_embeds = if let Some((channel, message)) = discord_reply {