Rework attachments & send attachments to Discord
This commit is contained in:
		
							parent
							
								
									f2900d834e
								
							
						
					
					
						commit
						4c37ec5f36
					
				
					 4 changed files with 42 additions and 13 deletions
				
			
		|  | @ -3,8 +3,15 @@ pub use reference::*; | ||||||
| 
 | 
 | ||||||
| #[derive(Debug, Clone)] | #[derive(Debug, Clone)] | ||||||
| pub enum ChatAttachment { | pub enum ChatAttachment { | ||||||
|     URL(String), |     Online { | ||||||
|     InMemory { file_name: String, data: Vec<u8> }, |         media_type: Option<String>, | ||||||
|  |         url: String, | ||||||
|  |     }, | ||||||
|  |     InMemory { | ||||||
|  |         media_type: Option<String>, | ||||||
|  |         file_name: String, | ||||||
|  |         data: Vec<u8>, | ||||||
|  |     }, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive(Debug, Clone)] | #[derive(Debug, Clone)] | ||||||
|  |  | ||||||
|  | @ -17,9 +17,9 @@ async fn get_base_url() -> &'static str { | ||||||
| 
 | 
 | ||||||
| pub async fn attachment_to_url(attachment: &ChatAttachment) -> String { | pub async fn attachment_to_url(attachment: &ChatAttachment) -> String { | ||||||
|     match attachment { |     match attachment { | ||||||
|         ChatAttachment::URL(s) => s.clone(), |         ChatAttachment::Online { url, .. } => url.clone(), | ||||||
|         ChatAttachment::InMemory { .. } => { |         ChatAttachment::InMemory { .. } => { | ||||||
|             todo!("Put in-memory attachment into webroot") |             todo!("Store in-memory attachment inside webroot") | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -28,16 +28,19 @@ impl DiscordHandler { | ||||||
| 
 | 
 | ||||||
|         let display_color = tag_color(ctx, message).await; |         let display_color = tag_color(ctx, message).await; | ||||||
| 
 | 
 | ||||||
|  |         let avatar_url = message | ||||||
|  |             .author | ||||||
|  |             .static_avatar_url() | ||||||
|  |             .unwrap_or_else(|| message.author.default_avatar_url()); | ||||||
|  | 
 | ||||||
|         ChatAuthor { |         ChatAuthor { | ||||||
|             reference: discord_reference(message.author.id), |             reference: discord_reference(message.author.id), | ||||||
|             display_name, |             display_name, | ||||||
|             display_color, |             display_color, | ||||||
|             avatar: ChatAttachment::URL( |             avatar: ChatAttachment::Online { | ||||||
|                 message |                 media_type: None, | ||||||
|                     .author |                 url: avatar_url, | ||||||
|                     .static_avatar_url() |             }, | ||||||
|                     .unwrap_or_else(|| message.author.default_avatar_url()), |  | ||||||
|             ), |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | @ -72,8 +75,10 @@ impl EventHandler for DiscordHandler { | ||||||
|         let attachments = message |         let attachments = message | ||||||
|             .attachments |             .attachments | ||||||
|             .into_iter() |             .into_iter() | ||||||
|             .map(|a| a.url) |             .map(|a| ChatAttachment::Online { | ||||||
|             .map(ChatAttachment::URL) |                 media_type: a.content_type, | ||||||
|  |                 url: a.url, | ||||||
|  |             }) | ||||||
|             .collect::<Vec<_>>(); |             .collect::<Vec<_>>(); | ||||||
| 
 | 
 | ||||||
|         let chat_message = ChatMessage { |         let chat_message = ChatMessage { | ||||||
|  |  | ||||||
|  | @ -4,7 +4,7 @@ use phoebe::{ | ||||||
|     prelude::*, |     prelude::*, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| use serenity::{model::prelude::*, prelude::*}; | use serenity::{http::AttachmentType, model::prelude::*, prelude::*}; | ||||||
| 
 | 
 | ||||||
| use crate::{chat_conv, discord_reference, DiscordService}; | use crate::{chat_conv, discord_reference, DiscordService}; | ||||||
| 
 | 
 | ||||||
|  | @ -95,6 +95,20 @@ pub async fn send_discord_message( | ||||||
|         None |         None | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|  |     let files = source | ||||||
|  |         .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 { | ||||||
|             create_webhook_reply_embeds(&discord.ctx, channel, message).await |             create_webhook_reply_embeds(&discord.ctx, channel, message).await | ||||||
|  | @ -113,6 +127,7 @@ pub async fn send_discord_message( | ||||||
|                     )) |                     )) | ||||||
|                     .avatar_url(&avatar_url) |                     .avatar_url(&avatar_url) | ||||||
|                     .embeds(reply_embeds) |                     .embeds(reply_embeds) | ||||||
|  |                     .add_files(files.clone()) | ||||||
|             }) |             }) | ||||||
|             .await? |             .await? | ||||||
|         { |         { | ||||||
|  | @ -132,6 +147,7 @@ pub async fn send_discord_message( | ||||||
|     let sent_message = channel_id |     let sent_message = channel_id | ||||||
|         .send_message(&discord.ctx, move |m| { |         .send_message(&discord.ctx, move |m| { | ||||||
|             let m = m.content(content); |             let m = m.content(content); | ||||||
|  |             let m = m.add_files(files); | ||||||
|             if let Some(reply) = discord_reply { |             if let Some(reply) = discord_reply { | ||||||
|                 m.reference_message(reply) |                 m.reference_message(reply) | ||||||
|             } else { |             } else { | ||||||
|  | @ -139,6 +155,7 @@ pub async fn send_discord_message( | ||||||
|             } |             } | ||||||
|         }) |         }) | ||||||
|         .await?; |         .await?; | ||||||
|  | 
 | ||||||
|     Ok(ChatMessageReference::new( |     Ok(ChatMessageReference::new( | ||||||
|         discord_reference(sent_message.channel_id), |         discord_reference(sent_message.channel_id), | ||||||
|         sent_message.id, |         sent_message.id, | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue