85 lines
2.9 KiB
Rust
85 lines
2.9 KiB
Rust
// 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<T> {
|
|
inner: HashMap<String, Arc<dyn MessageCommand<T>>>,
|
|
}
|
|
|
|
impl<T> MessageCommandRegistry<T>
|
|
where
|
|
T: Send + Sync + 'static,
|
|
{
|
|
pub fn new() -> Self {
|
|
Self {
|
|
inner: Default::default(),
|
|
}
|
|
}
|
|
|
|
pub fn register(&self, name: impl ToString, command: Arc<dyn MessageCommand<T>>) {
|
|
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<Runtime<T>>,
|
|
) -> 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)
|
|
}
|
|
}
|
|
}
|