github.com/diamondburned/arikawa/v2@v2.1.0/discord/auditlog.go (about)

     1  package discord
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	"github.com/diamondburned/arikawa/v2/utils/json"
     9  )
    10  
    11  // https://discord.com/developers/docs/resources/audit-log#audit-log-object
    12  type AuditLog struct {
    13  	// Webhooks is the list of webhooks found in the audit log.
    14  	Webhooks []Webhook `json:"webhooks"`
    15  	// Users is the list of users found in the audit log.
    16  	Users []User `json:"users"`
    17  	// Entries is the list of audit log entries.
    18  	Entries []AuditLogEntry `json:"audit_log_entries"`
    19  	// Integrations is a list ist of partial integration objects (only ID,
    20  	// Name, Type, and Account).
    21  	Integrations []Integration `json:"integrations"`
    22  }
    23  
    24  // AuditLogEntry is a single entry in the audit log.
    25  //
    26  // https://discord.com/developers/docs/resources/audit-log#audit-log-entry-object
    27  type AuditLogEntry struct {
    28  	// ID is the id of the entry.
    29  	ID AuditLogEntryID `json:"id"`
    30  	// TargetID is the id of the affected entity (webhook, user, role, etc.).
    31  	TargetID Snowflake `json:"target_id"`
    32  	// Changes are the changes made to the TargetID.
    33  	Changes []AuditLogChange `json:"changes,omitempty"`
    34  	// UserID is the id of the user who made the changes.
    35  	UserID UserID `json:"user_id"`
    36  
    37  	// ActionType is the type of action that occurred.
    38  	ActionType AuditLogEvent `json:"action_type"`
    39  
    40  	// Options contains additional info for certain action types.
    41  	Options AuditEntryInfo `json:"options,omitempty"`
    42  	// Reason is the reason for the change (0-512 characters).
    43  	Reason string `json:"reason,omitempty"`
    44  }
    45  
    46  // CreatedAt returns a time object representing when the audit log entry was created.
    47  func (e AuditLogEntry) CreatedAt() time.Time {
    48  	return e.ID.Time()
    49  }
    50  
    51  // AuditLogEvent is the type of audit log action that occurred.
    52  type AuditLogEvent uint8
    53  
    54  // https://discord.com/developers/docs/resources/audit-log#audit-log-entry-object-audit-log-events
    55  const (
    56  	GuildUpdate            AuditLogEvent = 1
    57  	ChannelCreate          AuditLogEvent = 10
    58  	ChannelUpdate          AuditLogEvent = 11
    59  	ChannelDelete          AuditLogEvent = 12
    60  	ChannelOverwriteCreate AuditLogEvent = 13
    61  	ChannelOverwriteUpdate AuditLogEvent = 14
    62  	ChannelOverwriteDelete AuditLogEvent = 15
    63  	MemberKick             AuditLogEvent = 20
    64  	MemberPrune            AuditLogEvent = 21
    65  	MemberBanAdd           AuditLogEvent = 22
    66  	MemberBanRemove        AuditLogEvent = 23
    67  	MemberUpdate           AuditLogEvent = 24
    68  	MemberRoleUpdate       AuditLogEvent = 25
    69  	MemberMove             AuditLogEvent = 26
    70  	MemberDisconnect       AuditLogEvent = 27
    71  	BotAdd                 AuditLogEvent = 28
    72  	RoleCreate             AuditLogEvent = 30
    73  	RoleUpdate             AuditLogEvent = 31
    74  	RoleDelete             AuditLogEvent = 32
    75  	InviteCreate           AuditLogEvent = 40
    76  	InviteUpdate           AuditLogEvent = 41
    77  	InviteDelete           AuditLogEvent = 42
    78  	WebhookCreate          AuditLogEvent = 50
    79  	WebhookUpdate          AuditLogEvent = 51
    80  	WebhookDelete          AuditLogEvent = 52
    81  	EmojiCreate            AuditLogEvent = 60
    82  	EmojiUpdate            AuditLogEvent = 61
    83  	EmojiDelete            AuditLogEvent = 62
    84  	MessageDelete          AuditLogEvent = 72
    85  	MessageBulkDelete      AuditLogEvent = 73
    86  	MessagePin             AuditLogEvent = 74
    87  	MessageUnpin           AuditLogEvent = 75
    88  	IntegrationCreate      AuditLogEvent = 80
    89  	IntegrationUpdate      AuditLogEvent = 81
    90  	IntegrationDelete      AuditLogEvent = 82
    91  )
    92  
    93  // https://discord.com/developers/docs/resources/audit-log#audit-log-entry-object-optional-audit-entry-info
    94  type AuditEntryInfo struct {
    95  	// DeleteMemberDays is the number of days after which inactive members were
    96  	// kicked.
    97  	//
    98  	// Events: MEMBER_PRUNE
    99  	DeleteMemberDays string `json:"delete_member_days,omitempty"`
   100  	// MembersRemoved is the number of members removed by the prune.
   101  	//
   102  	// Events: MEMBER_PRUNE
   103  	MembersRemoved string `json:"members_removed,omitempty"`
   104  	// ChannelID is the id of the channel in which the entities were targeted.
   105  	//
   106  	// Events: MEMBER_MOVE, MESSAGE_PIN, MESSAGE_UNPIN, MESSAGE_DELETE
   107  	ChannelID ChannelID `json:"channel_id,omitempty"`
   108  	// MessagesID is the id of the message that was targeted.
   109  	//
   110  	// Events: MESSAGE_PIN, MESSAGE_UNPIN
   111  	MessageID MessageID `json:"message_id,omitempty"`
   112  	// Count is the number of entities that were targeted.
   113  	//
   114  	// Events: MESSAGE_DELETE, MESSAGE_BULK_DELETE, MEMBER_DISCONNECT,
   115  	// MEMBER_MOVE
   116  	Count string `json:"count,omitempty"`
   117  	// ID is the id of the overwritten entity.
   118  	//
   119  	// Events: CHANNEL_OVERWRITE_CREATE, CHANNEL_OVERWRITE_UPDATE,
   120  	// CHANNEL_OVERWRITE_DELETE
   121  	ID Snowflake `json:"id,omitempty"`
   122  	// Type is the type of overwritten entity.
   123  	//
   124  	// Events: CHANNEL_OVERWRITE_CREATE, CHANNEL_OVERWRITE_UPDATE,
   125  	// CHANNEL_OVERWRITE_DELETE
   126  	Type OverwriteType `json:"type,string,omitempty"`
   127  	// RoleName is the name of the role if type is "role".
   128  	//
   129  	// Events: CHANNEL_OVERWRITE_CREATE, CHANNEL_OVERWRITE_UPDATE,
   130  	// CHANNEL_OVERWRITE_DELETE
   131  	RoleName string `json:"role_name,omitempty"`
   132  }
   133  
   134  // AuditLogChange is a single key type to changed value audit log entry. The
   135  // type can be found in the key's comment. Values can be nil.
   136  //
   137  // What
   138  //
   139  // I'm glad to see the same reaction that I had on you. In short, in this
   140  // struct, the Key dictates what type NewValue and OldValue will have. They will
   141  // always be the same type, but I will leave that as JSON for the user.
   142  //
   143  // Usage
   144  //
   145  // The usage of this is pretty simple, as AuditLogChange already has a
   146  // convenient method to use. Here's an example on how to do "owner_id":
   147  //
   148  //    if change.Key != discord.AuditGuildOwnerID {
   149  //        return errors.New("not owner ID")
   150  //    }
   151  //
   152  //    // We know these are UserIDs because the comment said so for AuditGuildOwnerID.
   153  //    var oldOwnerID, newOwnerID discord.UserID
   154  //    if err := change.UnmarshalValues(&oldOwnerID, &newOwnerID); err != nil {
   155  //        return err
   156  //    }
   157  //
   158  //    log.Println("Transferred ownership from user", oldOwnerID, "to", newOwnerID)
   159  //
   160  type AuditLogChange struct {
   161  	// Key is the name of audit log change key.
   162  	Key AuditLogChangeKey `json:"key"`
   163  	// NewValue is the new value of the key.
   164  	NewValue json.Raw `json:"new_value,omitempty"`
   165  	// OldValue is the old value of the key.
   166  	OldValue json.Raw `json:"old_value,omitempty"`
   167  }
   168  
   169  // UnmarshalValues unmarshals the values of the AuditLogChange into the passed
   170  // interfaces.
   171  func (a AuditLogChange) UnmarshalValues(old, new interface{}) error {
   172  	if err := a.NewValue.UnmarshalTo(new); err != nil {
   173  		return errors.Wrap(err, "failed to unmarshal old value")
   174  	}
   175  	if err := a.OldValue.UnmarshalTo(old); err != nil {
   176  		return errors.Wrap(err, "failed to unmarshal new value")
   177  	}
   178  	return nil
   179  }
   180  
   181  type AuditLogChangeKey string
   182  
   183  // https://discord.com/developers/docs/resources/audit-log#audit-log-change-object-audit-log-change-key
   184  const (
   185  	// AuditGuildName gets sent if the guild's name was changed.
   186  	//
   187  	// Type: string
   188  	AuditGuildName AuditLogChangeKey = "name"
   189  	// AuditGuildIconHash gets sent if the guild's icon was changed.
   190  	//
   191  	// Type: Hash
   192  	AuditGuildIconHash AuditLogChangeKey = "icon_hash"
   193  	// AuditGuildSplashHash gets sent if the guild's invite splash page artwork
   194  	// was changed.
   195  	//
   196  	// Type: Hash
   197  	AuditGuildSplashHash AuditLogChangeKey = "splash_hash"
   198  	// AuditGuildOwnerID gets sent if the guild's owner changed.
   199  	//
   200  	// Type: UserID
   201  	AuditGuildOwnerID AuditLogChangeKey = "owner_id"
   202  	// AuditGuildRegion gets sent if the guild's region changed.
   203  	//
   204  	// Type: string
   205  	AuditGuildRegion AuditLogChangeKey = "region"
   206  	// AuditGuildAFKChannelID gets sent if the guild's afk channel changed.
   207  	//
   208  	// Type: ChannelID
   209  	AuditGuildAFKChannelID AuditLogChangeKey = "afk_channel_id"
   210  	// AuditGuildAFKTimeout gets sent if the guild's afk timeout duration
   211  	// changed.
   212  	//
   213  	// Type: Seconds
   214  	AuditGuildAFKTimeout AuditLogChangeKey = "afk_timeout"
   215  	// AuditGuildMFA gets sent if the two-factor auth requirement changed.
   216  	//
   217  	// Type: MFALevel
   218  	AuditGuildMFA AuditLogChangeKey = "mfa_level"
   219  	// AuditGuildVerification gets sent if the guild's required verification
   220  	// level changed
   221  	//
   222  	// Type: Verification
   223  	AuditGuildVerification AuditLogChangeKey = "verification_level"
   224  	// AuditGuildExplicitFilter gets sent if there was a change in whose
   225  	// messages are scanned and deleted for explicit content in the server.
   226  	//
   227  	// Type: ExplicitFilter
   228  	AuditGuildExplicitFilter AuditLogChangeKey = "explicit_content_filter"
   229  	// AuditGuildNotification gets sent if the default message notification
   230  	// level changed.
   231  	//
   232  	// Type: Notification
   233  	AuditGuildNotification AuditLogChangeKey = "default_message_notifications"
   234  	// AuditGuildVanityURLCode gets sent if the guild invite vanity URL
   235  	// changed.
   236  	//
   237  	// Type: string
   238  	AuditGuildVanityURLCode AuditLogChangeKey = "vanity_url_code"
   239  	// AuditGuildRoleAdd gets sent if a new role was added.
   240  	//
   241  	// Type: []Role{ID, Name}
   242  	AuditGuildRoleAdd AuditLogChangeKey = "$add"
   243  	// AuditGuildRoleRemove gets sent if a role was removed.
   244  	//
   245  	// Type: []Role{ID, Name}
   246  	AuditGuildRoleRemove AuditLogChangeKey = "$remove"
   247  	// AuditGuildPruneDeleteDays gets sent if there was a change in number of
   248  	// days after which inactive and role-unassigned members are kicked.
   249  	//
   250  	// Type: int
   251  	AuditGuildPruneDeleteDays AuditLogChangeKey = "prune_delete_days"
   252  	// AuditGuildWidgetEnabled gets sent if the guild's widget was
   253  	// enabled/disabled.
   254  	//
   255  	// Type: bool
   256  	AuditGuildWidgetEnabled AuditLogChangeKey = "widget_enabled"
   257  	// AuditGuildWidgetChannelID gets sent if the channel ID of the guild
   258  	// widget changed.
   259  	//
   260  	// Type: ChannelID
   261  	AuditGuildWidgetChannelID AuditLogChangeKey = "widget_channel_id"
   262  	// AuditGuildSystemChannelID gets sent if the ID of the guild's system
   263  	// channel changed.
   264  	//
   265  	// Type: ChannelID
   266  	AuditGuildSystemChannelID AuditLogChangeKey = "system_channel_id"
   267  )
   268  
   269  const (
   270  	// AuditChannelPosition gets sent if a text or voice channel position was
   271  	// changed.
   272  	//
   273  	// Type: int
   274  	AuditChannelPosition AuditLogChangeKey = "position"
   275  	// AuditChannelTopic gets sent if the text channel topic changed.
   276  	//
   277  	// Type: string
   278  	AuditChannelTopic AuditLogChangeKey = "topic"
   279  	// AuditChannelBitrate gets sent if the voice channel bitrate changed.
   280  	//
   281  	// Type: uint
   282  	AuditChannelBitrate AuditLogChangeKey = "bitrate"
   283  	// AuditChannelPermissionOverwrites gets sent if the permissions on a
   284  	// channel changed.
   285  	//
   286  	// Type: []Overwrite
   287  	AuditChannelPermissionOverwrites AuditLogChangeKey = "permission_overwrites"
   288  	// AuditChannelNSFW gets sent if the channel NSFW restriction changed.
   289  	//
   290  	// Type: bool
   291  	AuditChannelNSFW AuditLogChangeKey = "nsfw"
   292  	// AuditChannelApplicationID contains the application ID of the added or
   293  	// removed webhook or bot.
   294  	//
   295  	// Type: AppID
   296  	AuditChannelApplicationID AuditLogChangeKey = "application_id"
   297  	// AuditChannelRateLimitPerUser gets sent if the amount of seconds a user
   298  	// has to wait before sending another message changed.
   299  	//
   300  	// Type: Seconds
   301  	AuditChannelRateLimitPerUser AuditLogChangeKey = "rate_limit_per_user"
   302  )
   303  
   304  const (
   305  	// AuditRolePermissions gets sent if the permissions for a role changed.
   306  	//
   307  	// Type: Permissions
   308  	AuditRolePermissions AuditLogChangeKey = "permissions"
   309  	// AuditRoleColor gets sent if the role color changed.
   310  	//
   311  	// Type: Color
   312  	AuditRoleColor AuditLogChangeKey = "color"
   313  	// AuditRoleHoist gets sent if the role is now displayed/no longer
   314  	// displayed separate from online users.
   315  	//
   316  	// Type: bool
   317  	AuditRoleHoist AuditLogChangeKey = "hoist"
   318  	// AuditRoleMentionable gets sent if a role is now
   319  	// mentionable/unmentionable.
   320  	//
   321  	// Type: bool
   322  	AuditRoleMentionable AuditLogChangeKey = "mentionable"
   323  	// AuditRoleAllow gets sent if a permission on a text or voice channel was
   324  	// allowed for a role.
   325  	//
   326  	// Type: Permissions
   327  	AuditRoleAllow AuditLogChangeKey = "allow"
   328  	// AuditRoleDeny gets sent if a permission on a text or voice channel was
   329  	// denied for a role.
   330  	//
   331  	// Type: Permissions
   332  	AuditRoleDeny AuditLogChangeKey = "deny"
   333  )
   334  
   335  const (
   336  	// AuditInviteCode gets sent if an invite code changed.
   337  	//
   338  	// Type: string
   339  	AuditInviteCode AuditLogChangeKey = "code"
   340  	// AuditInviteChannelID gets sent if the channel for an invite code
   341  	// changed.
   342  	//
   343  	// Type: ChannelID
   344  	AuditInviteChannelID AuditLogChangeKey = "channel_id"
   345  	// AuditInviteInviterID specifies the person who created invite code
   346  	// changed.
   347  	//
   348  	// Type: UserID
   349  	AuditInviteInviterID AuditLogChangeKey = "inviter_id"
   350  	// AuditInviteMaxUses specifies the change to max number of times invite
   351  	// code can be used.
   352  	//
   353  	// Type: int
   354  	AuditInviteMaxUses AuditLogChangeKey = "max_uses"
   355  	// AuditInviteUses specifies the number of times invite code used changed.
   356  	//
   357  	// Type: int
   358  	AuditInviteUses AuditLogChangeKey = "uses"
   359  	// AuditInviteMaxAge specifies the how long invite code lasts
   360  	// changed.
   361  	//
   362  	// Type: Seconds
   363  	AuditInviteMaxAge AuditLogChangeKey = "max_age"
   364  	// AuditInviteTemporary specifies if an invite code is temporary/never
   365  	// expires.
   366  	//
   367  	// Type: bool
   368  	AuditInviteTemporary AuditLogChangeKey = "temporary"
   369  )
   370  
   371  const (
   372  	// AuditUserDeaf specifies if the user was server deafened/undeafened.
   373  	//
   374  	// Type: bool
   375  	AuditUserDeaf AuditLogChangeKey = "deaf"
   376  	// AuditUserMute specifies if the user was server muted/unmuted.
   377  	//
   378  	// Type: bool
   379  	AuditUserMute AuditLogChangeKey = "mute"
   380  	// AuditUserNick specifies the new nickname of the user.
   381  	//
   382  	// Type: string
   383  	AuditUserNick AuditLogChangeKey = "nick"
   384  	// AuditUserAvatar specifies the hash of the new user avatar.
   385  	//
   386  	// Type: Hash
   387  	AuditUserAvatarHash AuditLogChangeKey = "avatar_hash"
   388  )
   389  
   390  const (
   391  	// AuditAnyID specifies the ID of the changed entity - sometimes used in
   392  	// conjunction with other keys.
   393  	//
   394  	// Type: Snowflake
   395  	AuditAnyID AuditLogChangeKey = "id"
   396  	// AuditAnyType is the type of the entity created.
   397  	// Type ChannelType or string
   398  	AuditAnyType AuditLogChangeKey = "type"
   399  )
   400  
   401  const (
   402  	// AuditIntegrationEnableEmoticons gets sent if the integration emoticons
   403  	// were enabled/disabled.
   404  	//
   405  	// Type: bool
   406  	AuditIntegrationEnableEmoticons AuditLogChangeKey = "enable_emoticons"
   407  	// AuditIntegrationExpireBehavior gets sent if the integration expiring
   408  	// subscriber behavior changed.
   409  	//
   410  	// Type: ExpireBehavior
   411  	AuditIntegrationExpireBehavior AuditLogChangeKey = "expire_behavior"
   412  	// AuditIntegrationExpireGracePeriod gets sent if the integration expire
   413  	// grace period changed.
   414  	//
   415  	// Type: int
   416  	AuditIntegrationExpireGracePeriod AuditLogChangeKey = "expire_grace_period"
   417  )