tulpje_framework/context/
component_interaction_context.rs1use std::sync::Arc;
2
3use twilight_http::{client::InteractionClient, response::marker::EmptyBody, Client};
4use twilight_model::{
5 application::interaction::message_component::MessageComponentInteractionData,
6 gateway::payload::incoming::InteractionCreate,
7 guild::Guild,
8 http::interaction::InteractionResponse,
9 id::{marker::ApplicationMarker, Id},
10};
11
12use super::Context;
13use crate::{Error, Metadata};
14
15#[derive(Clone, Debug)]
16pub struct ComponentInteractionContext<T: Clone + Send + Sync> {
17 pub meta: Metadata,
18 pub application_id: Id<ApplicationMarker>,
19 pub services: Arc<T>,
20 pub client: Arc<Client>,
21
22 pub event: InteractionCreate,
23 pub interaction: MessageComponentInteractionData,
24}
25
26impl<T: Clone + Send + Sync> ComponentInteractionContext<T> {
27 pub fn from_context(
28 ctx: Context<T>,
29 meta: Metadata,
30 event: InteractionCreate,
31 interaction: MessageComponentInteractionData,
32 ) -> Self {
33 Self {
34 application_id: ctx.application_id,
35 client: ctx.client,
36 services: ctx.services,
37
38 meta,
39 interaction,
40 event,
41 }
42 }
43
44 pub fn interaction(&self) -> InteractionClient<'_> {
45 self.client.interaction(self.application_id)
46 }
47
48 pub async fn guild(&self) -> Result<Option<Guild>, Error> {
49 let Some(guild_id) = self.event.guild_id else {
50 return Ok(None);
51 };
52
53 Ok(Some(self.client.guild(guild_id).await?.model().await?))
54 }
55
56 pub async fn response(
57 &self,
58 response: InteractionResponse,
59 ) -> Result<twilight_http::Response<EmptyBody>, twilight_http::Error> {
60 self.interaction()
61 .create_response(self.event.id, &self.event.token, &response)
62 .await
63 }
64}