2022-04-16 08:06:48 +00:00
|
|
|
use phoebe::{
|
|
|
|
mid_chat::{ChatMessage, ChatMessageReference, ChatReference},
|
2022-04-16 09:45:42 +00:00
|
|
|
prelude::*,
|
2022-04-16 08:06:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use serenity::{model::prelude::*, prelude::*};
|
|
|
|
|
2022-04-16 09:16:51 +00:00
|
|
|
use crate::{chat_conv, discord_reference, DiscordService};
|
2022-04-16 08:06:48 +00:00
|
|
|
|
2022-04-16 09:45:42 +00:00
|
|
|
async fn get_or_create_webhook_for_channel(
|
|
|
|
discord: &mut DiscordService,
|
|
|
|
channel: &ChannelId,
|
|
|
|
) -> Option<Webhook> {
|
|
|
|
if let Ok(webhooks) = channel.webhooks(&discord.ctx).await {
|
|
|
|
for webhook in webhooks {
|
|
|
|
if matches!(webhook.name.as_deref(), Some("Phoebe")) {
|
|
|
|
return Some(webhook);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(webhook) = channel.create_webhook(&discord.ctx, "Phoebe").await {
|
|
|
|
return Some(webhook);
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn create_webhook_reply_embeds(
|
|
|
|
discord_ctx: &Context,
|
|
|
|
channel_id: ChannelId,
|
|
|
|
message_id: MessageId,
|
|
|
|
) -> Vec<serde_json::Value> {
|
|
|
|
if let Ok(replied_message) = channel_id.message(discord_ctx, message_id).await {
|
|
|
|
let replied_author_name = format!(
|
|
|
|
"{} ↩️",
|
|
|
|
replied_message
|
|
|
|
.author_nick(discord_ctx)
|
|
|
|
.await
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&replied_message.author.name)
|
|
|
|
);
|
|
|
|
|
|
|
|
let reply_description = format!(
|
|
|
|
"**[Reply to:]({})**\n{}",
|
|
|
|
replied_message.id.link(
|
|
|
|
channel_id,
|
|
|
|
discord_ctx
|
|
|
|
.cache
|
|
|
|
.guild_channel(channel_id)
|
|
|
|
.await
|
|
|
|
.map(|gc| gc.guild_id)
|
|
|
|
),
|
|
|
|
&replied_message.content
|
|
|
|
);
|
|
|
|
|
|
|
|
return vec![Embed::fake(|e| {
|
|
|
|
e.author(|a| {
|
|
|
|
a.icon_url(
|
|
|
|
&replied_message
|
|
|
|
.author
|
|
|
|
.static_avatar_url()
|
|
|
|
.unwrap_or_else(|| replied_message.author.default_avatar_url()),
|
|
|
|
)
|
|
|
|
.name(replied_author_name)
|
|
|
|
})
|
|
|
|
.description(reply_description)
|
|
|
|
})];
|
|
|
|
}
|
|
|
|
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
|
2022-04-16 08:06:48 +00:00
|
|
|
pub async fn send_discord_message(
|
2022-04-16 09:16:51 +00:00
|
|
|
discord: &mut DiscordService,
|
2022-04-16 08:06:48 +00:00
|
|
|
source: &ChatMessage,
|
|
|
|
destination_channel: ChatReference,
|
|
|
|
) -> Result<ChatMessageReference> {
|
|
|
|
let channel_id = destination_channel.id.parse::<ChannelId>()?;
|
|
|
|
|
2022-04-16 09:16:51 +00:00
|
|
|
let discord_reply = if let Some(reply) = &source.replying {
|
|
|
|
if let Some(reply_ref) = discord
|
|
|
|
.lookup_message(reply, |r| future::ready(r.channel == destination_channel))
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
assert_eq!(reply_ref.channel.service, "discord");
|
|
|
|
let channel_id: ChannelId = reply_ref.channel.id.parse().unwrap();
|
|
|
|
let message_id: MessageId = reply_ref.message_id.parse::<u64>().unwrap().into();
|
|
|
|
Some((channel_id, message_id))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2022-04-16 09:45:42 +00:00
|
|
|
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
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(sent_message) = webhook
|
|
|
|
.execute(&discord.ctx, true, |w| {
|
|
|
|
w.content(chat_conv::format(&source.content))
|
|
|
|
.username(format!(
|
|
|
|
"{} ({})",
|
|
|
|
&source.author.display_name, &source.author.reference.service
|
|
|
|
))
|
|
|
|
.avatar_url(&source.author.avatar_url)
|
|
|
|
.embeds(reply_embeds)
|
|
|
|
})
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
return Ok(ChatMessageReference::new(
|
|
|
|
discord_reference(sent_message.channel_id),
|
|
|
|
sent_message.id,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let content = format!(
|
|
|
|
"{} ({}): {}",
|
|
|
|
source.author.display_name,
|
|
|
|
source.author.reference.service,
|
|
|
|
chat_conv::format(&source.content)
|
|
|
|
);
|
|
|
|
|
2022-04-16 08:06:48 +00:00
|
|
|
let sent_message = channel_id
|
2022-04-16 09:16:51 +00:00
|
|
|
.send_message(&discord.ctx, move |m| {
|
2022-04-16 08:06:48 +00:00
|
|
|
let m = m.content(content);
|
2022-04-16 09:16:51 +00:00
|
|
|
if let Some(reply) = discord_reply {
|
|
|
|
m.reference_message(reply)
|
2022-04-16 08:06:48 +00:00
|
|
|
} else {
|
|
|
|
m
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
Ok(ChatMessageReference::new(
|
|
|
|
discord_reference(sent_message.channel_id),
|
|
|
|
sent_message.id,
|
|
|
|
))
|
|
|
|
}
|