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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -48,6 +48,21 @@ where
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>)> {
let (left, rights) = self.left_to_rights.remove_entry(left)?;