use twilight_model::{
channel::message::{
Component,
MessageFlags,
component::{ActionRow, TextInput},
},
http::interaction::{InteractionResponse, InteractionResponseData, InteractionResponseType},
};
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct DeferInteractionResponseBuilder {
is_ephemeral: bool,
response_type: InteractionResponseType,
suppress_embeds: bool,
}
impl DeferInteractionResponseBuilder {
#[must_use]
pub fn build(self) -> InteractionResponse {
let mut flags = MessageFlags::empty();
if self.is_ephemeral {
flags.insert(MessageFlags::EPHEMERAL);
}
if self.suppress_embeds {
flags.insert(MessageFlags::SUPPRESS_EMBEDS);
}
InteractionResponse {
kind: self.response_type,
data: Some(InteractionResponseData {
allowed_mentions: None,
attachments: None,
choices: None,
components: None,
content: None,
custom_id: None,
embeds: None,
flags: (!flags.is_empty()).then_some(flags),
title: None,
tts: None,
}),
}
}
#[must_use]
pub const fn ephemeral(mut self) -> Self {
self.is_ephemeral = true;
self
}
#[must_use]
pub const fn suppress_embeds(mut self) -> Self {
self.suppress_embeds = true;
self
}
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct InteractionResponseBuilder;
impl InteractionResponseBuilder {
#[must_use]
pub const fn defer_send_message() -> DeferInteractionResponseBuilder {
DeferInteractionResponseBuilder {
response_type: InteractionResponseType::DeferredChannelMessageWithSource,
is_ephemeral: false,
suppress_embeds: false,
}
}
#[must_use]
pub const fn defer_update_message() -> DeferInteractionResponseBuilder {
DeferInteractionResponseBuilder {
response_type: InteractionResponseType::DeferredUpdateMessage,
is_ephemeral: false,
suppress_embeds: false,
}
}
#[must_use]
pub const fn pong() -> InteractionResponse {
InteractionResponse {
kind: InteractionResponseType::Pong,
data: None,
}
}
#[must_use]
pub const fn send_message(data: InteractionResponseData) -> InteractionResponse {
InteractionResponse {
kind: InteractionResponseType::ChannelMessageWithSource,
data: Some(data),
}
}
#[must_use]
pub const fn show_modal(title: String, custom_id: String) -> ModalInteractionResponseBuilder {
ModalInteractionResponseBuilder {
action_rows: vec![],
custom_id,
title,
}
}
#[must_use]
pub const fn update_message(data: InteractionResponseData) -> InteractionResponse {
InteractionResponse {
kind: InteractionResponseType::UpdateMessage,
data: Some(data),
}
}
}
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct ModalInteractionResponseBuilder {
action_rows: Vec<ActionRow>,
custom_id: String,
title: String,
}
impl ModalInteractionResponseBuilder {
pub fn build(self) -> InteractionResponse {
InteractionResponse {
kind: InteractionResponseType::Modal,
data: Some(InteractionResponseData {
allowed_mentions: None,
attachments: None,
choices: None,
components: Some(
self.action_rows
.into_iter()
.map(Component::ActionRow)
.collect(),
),
content: None,
custom_id: Some(self.custom_id),
embeds: None,
flags: None,
title: Some(self.title),
tts: None,
}),
}
}
#[must_use]
pub fn text_input(mut self, text_input: TextInput) -> Self {
self.action_rows.push(ActionRow {
components: vec![Component::TextInput(text_input)],
});
self
}
}