Skip to main content

twilight_model/guild/audit_log/
mod.rs

1//! Audit Logs, created whenever an administrative action is performed within a
2//! guild.
3//!
4//! For additional information refer to [Discord Docs/Audit Logs].
5//!
6//! # Merging
7//!
8//! Certain audit log actions support merging, so requests by the same user in
9//! quick succession update the data of the original entry, rather than always
10//! creating a new one. Only the reason of the first request in the batch is kept,
11//! and subsequent updates do not fire a Gateway event.
12//!
13//! The following event types get merged:
14//!
15//! - [`AuditLogEventType::ChannelPositionUpdate`]
16//! - [`AuditLogEventType::MemberDisconnect`]
17//! - [`AuditLogEventType::MemberMove`]
18//! - [`AuditLogEventType::MessageDelete`]
19//! - [`AuditLogEventType::MessageBulkDelete`]
20//! - [`AuditLogEventType::RolePositionUpdate`]
21//!
22//! [Discord Docs/Audit Logs]: https://discord.com/developers/docs/resources/audit-log
23
24mod change;
25mod change_key;
26mod entry;
27mod event_type;
28mod integration;
29mod optional_entry_info;
30
31pub use self::{
32    change::{AffectedRole, AuditLogChange, AuditLogChangeTypeValue},
33    change_key::AuditLogChangeKey,
34    entry::AuditLogEntry,
35    event_type::AuditLogEventType,
36    integration::AuditLogGuildIntegration,
37    optional_entry_info::AuditLogOptionalEntryInfo,
38};
39
40use super::auto_moderation::AutoModerationRule;
41use crate::{
42    application::command::Command,
43    channel::{Channel, Webhook},
44    guild::scheduled_event::GuildScheduledEvent,
45    user::User,
46};
47use serde::{Deserialize, Serialize};
48
49/// Paginated audit log entries with additional information.
50///
51/// For additional information refer to [Discord Docs/Audit Logs][1].
52///
53/// [1]: https://discord.com/developers/docs/resources/audit-log#audit-log-object
54#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
55pub struct AuditLog {
56    /// List of referenced application commands.
57    pub application_commands: Vec<Command>,
58    /// List of referenced auto moderation rules.
59    pub auto_moderation_rules: Vec<AutoModerationRule>,
60    /// Paginated entries in a guild's audit log.
61    #[serde(rename = "audit_log_entries")]
62    pub entries: Vec<AuditLogEntry>,
63    /// Information about mentioned scheduled events.
64    pub guild_scheduled_events: Vec<GuildScheduledEvent>,
65    /// Information about mentioned integrations.
66    pub integrations: Vec<AuditLogGuildIntegration>,
67    /// Information about mentioned threads.
68    pub threads: Vec<Channel>,
69    /// Information about mentioned users.
70    ///
71    /// For example, [users that performed the action][`AuditLogEntry::user_id`]
72    /// to create an entry are in this list.
73    pub users: Vec<User>,
74    /// Information about mentioned webhooks.
75    pub webhooks: Vec<Webhook>,
76}
77
78#[cfg(test)]
79mod tests {
80    use super::AuditLog;
81    use serde::{Deserialize, Serialize};
82    use serde_test::Token;
83    use static_assertions::{assert_fields, assert_impl_all};
84    use std::fmt::Debug;
85
86    assert_fields!(
87        AuditLog: entries,
88        guild_scheduled_events,
89        integrations,
90        users,
91        webhooks
92    );
93    assert_impl_all!(
94        AuditLog: Clone,
95        Debug,
96        Deserialize<'static>,
97        PartialEq,
98        Send,
99        Serialize,
100        Sync
101    );
102
103    /// Test the (de)serialization of an audit log.
104    ///
105    /// We don't need to test with values since they're individually tested, so
106    /// we just need to test that fields are present in deserialization and
107    /// serialization as expected.
108    #[test]
109    fn serde() {
110        let value = AuditLog {
111            application_commands: Vec::new(),
112            auto_moderation_rules: Vec::new(),
113            entries: Vec::new(),
114            guild_scheduled_events: Vec::new(),
115            integrations: Vec::new(),
116            threads: Vec::new(),
117            users: Vec::new(),
118            webhooks: Vec::new(),
119        };
120
121        serde_test::assert_tokens(
122            &value,
123            &[
124                Token::Struct {
125                    name: "AuditLog",
126                    len: 8,
127                },
128                Token::Str("application_commands"),
129                Token::Seq { len: Some(0) },
130                Token::SeqEnd,
131                Token::Str("auto_moderation_rules"),
132                Token::Seq { len: Some(0) },
133                Token::SeqEnd,
134                Token::Str("audit_log_entries"),
135                Token::Seq { len: Some(0) },
136                Token::SeqEnd,
137                Token::Str("guild_scheduled_events"),
138                Token::Seq { len: Some(0) },
139                Token::SeqEnd,
140                Token::Str("integrations"),
141                Token::Seq { len: Some(0) },
142                Token::SeqEnd,
143                Token::Str("threads"),
144                Token::Seq { len: Some(0) },
145                Token::SeqEnd,
146                Token::Str("users"),
147                Token::Seq { len: Some(0) },
148                Token::SeqEnd,
149                Token::Str("webhooks"),
150                Token::Seq { len: Some(0) },
151                Token::SeqEnd,
152                Token::StructEnd,
153            ],
154        )
155    }
156}