feat: fix the command handler to reveal more data about the interaction

This commit is contained in:
2026-03-27 01:35:29 -04:00
parent d9e0801ec9
commit 3439bf9699
8 changed files with 70 additions and 55 deletions

26
Cargo.lock generated
View File

@@ -461,16 +461,6 @@ dependencies = [
"wyz", "wyz",
] ]
[[package]]
name = "blart"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "220e5685b0a23b634e9215a69cfa9800e103ccd2f0134bd1a81cc9c584059701"
dependencies = [
"bytemuck",
"paste",
]
[[package]] [[package]]
name = "block-buffer" name = "block-buffer"
version = "0.10.4" version = "0.10.4"
@@ -1453,11 +1443,11 @@ checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
name = "fomo-reducer" name = "fomo-reducer"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"blart",
"clap", "clap",
"dashmap 6.1.0", "dashmap 6.1.0",
"futures", "futures",
"opendal", "opendal",
"patricia_tree 0.10.1",
"rhai", "rhai",
"rustls 0.23.35", "rustls 0.23.35",
"secrecy 0.10.3", "secrecy 0.10.3",
@@ -3599,12 +3589,6 @@ dependencies = [
"windows-link", "windows-link",
] ]
[[package]]
name = "paste"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]] [[package]]
name = "patricia_tree" name = "patricia_tree"
version = "0.8.0" version = "0.8.0"
@@ -3614,6 +3598,12 @@ dependencies = [
"bitflags 2.10.0", "bitflags 2.10.0",
] ]
[[package]]
name = "patricia_tree"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4df0e43512f12f23a6b08c7b893192b7d6ec937b95ee03af040847907fe5cef7"
[[package]] [[package]]
name = "pbkdf2" name = "pbkdf2"
version = "0.12.2" version = "0.12.2"
@@ -5466,7 +5456,7 @@ dependencies = [
"futures-core", "futures-core",
"futures-util", "futures-util",
"hls_m3u8", "hls_m3u8",
"patricia_tree", "patricia_tree 0.8.0",
"reqwest", "reqwest",
"tokio", "tokio",
"tracing", "tracing",

View File

@@ -4,7 +4,6 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
blart = "0.4.0"
clap = { version = "4.5.40", features = ["derive", "env"] } clap = { version = "4.5.40", features = ["derive", "env"] }
dashmap = "6.1.0" dashmap = "6.1.0"
futures = "0.3.32" futures = "0.3.32"
@@ -45,6 +44,7 @@ opendal = { git = "https://github.com/apache/opendal", features = [
"services-sled", "services-sled",
"services-webdav", "services-webdav",
] } ] }
patricia_tree = "0.10.1"
rhai = "1.23.6" rhai = "1.23.6"
rustls = "0.23" rustls = "0.23"
secrecy = { version = "0.10.3", features = ["serde"] } secrecy = { version = "0.10.3", features = ["serde"] }

View File

@@ -2,7 +2,7 @@ use std::sync::LazyLock;
use twilight_model::application::{ use twilight_model::application::{
command::{Command, CommandType}, command::{Command, CommandType},
interaction::application_command::CommandData, interaction::{Interaction, application_command::CommandData},
}; };
use twilight_util::builder::command::CommandBuilder; use twilight_util::builder::command::CommandBuilder;
@@ -19,6 +19,20 @@ pub static COMMAND: LazyLock<Command> = LazyLock::new(|| {
}); });
#[tracing::instrument] #[tracing::instrument]
pub async fn handle(state: State, data: CommandData) { pub async fn handle(state: State, interaction: Interaction) {
todo!(); let vcs = state.vcs;
let guild_id = interaction.guild_id.expect("TODO");
// let user_id = data.user.map(|user| user.id).expect("TODO");
let user_id = interaction
.member
.and_then(|member| member.user.map(|user| user.id))
.expect("TODO");
let guild_vcs = vcs.get(&guild_id).expect("TODO");
tracing::error!(?guild_vcs, "TODO");
let user_in_vc_data = guild_vcs.get_left_and_data_for(&user_id);
tracing::error!(?user_in_vc_data, "TODO");
} }

