tulpje_framework/context/
component_interaction_context.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::sync::Arc;

use twilight_http::{client::InteractionClient, response::marker::EmptyBody, Client};
use twilight_model::{
    application::interaction::message_component::MessageComponentInteractionData,
    gateway::payload::incoming::InteractionCreate,
    guild::Guild,
    http::interaction::InteractionResponse,
    id::{marker::ApplicationMarker, Id},
};

use super::Context;
use crate::{Error, Metadata};

#[derive(Clone, Debug)]
pub struct ComponentInteractionContext<T: Clone + Send + Sync> {
    pub meta: Metadata,
    pub application_id: Id<ApplicationMarker>,
    pub services: Arc<T>,
    pub client: Arc<Client>,

    pub event: InteractionCreate,
    pub interaction: MessageComponentInteractionData,
}

impl<T: Clone + Send + Sync> ComponentInteractionContext<T> {
    pub fn from_context(
        ctx: Context<T>,
        meta: Metadata,
        event: InteractionCreate,
        interaction: MessageComponentInteractionData,
    ) -> Self {
        Self {
            application_id: ctx.application_id,
            client: ctx.client,
            services: ctx.services,

            meta,
            interaction,
            event,
        }
    }

    pub fn interaction(&self) -> InteractionClient<'_> {
        self.client.interaction(self.application_id)
    }

    pub async fn guild(&self) -> Result<Option<Guild>, Error> {
        let Some(guild_id) = self.event.guild_id else {
            return Ok(None);
        };

        Ok(Some(self.client.guild(guild_id).await?.model().await?))
    }

    pub async fn response(
        &self,
        response: InteractionResponse,
    ) -> Result<twilight_http::Response<EmptyBody>, twilight_http::Error> {
        self.interaction()
            .create_response(self.event.id, &self.event.token, &response)
            .await
    }
}