The world's most clever kitty cat
1use std::sync::{Arc, atomic::Ordering};
2
3use twilight_interactions::command::{CommandModel, CreateCommand};
4use twilight_model::application::interaction::{Interaction, application_command::CommandData};
5
6use crate::{
7 BotContext, cmd::DEFER_INTER_RESP_EPHEMERAL, prelude::*, require_owner, status::update_status,
8};
9
10#[derive(CommandModel, CreateCommand)]
11#[command(
12 name = "forget-edge",
13 desc = "Erase a specific edge in the graph. THIS ACTION IS IRREVERSIBLE!"
14)]
15pub struct ForgetEdgeCommand {
16 /// From token
17 from: String,
18 /// To token
19 to: String,
20}
21
22impl ForgetEdgeCommand {
23 pub async fn handle(inter: Interaction, data: CommandData, ctx: Arc<BotContext>) -> Result {
24 let client = ctx.http.interaction(ctx.app_id);
25
26 require_owner!(inter, ctx, client);
27
28 let Self { from, to } =
29 Self::from_interaction(data.into()).context("Failed to parse command data")?;
30
31 client
32 .create_response(inter.id, &inter.token, &DEFER_INTER_RESP_EPHEMERAL)
33 .await
34 .context("Failed to defer")?;
35
36 let existed = {
37 let mut brain = ctx.brain_handle.write().await;
38 let existed = brain.forget_edge(&from, &to);
39 if existed {
40 ctx.pending_save.store(true, Ordering::Relaxed);
41 update_status(&brain, &ctx.shard_sender).context("Failed to update status")?;
42 }
43 existed
44 };
45
46 let msg = if existed {
47 "Edge forgotten"
48 } else {
49 "That edge does not seem to exist"
50 };
51
52 client
53 .update_response(&inter.token)
54 .content(Some(msg))
55 .await
56 .context("Failed to send brain")?;
57
58 Ok(())
59 }
60}