// to be later integrated into interaction registry...currently component registry use crate::framework::{ interactions::{ InteractionError, context::InteractionContext, message_command::MessageCommand, }, runtime::Runtime, }; use scc::HashMap; use std::sync::Arc; use twilight_model::{ application::command::CommandType, application::interaction::InteractionData, gateway::payload::incoming::InteractionCreate, }; #[derive(Default, Debug)] pub struct MessageCommandRegistry { inner: HashMap>>, } impl MessageCommandRegistry where T: Send + Sync + 'static, { pub fn new() -> Self { Self { inner: Default::default(), } } pub fn register(&self, name: impl ToString, command: Arc>) { self.inner.upsert(name.to_string(), command); } pub fn unregister(&self, name: impl ToString) { self.inner.remove(&name.to_string()); } pub async fn execute( &self, event: &InteractionCreate, rt: Arc>, ) -> Result<(), InteractionError> { if let Some(data) = &event.data { if let InteractionData::ApplicationCommand(data) = data { if data.kind == CommandType::Message { if let Some(command) = self.inner.get_async(&data.name).await { let prefixes = rt .get_prefixes( event.guild_id.map(|id| id.get()), event.author_id().map(|id| id.get()), ) .await; let prefix = prefixes .first() .ok_or(InteractionError::PrefixesVectorEmpty)?; // Message commands have a resolved target_id if let Some(target_id) = data.target_id { command .execute( InteractionContext::new( event.clone(), rt.clone(), prefix.to_string(), ), target_id.get(), ) .await } else { Err(InteractionError::InteractionNotFound) } } else { Err(InteractionError::InteractionNotFound) } } else { Err(InteractionError::InteractionTypeUnsent) } } else { Err(InteractionError::InteractionTypeUnsent) } } else { Err(InteractionError::InteractionTypeUnsent) } } }