tulpje-framework 0.17.0

Multi-purpose discord bot & framework
Documentation
use std::sync::Arc;

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

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 standby: Arc<Standby>,

    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,
            standby: ctx.standby,

            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
    }
}