#[cfg(feature = "http")]
use super::Builder;
#[cfg(feature = "http")]
use crate::http::{CacheHttp, MessagePagination};
#[cfg(feature = "http")]
use crate::internal::prelude::*;
use crate::model::prelude::*;
#[derive(Clone, Copy, Debug, Default)]
#[must_use]
pub struct GetMessages {
search_filter: Option<SearchFilter>,
limit: Option<u8>,
}
impl GetMessages {
pub fn new() -> Self {
Self::default()
}
pub fn after(mut self, message_id: impl Into<MessageId>) -> Self {
self.search_filter = Some(SearchFilter::After(message_id.into()));
self
}
pub fn around(mut self, message_id: impl Into<MessageId>) -> Self {
self.search_filter = Some(SearchFilter::Around(message_id.into()));
self
}
pub fn before(mut self, message_id: impl Into<MessageId>) -> Self {
self.search_filter = Some(SearchFilter::Before(message_id.into()));
self
}
pub fn limit(mut self, limit: u8) -> Self {
self.limit = Some(limit.min(100));
self
}
}
#[cfg(feature = "http")]
#[async_trait::async_trait]
impl Builder for GetMessages {
type Context<'ctx> = ChannelId;
type Built = Vec<Message>;
async fn execute(
self,
cache_http: impl CacheHttp,
ctx: Self::Context<'_>,
) -> Result<Self::Built> {
cache_http.http().get_messages(ctx, self.search_filter.map(Into::into), self.limit).await
}
}
#[derive(Clone, Copy, Debug)]
enum SearchFilter {
After(MessageId),
Around(MessageId),
Before(MessageId),
}
#[cfg(feature = "http")]
impl From<SearchFilter> for MessagePagination {
fn from(filter: SearchFilter) -> Self {
match filter {
SearchFilter::After(id) => MessagePagination::After(id),
SearchFilter::Around(id) => MessagePagination::Around(id),
SearchFilter::Before(id) => MessagePagination::Before(id),
}
}
}