69 lines
1.8 KiB
Rust
69 lines
1.8 KiB
Rust
use std::sync::Arc;
|
|
|
|
use twilight_model::{
|
|
gateway::payload::incoming::InteractionCreate,
|
|
http::interaction::{InteractionResponse, InteractionResponseType},
|
|
id::{Id, marker::InteractionMarker},
|
|
};
|
|
|
|
use crate::framework::{
|
|
commands::messages::OriginalMessageUpdater, interactions::InteractionError, runtime::Runtime,
|
|
};
|
|
|
|
pub struct InteractionContext<T> {
|
|
pub event: InteractionCreate,
|
|
pub rt: Arc<Runtime<T>>,
|
|
pub prefix: String,
|
|
}
|
|
|
|
impl<T> InteractionContext<T> {
|
|
pub fn new(event: InteractionCreate, rt: Arc<Runtime<T>>, prefix: String) -> Self {
|
|
Self { event, rt, prefix }
|
|
}
|
|
|
|
pub async fn defer(&self) -> Result<(), InteractionError> {
|
|
self.rt
|
|
.http
|
|
.interaction(self.rt.app.id)
|
|
.create_response(
|
|
self.event.id,
|
|
&self.event.token,
|
|
&InteractionResponse {
|
|
data: None,
|
|
kind: InteractionResponseType::DeferredUpdateMessage,
|
|
},
|
|
)
|
|
.await
|
|
.map_err(|_| InteractionError::DeferringFailed)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn original_message(&self) -> Option<OriginalMessage<T>> {
|
|
self.event.message.as_ref().map(|_| OriginalMessage {
|
|
interaction_id: self.event.id,
|
|
interaction_token: self.event.token.clone(),
|
|
rt: self.rt.clone(),
|
|
})
|
|
}
|
|
}
|
|
|
|
pub struct OriginalMessage<T> {
|
|
interaction_id: Id<InteractionMarker>,
|
|
interaction_token: String,
|
|
rt: Arc<Runtime<T>>,
|
|
}
|
|
|
|
impl<T> OriginalMessage<T>
|
|
where
|
|
T: Send + Sync + 'static,
|
|
{
|
|
pub fn edit(&self) -> OriginalMessageUpdater<T> {
|
|
OriginalMessageUpdater::new(
|
|
self.interaction_id,
|
|
self.interaction_token.clone(),
|
|
self.rt.clone(),
|
|
)
|
|
}
|
|
}
|