View File

@@ -2,7 +2,7 @@ use std::sync::LazyLock;
use twilight_model::application::{ use twilight_model::application::{
command::{Command, CommandType}, command::{Command, CommandType},
interaction::application_command::CommandData, interaction::Interaction,
}; };
use twilight_util::builder::command::CommandBuilder; use twilight_util::builder::command::CommandBuilder;
@@ -19,6 +19,6 @@ pub static COMMAND: LazyLock<Command> = LazyLock::new(|| {
}); });
#[tracing::instrument] #[tracing::instrument]
pub async fn handle(state: State, data: CommandData) { pub async fn handle(state: State, interaction: Interaction) {
todo!(); todo!();
} }

View File

@@ -1,14 +1,8 @@
use std::{ use std::{fmt::Debug, sync::Arc};
ffi::{CStr, CString},
fmt::Debug,
sync::Arc,
};
use blart::TreeMap;
use futures::future::BoxFuture; use futures::future::BoxFuture;
use twilight_model::application::{ use patricia_tree::StringPatriciaMap;
command::Command, interaction::application_command::CommandData, use twilight_model::application::{command::Command, interaction::Interaction};
};
use crate::VCs; use crate::VCs;
@@ -22,14 +16,14 @@ pub struct State {
} }
type Return = (); type Return = ();
type BoxedHandler = Box<dyn Fn(State, CommandData) -> BoxFuture<'static, Return>>; type BoxedHandler = Box<dyn Fn(State, Interaction) -> BoxFuture<'static, Return>>;
fn box_handler<Handler, Fut>(handler: Handler) -> BoxedHandler fn box_handler<Handler, Fut>(handler: Handler) -> BoxedHandler
where where
Fut: Future<Output = Return> + Send + 'static, Fut: Future<Output = Return> + Send + 'static,
Handler: Fn(State, CommandData) -> Fut + 'static, Handler: Fn(State, Interaction) -> Fut + 'static,
{ {
Box::new(move |state, command_data| Box::pin(handler(state, command_data))) Box::new(move |state, interaction| Box::pin(handler(state, interaction)))
} }
pub fn all() -> Vec<(&'static Command, BoxedHandler)> { pub fn all() -> Vec<(&'static Command, BoxedHandler)> {
@@ -42,32 +36,31 @@ pub fn all() -> Vec<(&'static Command, BoxedHandler)> {
#[derive(Default)] #[derive(Default)]
pub struct Router { pub struct Router {
map: TreeMap<CString, BoxedHandler>, map: StringPatriciaMap<BoxedHandler>,
} }
impl Router { impl Router {
fn add_route<'s, 'a, Fut, Handler>(&'s mut self, name: &'a str, handler: Handler) fn add_route<'s, 'a, Fut, Handler>(&'s mut self, name: &'a str, handler: Handler)
where where
Fut: Future<Output = Return> + Send + 'static, Fut: Future<Output = Return> + Send + 'static,
Handler: Fn(State, CommandData) -> Fut + 'static, Handler: Fn(State, Interaction) -> Fut + 'static,
{ {
self.add_route_already_boxed(name, box_handler(handler)); self.add_route_already_boxed(name, box_handler(handler));
} }
fn add_route_already_boxed<'s, 'a>(&'s mut self, name: &'a str, boxed_handler: BoxedHandler) { fn add_route_already_boxed<'s, 'a>(&'s mut self, name: &'a str, boxed_handler: BoxedHandler) {
self.map.insert(name.parse().unwrap(), boxed_handler); self.map.insert(name, boxed_handler);
} }
pub async fn handle(&self, state: State, command_data: CommandData) -> Return { pub async fn handle(
let name = &command_data.name; &self,
let key = CStr::from_bytes_with_nul(name.as_bytes()).unwrap(); state: State,
command_name: &str,
interaction: Interaction,
) -> Option<Return> {
let handler = self.map.get(command_name)?;
let handler = self Some(handler(state, interaction).await)
.map
.get(key)
.expect("asked to handle an inexistent command");
handler(state, command_data).await
} }
} }

View File

@@ -2,7 +2,7 @@ use std::sync::LazyLock;
use twilight_model::application::{ use twilight_model::application::{
command::{Command, CommandType}, command::{Command, CommandType},
interaction::application_command::CommandData, interaction::Interaction,
}; };
use twilight_util::builder::command::CommandBuilder; use twilight_util::builder::command::CommandBuilder;
@@ -19,6 +19,6 @@ pub static COMMAND: LazyLock<Command> = LazyLock::new(|| {
}); });
#[tracing::instrument] #[tracing::instrument]
pub async fn handle(state: State, data: CommandData) { pub async fn handle(state: State, interaction: Interaction) {
todo!(); todo!();
} }

