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 -28
View File
@@ -19,27 +19,30 @@ use twilight_model::{
use twilight_util::builder::InteractionResponseDataBuilder;
use crate::framework::{
handlers::Handlers,
interactions::components::{ActionRow, Component},
runtime::Runtime,
};
pub struct MessageSender {
pub struct MessageSender<T> {
channel_id: Id<ChannelMarker>,
handlers: Arc<Handlers>,
rt: Arc<Runtime<T>>,
content: Option<String>,
embeds: Vec<Embed>,
// Row index, component
components: Vec<(u8, Arc<dyn Component>)>,
components: Vec<(u8, Arc<dyn Component<T>>)>,
}
impl MessageSender {
pub fn new(channel_id: Id<ChannelMarker>, handlers: Arc<Handlers>) -> Self {
impl<T> MessageSender<T>
where
T: Send + Sync + 'static,
{
pub fn new(channel_id: Id<ChannelMarker>, rt: Arc<Runtime<T>>) -> Self {
Self {
channel_id,
handlers,
rt,
content: None,
components: vec![],
embeds: vec![],
@@ -58,14 +61,14 @@ impl MessageSender {
self
}
pub fn component(mut self, component: impl Component + 'static, row: u8) -> Self {
pub fn component(mut self, component: impl Component<T> + 'static, row: u8) -> Self {
self.components.push((row, Arc::new(component)));
self
}
pub async fn send(self) -> Result<Response<Message>, Error> {
let mut builder = self.handlers.http.create_message(self.channel_id);
let mut builder = self.rt.http.create_message(self.channel_id);
if let Some(content) = &self.content {
builder = builder.content(content);
@@ -92,9 +95,7 @@ impl MessageSender {
if let Some(custom_id) = &custom_id {
custom_ids.push(custom_id.clone());
self.handlers
.registry
.register(custom_id, component.clone());
self.rt.registry.register(custom_id, component.clone());
}
current_row.add(custom_id.clone(), component.clone());
@@ -111,7 +112,7 @@ impl MessageSender {
if result.is_err() {
for custom_id in custom_ids {
self.handlers.registry.unregister(custom_id);
self.rt.registry.unregister(custom_id);
}
}
@@ -119,7 +120,7 @@ impl MessageSender {
}
}
impl IntoFuture for MessageSender {
impl<T: Send + Sync + 'static> IntoFuture for MessageSender<T> {
type Output = Result<Response<Message>, Error>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
@@ -128,29 +129,32 @@ impl IntoFuture for MessageSender {
}
}
pub struct OriginalMessageUpdater {
pub struct OriginalMessageUpdater<T> {
interaction_id: Id<InteractionMarker>,
interaction_token: String,
handlers: Arc<Handlers>,
rt: Arc<Runtime<T>>,
content: Option<String>,
embeds: Vec<Embed>,
// Row index, component
components: Vec<(u8, Arc<dyn Component>)>,
components: Vec<(u8, Arc<dyn Component<T>>)>,
}
impl OriginalMessageUpdater {
impl<T> OriginalMessageUpdater<T>
where
T: Send + Sync + 'static,
{
pub fn new(
interaction_id: Id<InteractionMarker>,
interaction_token: String,
handlers: Arc<Handlers>,
rt: Arc<Runtime<T>>,
) -> Self {
Self {
interaction_id,
interaction_token,
handlers,
rt,
content: None,
components: vec![],
embeds: vec![],
@@ -169,7 +173,7 @@ impl OriginalMessageUpdater {
self
}
pub fn component(mut self, component: impl Component + 'static, row: u8) -> Self {
pub fn component(mut self, component: impl Component<T> + 'static, row: u8) -> Self {
self.components.push((row, Arc::new(component)));
self
@@ -203,9 +207,7 @@ impl OriginalMessageUpdater {
if let Some(custom_id) = &custom_id {
custom_ids.push(custom_id.clone());
self.handlers
.registry
.register(custom_id, component.clone());
self.rt.registry.register(custom_id, component.clone());
}
current_row.add(custom_id.clone(), component.clone());
@@ -219,9 +221,9 @@ impl OriginalMessageUpdater {
builder = builder.embeds(self.embeds);
let result = self
.handlers
.rt
.http
.interaction(self.handlers.app.id)
.interaction(self.rt.app.id)
.create_response(
self.interaction_id,
&self.interaction_token,
@@ -234,7 +236,7 @@ impl OriginalMessageUpdater {
if result.is_err() {
for custom_id in custom_ids {
self.handlers.registry.unregister(custom_id);
self.rt.registry.unregister(custom_id);
}
}
@@ -242,7 +244,7 @@ impl OriginalMessageUpdater {
}
}
impl IntoFuture for OriginalMessageUpdater {
impl<T: Send + Sync + 'static> IntoFuture for OriginalMessageUpdater<T> {
type Output = Result<Response<EmptyBody>, Error>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;