Rework attachments & send attachments to Discord

main
Charlotte Som 2022-04-17 12:34:04 +01:00
parent f2900d834e
commit 4c37ec5f36
4 changed files with 42 additions and 13 deletions

View File

@ -3,8 +3,15 @@ pub use reference::*;
#[derive(Debug, Clone)]
pub enum ChatAttachment {
URL(String),
InMemory { file_name: String, data: Vec<u8> },
Online {
media_type: Option<String>,
url: String,
},
InMemory {
media_type: Option<String>,
file_name: String,
data: Vec<u8>,
},
}
#[derive(Debug, Clone)]

View File

@ -17,9 +17,9 @@ async fn get_base_url() -> &'static str {
pub async fn attachment_to_url(attachment: &ChatAttachment) -> String {
match attachment {
ChatAttachment::URL(s) => s.clone(),
ChatAttachment::Online { url, .. } => url.clone(),
ChatAttachment::InMemory { .. } => {
todo!("Put in-memory attachment into webroot")
todo!("Store in-memory attachment inside webroot")
}
}
}

View File

@ -28,16 +28,19 @@ impl DiscordHandler {
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 {
reference: discord_reference(message.author.id),
display_name,
display_color,
avatar: ChatAttachment::URL(
message
.author
.static_avatar_url()
.unwrap_or_else(|| message.author.default_avatar_url()),
),
avatar: ChatAttachment::Online {
media_type: None,
url: avatar_url,
},
}
}
}
@ -72,8 +75,10 @@ impl EventHandler for DiscordHandler {
let attachments = message
.attachments
.into_iter()
.map(|a| a.url)
.map(ChatAttachment::URL)
.map(|a| ChatAttachment::Online {
media_type: a.content_type,
url: a.url,
})
.collect::<Vec<_>>();
let chat_message = ChatMessage {

View File

@ -4,7 +4,7 @@ use phoebe::{
prelude::*,
};
use serenity::{model::prelude::*, prelude::*};
use serenity::{http::AttachmentType, model::prelude::*, prelude::*};
use crate::{chat_conv, discord_reference, DiscordService};
@ -95,6 +95,20 @@ pub async fn send_discord_message(
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 {
let reply_embeds = if let Some((channel, message)) = discord_reply {
create_webhook_reply_embeds(&discord.ctx, channel, message).await
@ -113,6 +127,7 @@ pub async fn send_discord_message(
))
.avatar_url(&avatar_url)
.embeds(reply_embeds)
.add_files(files.clone())
})
.await?
{
@ -132,6 +147,7 @@ pub async fn send_discord_message(
let sent_message = channel_id
.send_message(&discord.ctx, move |m| {
let m = m.content(content);
let m = m.add_files(files);
if let Some(reply) = discord_reply {
m.reference_message(reply)
} else {
@ -139,6 +155,7 @@ pub async fn send_discord_message(
}
})
.await?;
Ok(ChatMessageReference::new(
discord_reference(sent_message.channel_id),
sent_message.id,