use crate::types::component::Component;
use crate::types::embed::Embed;
pub struct Response {
kind: ResponseKind,
}
enum ResponseKind {
Empty,
Text(TextResponse),
Acknowledge,
File(FileResponse),
}
struct TextResponse {
content: String,
embeds: Vec<Embed>,
components: Vec<Component>,
ephemeral: bool,
}
pub struct FileResponse {
pub file: async_fs::File,
pub filename: Option<String>,
pub caption: Option<String>,
}
impl Response {
pub fn empty() -> Self {
Self {
kind: ResponseKind::Empty,
}
}
pub fn text(content: impl Into<String>) -> Self {
Self {
kind: ResponseKind::Text(TextResponse {
content: content.into(),
embeds: Vec::new(),
components: Vec::new(),
ephemeral: false,
}),
}
}
pub fn acknowledge() -> Self {
Self {
kind: ResponseKind::Acknowledge,
}
}
pub fn embed(embed: Embed) -> Self {
Self {
kind: ResponseKind::Text(TextResponse {
content: String::new(),
embeds: vec![embed],
components: Vec::new(),
ephemeral: false,
}),
}
}
pub fn with_embed(mut self, embed: Embed) -> Self {
if let ResponseKind::Text(ref mut text) = self.kind {
text.embeds.push(embed);
}
self
}
pub fn with_components(mut self, components: Vec<Component>) -> Self {
if let ResponseKind::Text(ref mut text) = self.kind {
text.components = components;
}
self
}
pub fn ephemeral(mut self) -> Self {
if let ResponseKind::Text(ref mut text) = self.kind {
text.ephemeral = true;
}
self
}
pub fn file(file: async_fs::File) -> Self {
Self {
kind: ResponseKind::File(FileResponse {
file,
filename: None,
caption: None,
}),
}
}
pub fn with_filename(mut self, name: impl Into<String>) -> Self {
if let ResponseKind::File(ref mut f) = self.kind {
f.filename = Some(name.into());
}
self
}
pub fn with_caption(mut self, caption: impl Into<String>) -> Self {
if let ResponseKind::File(ref mut f) = self.kind {
f.caption = Some(caption.into());
}
self
}
pub fn is_empty(&self) -> bool {
matches!(self.kind, ResponseKind::Empty)
}
pub fn is_acknowledge(&self) -> bool {
matches!(self.kind, ResponseKind::Acknowledge)
}
pub fn content(&self) -> Option<&str> {
match &self.kind {
ResponseKind::Text(t) => Some(&t.content),
_ => None,
}
}
pub fn embeds(&self) -> &[Embed] {
match &self.kind {
ResponseKind::Text(t) => &t.embeds,
_ => &[],
}
}
pub fn components(&self) -> &[Component] {
match &self.kind {
ResponseKind::Text(t) => &t.components,
_ => &[],
}
}
pub fn is_ephemeral(&self) -> bool {
match &self.kind {
ResponseKind::Text(t) => t.ephemeral,
_ => false,
}
}
pub fn is_file(&self) -> bool {
matches!(self.kind, ResponseKind::File(_))
}
pub fn take_file(&mut self) -> Option<FileResponse> {
match std::mem::replace(&mut self.kind, ResponseKind::Empty) {
ResponseKind::File(f) => Some(f),
other => {
self.kind = other;
None
}
}
}
}