push 1 checkpoiint *holy hell i sure hope it works*

This commit is contained in:
Anish Nayak
2025-08-12 18:01:01 +01:00
parent 79f0ca08b2
commit 9549e70593
8 changed files with 310 additions and 67 deletions
@@ -0,0 +1,76 @@
use std::sync::Arc;
use scc::HashMap;
use twilight_model::{
application::interaction::InteractionData, application::command::CommandType, gateway::payload::incoming::InteractionCreate,
};
use crate::framework::{
interactions::{InteractionError, context::InteractionContext, message_command::MessageCommand},
runtime::Runtime,
};
#[derive(Default)]
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)
}
}
}