use twilight_model::channel::Message;
use twilight_model::channel::message::Embed;
use twilight_model::id::Id;
use twilight_model::id::marker::{ChannelMarker, MessageMarker};
use crate::aliases::DiscordClient;
use crate::utils::DynFuture;
use crate::wrappers::TwilightError;
pub struct MessageCreate {
client: DiscordClient,
channel_id: Id<ChannelMarker>,
content: String,
replying_to: Option<Id<MessageMarker>>,
embeds: Vec<Embed>,
}
impl MessageCreate {
pub(crate) fn new(
client: DiscordClient,
channel_id: Id<ChannelMarker>,
content: impl Into<String>,
) -> Self {
Self {
client,
channel_id,
content: content.into(),
replying_to: None,
embeds: Vec::new(),
}
}
pub fn reply(mut self, message_id: Id<MessageMarker>) -> Self {
self.replying_to = Some(message_id);
self
}
pub fn embed(mut self, embed: impl Into<Embed>) -> Self {
self.embeds.push(embed.into());
self
}
async fn send(self) -> Result<Message, TwilightError> {
let mut builder = self
.client
.create_message(self.channel_id)
.embeds(&self.embeds)
.content(&self.content);
if let Some(reply) = self.replying_to {
builder = builder.reply(reply);
}
Ok(builder.await?.model().await?)
}
}
impl IntoFuture for MessageCreate {
type Output = Result<Message, TwilightError>;
type IntoFuture = DynFuture<'static, Self::Output>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(self.send())
}
}