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