View File

@@ -109,8 +109,8 @@ async fn main() -> Result<(), MainError> {
let intents = Intents::GUILD_VOICE_STATES; let intents = Intents::GUILD_VOICE_STATES;
let mut shard = Shard::new(shard_id, discord_token.expose_secret().to_owned(), intents); let mut shard = Shard::new(shard_id, discord_token.expose_secret().to_owned(), intents);
let vc_event_types = EventTypeFlags::GUILD_VOICE_STATES; let event_types = EventTypeFlags::GUILD_VOICE_STATES | EventTypeFlags::INTERACTION_CREATE;
let mut next_event = shard.next_event(vc_event_types); let mut next_event = shard.next_event(event_types);
let discord_client = twilight_http::Client::new(discord_token.expose_secret().to_owned()); let discord_client = twilight_http::Client::new(discord_token.expose_secret().to_owned());
@@ -161,7 +161,7 @@ async fn main() -> Result<(), MainError> {
} }
} }
next_event = shard.next_event(vc_event_types); next_event = shard.next_event(event_types);
} }
Ok(()) Ok(())
@@ -176,13 +176,16 @@ async fn handle_event(command_router: &CommandRouter, vcs: Arc<VCs>, event: Even
Event::InteractionCreate(interaction_create) => { Event::InteractionCreate(interaction_create) => {
let InteractionCreate(interaction) = *interaction_create; let InteractionCreate(interaction) = *interaction_create;
match interaction.data { match &interaction.data {
None => { None => {
tracing::warn!("missing expected interaction data"); tracing::warn!("missing expected interaction data");
} }
Some(InteractionData::ApplicationCommand(command_data)) => { Some(InteractionData::ApplicationCommand(command_data)) => {
let command_name = &command_data.name.clone();
let state = State { vcs }; let state = State { vcs };
command_router.handle(state, *command_data).await; command_router
.handle(state, command_name, interaction)
.await;
} }
Some(InteractionData::MessageComponent(component_data)) => { Some(InteractionData::MessageComponent(component_data)) => {

View File

@@ -48,6 +48,21 @@ where
self.right_to_left.get(right) self.right_to_left.get(right)
} }
pub fn get_left_and_data_for(&self, right: &Right) -> Option<(&Left, &RightData)> {
let left = self.right_to_left.get(right)?;
let rights = self.get_rights_for(left)?;
let right_data = rights.get(right)?;
Some((left, right_data))
}
pub fn get_data_for(&self, right: &Right) -> Option<&RightData> {
let (_left, right_data) = self.get_left_and_data_for(right)?;
Some(right_data)
}
pub fn remove_left(&mut self, left: &Left) -> Option<(Left, BTreeMap<Right, RightData>)> { pub fn remove_left(&mut self, left: &Left) -> Option<(Left, BTreeMap<Right, RightData>)> {
let (left, rights) = self.left_to_rights.remove_entry(left)?; let (left, rights) = self.left_to_rights.remove_entry(left)?;