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
+19 -13
View File
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{fmt::Debug, sync::Arc};
use async_trait::async_trait;
use twilight_model::channel::message::{
@@ -8,18 +8,20 @@ use twilight_model::channel::message::{
use crate::framework::interactions::{InteractionError, context::InteractionContext};
pub struct ActionRow {
components: Vec<(Option<String>, Arc<dyn Component>)>,
type ComponentWithID<T> = (Option<String>, Arc<dyn Component<T>>);
pub struct ActionRow<T> {
components: Vec<ComponentWithID<T>>,
}
impl ActionRow {
pub fn new(components: &[(Option<String>, Arc<dyn Component>)]) -> Self {
impl<T> ActionRow<T> {
pub fn new(components: &[ComponentWithID<T>]) -> Self {
Self {
components: components.to_vec(),
}
}
pub fn add(&mut self, id: Option<String>, component: Arc<dyn Component>) {
pub fn add(&mut self, id: Option<String>, component: Arc<dyn Component<T>>) {
self.components.push((id, component));
}
@@ -27,6 +29,10 @@ impl ActionRow {
self.components.len()
}
pub fn is_empty(&self) -> bool {
self.components.is_empty()
}
pub fn build(self) -> TwilightComponent {
TwilightComponent::ActionRow(TwilightActionRow {
components: self
@@ -39,19 +45,19 @@ impl ActionRow {
}
#[async_trait]
pub trait Component: Send + Sync {
pub trait Component<T>: Send + Sync + Debug {
fn custom_id(&self) -> Option<String> {
Some(String::from("component"))
}
fn build(&self, id: Option<String>) -> TwilightComponent;
async fn execute(&self, ctx: InteractionContext) -> Result<(), InteractionError>;
async fn execute(&self, ctx: InteractionContext<T>) -> Result<(), InteractionError>;
}
#[async_trait]
impl<T> Component for T
impl<T, S: Send + Sync + 'static> Component<S> for T
where
T: Button,
T: Button<S>,
{
fn custom_id(&self) -> Option<String> {
Button::id(self)
@@ -73,13 +79,13 @@ where
})
}
async fn execute(&self, ctx: InteractionContext) -> Result<(), InteractionError> {
async fn execute(&self, ctx: InteractionContext<S>) -> Result<(), InteractionError> {
self.on_press(ctx).await
}
}
#[async_trait]
pub trait Button: Send + Sync {
pub trait Button<S: Send + Sync + 'static>: Send + Sync + Debug {
fn id(&self) -> Option<String> {
if self.url().is_none() {
Some(String::from("Button"))
@@ -97,7 +103,7 @@ pub trait Button: Send + Sync {
None
}
async fn on_press(&self, ctx: InteractionContext) -> Result<(), InteractionError> {
async fn on_press(&self, ctx: InteractionContext<S>) -> Result<(), InteractionError> {
ctx.defer().await
}
}