use std::sync::Arc; use twilight_interactions::command::{CommandModel, CreateCommand}; use twilight_model::{ application::interaction::{Interaction, application_command::CommandData}, http::attachment::Attachment, }; use crate::{ BROTLI_BUF_SIZE, BotContext, cmd::DEFER_INTER_RESP_EPHEMERAL, get_brotli_params, prelude::*, require_owner, }; #[derive(CommandModel, CreateCommand)] #[command(name = "dump_chain", desc = "Dump chain")] pub struct DumpChainCommand { /// Generate as Nushell (Legacy) compatible msgpack compat: Option, } impl DumpChainCommand { pub async fn handle(inter: Interaction, data: CommandData, ctx: Arc) -> Result { let client = ctx.http.interaction(ctx.app_id); require_owner!(inter, ctx, client); let Self { compat } = Self::from_interaction(data.into()).context("Failed to parse command data")?; client .create_response(inter.id, &inter.token, &DEFER_INTER_RESP_EPHEMERAL) .await .context("Failed to defer")?; let mut buf = Vec::::with_capacity(4096); let mut brotli_writer = brotli::CompressorWriter::with_params(&mut buf, BROTLI_BUF_SIZE, &get_brotli_params()); if compat.unwrap_or_default() { let brain = ctx.brain_handle.read().await; let map = brain.as_legacy_hashmap(); drop(brain); rmp_serde::encode::write(&mut brotli_writer, &map) .context("Failed to legacy encode brain")?; } else { let brain = ctx.brain_handle.read().await; rmp_serde::encode::write(&mut brotli_writer, &*brain) .context("Failed to write serialized brain")?; } drop(brotli_writer); let attachment = Attachment::from_bytes(String::from("brain.msgpackz"), buf, 1); client .update_response(&inter.token) .attachments(&[attachment]) .await .context("Failed to send brain")?; Ok(()) } }