diff --git a/packages/structures/src/auditlog/AuditLog.ts b/packages/structures/src/auditlog/AuditLog.ts new file mode 100644 index 000000000000..d764b41f581e --- /dev/null +++ b/packages/structures/src/auditlog/AuditLog.ts @@ -0,0 +1,19 @@ +import type { APIAuditLog } from 'discord-api-types/v10'; +import { Structure } from '../Structure'; +import type { Partialize } from '../utils/types'; + +/** + * Represents an audit log on Discord. + * + * @typeParam Omitted - Specify the properties that will not be stored in the raw data field as a union, implement via `DataTemplate` + * @remarks Has substructures `ApplicationCommand`, `AuditLogEntry`, `AutoModerationRule`, `GuildScheduledEvent`, `GuildIntegration`, `ThreadChannel`, `User`, and `Webhook`, + * which need to be instantiated and stored by an extending class using it + */ +export class AuditLog extends Structure { + /** + * @param data - The raw data received from the API for the audit log. + */ + public constructor(data: Partialize) { + super(data); + } +} diff --git a/packages/structures/src/auditlog/AuditLogEntry.ts b/packages/structures/src/auditlog/AuditLogEntry.ts new file mode 100644 index 000000000000..ba3cd6ba84f6 --- /dev/null +++ b/packages/structures/src/auditlog/AuditLogEntry.ts @@ -0,0 +1,75 @@ +import { DiscordSnowflake } from '@sapphire/snowflake'; +import type { APIAuditLogEntry } from 'discord-api-types/v10'; +import { Structure } from '../Structure'; +import { kData } from '../utils/symbols'; +import { isIdSet } from '../utils/type-guards'; +import type { Partialize } from '../utils/types'; + +/** + * Represents an audit log entry on Discord. + * + * @typeParam Omitted - Specify the properties that will not be stored in the raw data field as a union, implement via `DataTemplate` + * @remarks Intentionally does not export `changes` so that extending classes can resolve array to `AuditLogChange[]` + * @remarks Has substructure `AuditLogOptions`, which needs to be instantiated and stored by an extending class using it. + */ +export class AuditLogEntry extends Structure< + APIAuditLogEntry, + Omitted +> { + /** + * @param data - The raw data received from the API for the audit log entry. + */ + public constructor(data: Partialize) { + super(data); + } + + /** + * The id of the affected entity (webhook, user, role, etc.) + */ + public get targetId() { + return this[kData].target_id; + } + + /** + * The user or app that made the changes. + */ + public get userId() { + return this[kData].user_id; + } + + /** + * The id of the entry. + */ + public get id() { + return this[kData].id; + } + + /** + * The type of action that occurred. + */ + public get actionType() { + return this[kData].action_type; + } + + /** + * The reason for the change (0-512 characters) + */ + public get reason() { + return this[kData].reason; + } + + /** + * The timestamp the entry was created at. + */ + public get createdTimestamp() { + return isIdSet(this.id) ? DiscordSnowflake.timestampFrom(this.id) : null; + } + + /** + * The date the entry was created at. + */ + public get createdDate() { + const createdTimestamp = this.createdTimestamp; + return createdTimestamp ? new Date(createdTimestamp) : null; + } +} diff --git a/packages/structures/src/auditlog/AuditLogOptions.ts b/packages/structures/src/auditlog/AuditLogOptions.ts new file mode 100644 index 000000000000..c96cb4615ad0 --- /dev/null +++ b/packages/structures/src/auditlog/AuditLogOptions.ts @@ -0,0 +1,100 @@ +import type { APIAuditLogOptions } from 'discord-api-types/v10'; +import { Structure } from '../Structure'; +import { kData } from '../utils/symbols'; +import type { Partialize } from '../utils/types'; + +/** + * Represents the audit log options on Discord. + * + * @typeParam Omitted - Specify the properties that will not be stored in the raw data field as a union, implement via `DataTemplate` + */ +export class AuditLogOptions extends Structure< + APIAuditLogOptions, + Omitted +> { + /** + * @param data - The raw data received from the API for the audit log options. + */ + public constructor(data: Partialize) { + super(data); + } + + /** + * Name of the auto moderation rule that was triggered. + */ + public get autoModerationRuleName() { + return this[kData].auto_moderation_rule_name; + } + + /** + * Trigger type of the auto moderation rule that was triggered. + */ + public get autoModerationRuleTriggerType() { + return this[kData].auto_moderation_rule_trigger_type; + } + + /** + * The id of the channel in which the entities were targeted. + */ + public get channelId() { + return this[kData].channel_id; + } + + /** + * The number of entities that were targeted. + */ + public get count() { + return this[kData].count; + } + + /** + * Number of days after which inactive members were kicked. + */ + public get deleteMemberDays() { + return this[kData].delete_member_days; + } + + /** + * The id of the overwritten entity. + */ + public get id() { + return this[kData].id; + } + + /** + * The number of members removed by the prune. + */ + public get membersRemoved() { + return this[kData].members_removed; + } + + /** + * The id of the message that was targeted. + */ + public get messageId() { + return this[kData].message_id; + } + + /** + * Name of the role. + * + * @remarks Only present if the {@link AuditLogOptions.type} is set to `0` + */ + public get roleName() { + return this[kData].role_name; + } + + /** + * The type of overwritten entity - `"0"` for `role` or `"1"` for `member` + */ + public get type() { + return this[kData].type; + } + + /** + * The type of integration which performed the action. + */ + public get integrationType() { + return this[kData].integration_type; + } +} diff --git a/packages/structures/src/auditlog/index.ts b/packages/structures/src/auditlog/index.ts new file mode 100644 index 000000000000..7c963ea26d9b --- /dev/null +++ b/packages/structures/src/auditlog/index.ts @@ -0,0 +1,3 @@ +export * from './AuditLog.js'; +export * from './AuditLogEntry.js'; +export * from './AuditLogOptions.js'; diff --git a/packages/structures/src/index.ts b/packages/structures/src/index.ts index 7ace22f7db69..ecb46351e350 100644 --- a/packages/structures/src/index.ts +++ b/packages/structures/src/index.ts @@ -1,3 +1,4 @@ +export * from './auditlog/index.js'; export * from './automoderation/index.js'; export * from './bitfields/index.js'; export * from './channels/index.js';