The world's most clever kitty cat
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add forget edge command

Ben C e9b97b9e e77c235d

+104
+40
src/brain.rs
··· 153 153 } 154 154 } 155 155 156 + pub fn forget_edge(&mut self, from: &str, to: &str) -> bool { 157 + if let Some(edges) = self.0.get_mut(&Self::normalize_token(from)) { 158 + edges.forget(&Self::normalize_token(to)); 159 + true 160 + } else { 161 + false 162 + } 163 + } 164 + 156 165 pub fn merge_from(&mut self, other: Self) { 157 166 for (k, v) in other.0.into_iter() { 158 167 if let Some(edges) = self.0.get_mut(&k) { ··· 378 387 assert!( 379 388 !edges.0.contains_key(&Some(String::from("evil"))), 380 389 "Edges for hello still has evil" 390 + ); 391 + assert_eq!(edges.1, 1); 392 + } 393 + 394 + #[test] 395 + fn forget_edge() { 396 + let mut brain = Brain::default(); 397 + 398 + brain.ingest("hello world"); 399 + brain.ingest("hello evil"); 400 + brain.ingest("evil bad"); 401 + 402 + let exists = brain.forget_edge("hello", "evil"); 403 + 404 + assert!(exists, "hello -> evil did not exist"); 405 + 406 + assert!( 407 + brain.0.contains_key(&Some(String::from("evil"))), 408 + "Edges don't exist for evil" 409 + ); 410 + let edges = brain 411 + .0 412 + .get(&Some(String::from("hello"))) 413 + .expect("No weights for hello"); 414 + assert!( 415 + !edges.0.contains_key(&Some(String::from("evil"))), 416 + "Edges for hello still has evil" 417 + ); 418 + assert!( 419 + edges.0.contains_key(&Some(String::from("world"))), 420 + "Edges for hello does not have world" 381 421 ); 382 422 assert_eq!(edges.1, 1); 383 423 }
+60
src/cmd/forget_edge.rs
··· 1 + use std::sync::{Arc, atomic::Ordering}; 2 + 3 + use twilight_interactions::command::{CommandModel, CreateCommand}; 4 + use twilight_model::application::interaction::{Interaction, application_command::CommandData}; 5 + 6 + use 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 + )] 15 + pub struct ForgetEdgeCommand { 16 + /// From token 17 + from: String, 18 + /// To token 19 + to: String, 20 + } 21 + 22 + impl 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 + }
+4
src/cmd/mod.rs
··· 1 1 mod dump_chain; 2 2 mod forget; 3 + mod forget_edge; 3 4 mod load_chain; 4 5 mod markov; 5 6 mod weights; ··· 18 19 19 20 use dump_chain::DumpChainCommand; 20 21 use forget::ForgetCommand; 22 + use forget_edge::ForgetEdgeCommand; 21 23 use load_chain::LoadChainCommand; 22 24 use markov::MarkovCommand; 23 25 use weights::WeightsCommand; ··· 69 71 LoadChainCommand::create_command().into(), 70 72 MarkovCommand::create_command().into(), 71 73 ForgetCommand::create_command().into(), 74 + ForgetEdgeCommand::create_command().into(), 72 75 ]; 73 76 74 77 let client = ctx.http.interaction(ctx.app_id); ··· 92 95 "load_chain" => LoadChainCommand::handle(inter, data, ctx).await, 93 96 "markov" => MarkovCommand::handle(inter, data, ctx).await, 94 97 "forget" => ForgetCommand::handle(inter, data, ctx).await, 98 + "forget-edge" => ForgetEdgeCommand::handle(inter, data, ctx).await, 95 99 other => { 96 100 warn!("Unknown command send: {other}"); 97 101 Ok(())