spectacles_model/guild/
audit_log.rs

1use serde_json::value::RawValue;
2use serde_repr::{Deserialize_repr, Serialize_repr};
3
4use crate::message::Webhook;
5use crate::snowflake::Snowflake;
6use crate::User;
7
8/// Represents a guild's audit log on Discord.
9#[derive(Clone, Debug, Deserialize, Serialize)]
10pub struct GuildAuditLog {
11    /// A list of webhooks that were found in this audit log.
12    pub webhooks: Vec<Webhook>,
13    /// A list of users that were found in this audit log.
14    pub users: Vec<User>,
15    /// A collection of guild audit log entries.
16    #[serde(rename = "audit_log_entries")]
17    pub entries: Vec<GuildAuditLogEntry>,
18}
19
20/// An entry contained in a guild's audit log, which holds details about the audit log action.
21#[derive(Clone, Debug, Deserialize, Serialize)]
22pub struct GuildAuditLogEntry {
23    /// The snowflake ID of the affected entity.
24    pub target_id: Option<String>,
25    /// The changes that were made to the target ID.
26    #[serde(default)]
27    pub changes: Option<Vec<GuildAuditLogChange>>,
28    /// The user who performaned the changes.
29    pub user_id: Snowflake,
30    /// The ID that this entry belongs to.
31    pub id: Snowflake,
32    /// The type of action which occured.
33    pub action_type: GuildAuditLogEvent,
34    /// Additional options for certain action types.
35    pub options: Option<GuildAuditEntryInfo>,
36    /// The reason for this change.
37    pub reason: Option<String>,
38}
39
40#[derive(Clone, Debug, Deserialize, Serialize)]
41pub struct GuildAuditLogChange {
42    /// The new value of the key.
43    /// Note: Due to the mixed nature of this field, a raw JSON string is returned, instead of an object. You are responsible for parsing the JSON to the appropriate struct.
44    pub new_value: Box<RawValue>,
45    /// The old value of this key.
46    /// /// Note: Due to the mixed nature of this field, a raw JSON string is returned, instead of an object. You are responsible for parsing the JSON to the appropriate struct.
47    pub old_value: Box<RawValue>,
48    // The audit log change key.
49    // pub key: GuildAuditLogChangeKey
50}
51
52#[derive(Clone, Debug, Deserialize, Serialize)]
53pub struct GuildAuditEntryInfo {
54    /// The number of days after which inactive members were kicked from the guild.
55    /// This action is sent with the MEMBER_PRUNE audit log event.
56    #[serde(default)]
57    pub delete_member_days: Option<String>,
58    /// The number of members removed by the prune.
59    /// This action is sent with the MEMBER_PRUNE audit log event.,
60    #[serde(default)]
61    pub members_removed: Option<String>,
62    /// The channel ID in which the messages were deleted. Sent with the MESSAGE_DELETE event.
63    #[serde(default)]
64    pub channel_id: Option<Snowflake>,
65    /// The number of messages which were deleted. Sent with the MESSAGE_DELETE event.
66    #[serde(default)]
67    pub count: Option<String>,
68    /// The ID of the overwritten entity, found in a channel overwrite.
69    #[serde(default)]
70    pub id: Option<Snowflake>,
71    /// The type of the overwritten entity, sent with the channel overwrite events.
72    #[serde(default, rename = "type")]
73    pub kind: Option<String>,
74    /// The name of the role found in the channel overwrite.
75    #[serde(default)]
76    pub role_name: Option<String>,
77}
78
79// TODO: Figure out change key
80#[derive(Clone, Debug, Deserialize, Serialize)]
81pub enum GuildAuditLogChangeKey {
82    Name(String),
83    IconHash(String),
84    SplashHash(String),
85}
86
87#[derive(Clone, Debug, Deserialize_repr, Serialize_repr)]
88#[repr(u16)]
89pub enum GuildAuditLogEvent {
90    GuildUpdate = 1,
91    ChannelCreate = 10,
92    ChannelUpdate,
93    ChannelDelete,
94    ChannelOverwriteCreate,
95    ChannelOverwriteUpdate,
96    ChannelOverwriteDelete,
97    MemberKick = 20,
98    MemberPrune,
99    MemberBanAdd,
100    MemberBanRemove,
101    MemberUpdate,
102    MemberRoleUpdate,
103    RoleCreate = 30,
104    RoleUpdate,
105    RoleDelete,
106    InviteCreate = 40,
107    InviteUpdate,
108    InviteDelete,
109    WebhookCreate = 50,
110    WebhookUpdate,
111    WebhookDelete,
112    EmojiCreate = 60,
113    EmojiUpdate,
114    EmojiDelete,
115    MessageDelete = 72,
116}
117
118#[derive(Serialize, Debug, Default)]
119pub struct GetAuditLogOptions {
120    #[serde(skip_serializing_if = "Option::is_none")]
121    user_id: Option<Snowflake>,
122    #[serde(skip_serializing_if = "Option::is_none")]
123    action_type: Option<GuildAuditLogEvent>,
124    #[serde(skip_serializing_if = "Option::is_none")]
125    before: Option<Snowflake>,
126    #[serde(skip_serializing_if = "Option::is_none")]
127    limit: Option<i32>,
128}
129
130impl GetAuditLogOptions {
131    /// Sets the user ID to filter audit log entries by.
132    pub fn user_id(mut self, id: Snowflake) -> Self {
133        self.user_id = Some(id);
134        self
135    }
136
137    /// Sets the action type of the audit logs being returned.
138    pub fn action_type(mut self, event: GuildAuditLogEvent) -> Self {
139        self.action_type = Some(event);
140        self
141    }
142
143    /// Sets the entry ID to filter log entries before.
144    pub fn before(mut self, id: Snowflake) -> Self {
145        self.before = Some(id);
146        self
147    }
148
149    /// Sets the amount of audit log entries to be returned in the request.
150    pub fn limit(mut self, num: i32) -> Self {
151        self.limit = Some(num);
152        self
153    }
154}