Dough Git

clove/proxyBot

src/commands/edit/mod.rs

back to src/commands/edit

// src/commands/edit/mod.rs

use secrecy::ExposeSecret;
use serde_json::json;
use serenity::all::{Context, Message};

use crate::{
    Bot,
    discord::{
        Destination, NO_TARGET, Target, attachment_links_with_spoilers, attachment_spoilers,
        existing_media, message_text, proxy_payload, proxy_text,
        split_attachment_links, user_mentions,
    },
    proxy_log::ProxyLog,
};

impl Bot {
    pub(super) async fn edit(
        &self,
        ctx: &Context,
        command: &Message,
        content: &str,
    ) -> anyhow::Result<Option<&'static str>> {
        let Some(Target {
            message: target,
            components,
            classic,
            }) = self.target(ctx, command).await?
        else {
            return Ok(Some(NO_TARGET));
        };

        let links = if classic {
            split_attachment_links(&target.content).1.to_owned()
        } else {
            proxy_text(&components).1
        };

        if content.is_empty() && links.is_empty() && target.attachments.is_empty() {
            return Ok(Some(
                "That would leave the message empty. Use ❌ to delete it instead.",
                ));
        }

        let spoilers = attachment_spoilers(&target, &components);
        let mut payload = if classic {
            let replacement = proxy_payload(content, &links, Vec::new(), false);

            if replacement["content"].is_null() {
                return Ok(Some(
                    "Message text and upload links exceed 2000 characters.",
                    ));
            }

            json!({ "content": replacement["content"] })
        } else {
            let (components, attachments) = existing_media(&target, components, true);
            let mut replacement = proxy_payload(content, &links, components, true);
            replacement["attachments"] = json!(attachments);
            replacement
        };
        payload["allowed_mentions"] = user_mentions();

        let guild = command.guild_id.unwrap();
        let Some(dest) = Destination::resolve(ctx, command.channel_id, guild).await? else {
            return Ok(Some("Can't use webhooks in this channel."));
        };
        let id = target.webhook_id.unwrap();
        let webhook = id.to_webhook(&ctx.http).await?;
        let token = webhook
        .token
        .as_ref()
        .map(ExposeSecret::expose_secret)
        .ok_or_else(|| anyhow::anyhow!("webhook had no token"))?;
        let edited = ctx.http
        .edit_webhook_message(id, dest.thread, token, target.id, &payload, Vec::new())
        .await
        .map_err(|_| anyhow::anyhow!("Discord rejected webhook edit"))?;
        let label = format!("Edited proxy by {}", target.author.name);
        let avatar = self
        .senders
        .lock()
        .await
        .iter()
        .rev()
        .find(|(id, _)| *id == target.id)
        .and_then(|(_, members)| crate::avatars::log_avatar(members))
        .map(str::to_owned)
        .or_else(|| target.author.avatar_url());
        let links = message_text(&links, &attachment_links_with_spoilers(&edited.attachments, &spoilers));
        self.log_proxy(
            ctx,
            guild,
            ProxyLog {
                source: &target,
                sent: target.id,
                label: &label,
                content,
                links: &links,
                avatar: avatar.as_deref(),
            },
        )
        .await;
        command.delete(&ctx.http).await?;
        Ok(None)
    }
}