Finish refactoring

This commit is contained in:
2025-08-10 21:15:53 -03:00
parent f123282699
commit a004fe2308
50 changed files with 2639 additions and 587 deletions
+30 -10
View File
@@ -1,28 +1,34 @@
use std::sync::Arc;
use scc::HashMap;
use twilight_http::Client;
use twilight_model::{
application::interaction::InteractionData, gateway::payload::incoming::InteractionCreate,
oauth::Application,
};
use crate::framework::{handlers::Handlers, interactions::{context::InteractionContext, InteractionError}};
use crate::framework::{
interactions::{InteractionError, context::InteractionContext},
runtime::Runtime,
};
use super::components::Component;
pub struct Registry {
inner: HashMap<String, Arc<dyn Component>>,
#[derive(Default)]
pub struct ComponentRegistry<T> {
inner: HashMap<String, Arc<dyn Component<T>>>,
}
impl Registry {
impl<T> ComponentRegistry<T>
where
T: Send + Sync + 'static,
{
pub fn new() -> Self {
Self {
inner: Default::default(),
}
}
pub fn register(&self, id: impl ToString, component: Arc<dyn Component>) {
pub fn register(&self, id: impl ToString, component: Arc<dyn Component<T>>) {
self.inner.upsert(id.to_string(), component);
}
@@ -32,15 +38,29 @@ impl Registry {
pub async fn execute(
&self,
event: InteractionCreate,
handlers: Arc<Handlers>,
event: &InteractionCreate,
rt: Arc<Runtime<T>>,
) -> Result<(), InteractionError> {
if let Some(data) = &event.data {
match data {
InteractionData::MessageComponent(data) => {
if let Some(component) = self.inner.get_async(&data.custom_id).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)?;
component
.execute(InteractionContext::new(event, handlers))
.execute(InteractionContext::new(
event.clone(),
rt.clone(),
prefix.to_string(),
))
.await
} else {
Err(InteractionError::InteractionNotFound)