The world's most clever kitty cat
at main 62 lines 2.1 kB view raw
1use std::sync::Arc; 2 3use twilight_interactions::command::{CommandModel, CreateCommand}; 4use twilight_model::{ 5 application::interaction::{Interaction, application_command::CommandData}, 6 http::attachment::Attachment, 7}; 8 9use crate::{ 10 BROTLI_BUF_SIZE, BotContext, cmd::DEFER_INTER_RESP_EPHEMERAL, get_brotli_params, prelude::*, 11 require_owner, 12}; 13 14#[derive(CommandModel, CreateCommand)] 15#[command(name = "dump_chain", desc = "Dump chain")] 16pub struct DumpChainCommand { 17 /// Generate as Nushell (Legacy) compatible msgpack 18 compat: Option<bool>, 19} 20 21impl DumpChainCommand { 22 pub async fn handle(inter: Interaction, data: CommandData, ctx: Arc<BotContext>) -> Result { 23 let client = ctx.http.interaction(ctx.app_id); 24 25 require_owner!(inter, ctx, client); 26 27 let Self { compat } = 28 Self::from_interaction(data.into()).context("Failed to parse command data")?; 29 30 client 31 .create_response(inter.id, &inter.token, &DEFER_INTER_RESP_EPHEMERAL) 32 .await 33 .context("Failed to defer")?; 34 35 let mut buf = Vec::<u8>::with_capacity(4096); 36 let mut brotli_writer = 37 brotli::CompressorWriter::with_params(&mut buf, BROTLI_BUF_SIZE, &get_brotli_params()); 38 39 if compat.unwrap_or_default() { 40 let brain = ctx.brain_handle.read().await; 41 let map = brain.as_legacy_hashmap(); 42 drop(brain); 43 rmp_serde::encode::write(&mut brotli_writer, &map) 44 .context("Failed to legacy encode brain")?; 45 } else { 46 let brain = ctx.brain_handle.read().await; 47 rmp_serde::encode::write(&mut brotli_writer, &*brain) 48 .context("Failed to write serialized brain")?; 49 } 50 drop(brotli_writer); 51 52 let attachment = Attachment::from_bytes(String::from("brain.msgpackz"), buf, 1); 53 54 client 55 .update_response(&inter.token) 56 .attachments(&[attachment]) 57 .await 58 .context("Failed to send brain")?; 59 60 Ok(()) 61 } 62}