mod builder;
pub use builder::*;
mod send_reply;
pub use send_reply::*;
use crate::serenity_prelude as serenity;
use std::borrow::Cow;
#[derive(Clone)]
enum ReplyHandleInner<'a> {
Prefix(Box<serenity::Message>),
Application {
http: &'a serenity::Http,
interaction: &'a serenity::CommandInteraction,
followup: Option<Box<serenity::Message>>,
},
Autocomplete,
}
#[derive(Clone)]
pub struct ReplyHandle<'a>(ReplyHandleInner<'a>);
impl ReplyHandle<'_> {
pub async fn into_message(self) -> Result<serenity::Message, serenity::Error> {
use ReplyHandleInner::*;
match self.0 {
Prefix(msg)
| Application {
followup: Some(msg),
..
} => Ok(*msg),
Application {
http,
interaction,
followup: None,
} => interaction.get_response(http).await,
Autocomplete => panic!("reply is a no-op in autocomplete context"),
}
}
pub async fn message(&self) -> Result<Cow<'_, serenity::Message>, serenity::Error> {
use ReplyHandleInner::*;
match &self.0 {
Prefix(msg)
| Application {
followup: Some(msg),
..
} => Ok(Cow::Borrowed(msg)),
Application {
http,
interaction,
followup: None,
} => Ok(Cow::Owned(interaction.get_response(http).await?)),
Autocomplete => panic!("reply is a no-op in autocomplete context"),
}
}
pub async fn edit<U, E>(
&self,
ctx: crate::Context<'_, U, E>,
builder: CreateReply,
) -> Result<(), serenity::Error> {
let reply = ctx.reply_builder(builder);
match &self.0 {
ReplyHandleInner::Prefix(msg) => {
msg.clone()
.edit(ctx.serenity_context(), {
reply.to_prefix_edit(serenity::EditMessage::new())
})
.await?;
}
ReplyHandleInner::Application {
http,
interaction,
followup: None,
} => {
let builder =
reply.to_slash_initial_response_edit(serenity::EditInteractionResponse::new());
interaction.edit_response(http, builder).await?;
}
ReplyHandleInner::Application {
http,
interaction,
followup: Some(msg),
} => {
let builder = reply
.to_slash_followup_response(serenity::CreateInteractionResponseFollowup::new());
interaction.edit_followup(http, msg.id, builder).await?;
}
ReplyHandleInner::Autocomplete => panic!("reply is a no-op in autocomplete context"),
}
Ok(())
}
pub async fn delete<U, E>(&self, ctx: crate::Context<'_, U, E>) -> Result<(), serenity::Error> {
match &self.0 {
ReplyHandleInner::Prefix(msg) => msg.delete(ctx.serenity_context()).await?,
ReplyHandleInner::Application {
http: _,
interaction,
followup,
} => match followup {
Some(followup) => {
interaction.delete_followup(ctx, followup.id).await?;
}
None => {
interaction.delete_response(ctx).await?;
}
},
ReplyHandleInner::Autocomplete => panic!("delete is a no-op in autocomplete context"),
}
Ok(())
}
}