twilight_http/request/guild/get_audit_log.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
use crate::{
client::Client,
error::Error,
request::{Request, TryIntoRequest},
response::{Response, ResponseFuture},
routing::Route,
};
use std::future::IntoFuture;
use twilight_model::{
guild::audit_log::{AuditLog, AuditLogEventType},
id::{
marker::{GuildMarker, UserMarker},
Id,
},
};
use twilight_validate::request::{
get_guild_audit_log_limit as validate_get_guild_audit_log_limit, ValidationError,
};
struct GetAuditLogFields {
action_type: Option<AuditLogEventType>,
after: Option<u64>,
before: Option<u64>,
limit: Option<u16>,
user_id: Option<Id<UserMarker>>,
}
/// Get the audit log for a guild.
///
/// # Examples
///
/// ```no_run
/// use twilight_http::Client;
/// use twilight_model::id::Id;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("token".to_owned());
///
/// let guild_id = Id::new(101);
/// let audit_log = client.audit_log(guild_id).await?.model().await?;
///
/// for entry in audit_log.entries {
/// println!("ID: {}", entry.id);
/// println!(" Action Type: {}", u16::from(entry.action_type));
/// println!(" Changes:");
///
/// for change in entry.changes {
/// println!("{change:?}");
/// }
/// }
/// # Ok(()) }
/// ```
#[must_use = "requests must be configured and executed"]
pub struct GetAuditLog<'a> {
fields: Result<GetAuditLogFields, ValidationError>,
guild_id: Id<GuildMarker>,
http: &'a Client,
}
impl<'a> GetAuditLog<'a> {
pub(crate) const fn new(http: &'a Client, guild_id: Id<GuildMarker>) -> Self {
Self {
fields: Ok(GetAuditLogFields {
action_type: None,
after: None,
before: None,
limit: None,
user_id: None,
}),
guild_id,
http,
}
}
/// Filter by an action type.
pub fn action_type(mut self, action_type: AuditLogEventType) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.action_type = Some(action_type);
}
self
}
/// Get audit log entries after the entry specified.
pub fn after(mut self, after: u64) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.after = Some(after);
}
self
}
/// Get audit log entries before the entry specified.
pub fn before(mut self, before: u64) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.before = Some(before);
}
self
}
/// Set the maximum number of audit logs to retrieve.
///
/// The minimum is 1 and the maximum is 100.
///
/// # Errors
///
/// Returns an error of type [`GetGuildAuditLog`] if the `limit` is 0 or
/// greater than 100.
///
/// [`GetGuildAuditLog`]: twilight_validate::request::ValidationErrorType::GetGuildAuditLog
pub fn limit(mut self, limit: u16) -> Self {
self.fields = self.fields.and_then(|mut fields| {
validate_get_guild_audit_log_limit(limit)?;
fields.limit = Some(limit);
Ok(fields)
});
self
}
/// Filter audit log for entries from a user.
///
/// This is the user who did the auditable action, not the target of the auditable action.
pub fn user_id(mut self, user_id: Id<UserMarker>) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.user_id = Some(user_id);
}
self
}
}
impl IntoFuture for GetAuditLog<'_> {
type Output = Result<Response<AuditLog>, Error>;
type IntoFuture = ResponseFuture<AuditLog>;
fn into_future(self) -> Self::IntoFuture {
let http = self.http;
match self.try_into_request() {
Ok(request) => http.request(request),
Err(source) => ResponseFuture::error(source),
}
}
}
impl TryIntoRequest for GetAuditLog<'_> {
fn try_into_request(self) -> Result<Request, Error> {
let fields = self.fields.map_err(Error::validation)?;
Ok(Request::from_route(&Route::GetAuditLogs {
action_type: fields.action_type.map(|x| u64::from(u16::from(x))),
after: fields.after,
before: fields.before,
guild_id: self.guild_id.get(),
limit: fields.limit,
user_id: fields.user_id.map(Id::get),
}))
}
}