Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/structures/src/auditlog/AuditLog.ts
Original file line number Diff line number Diff line change
@@ -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<Omitted extends keyof APIAuditLog | '' = ''> extends Structure<APIAuditLog, Omitted> {
/**
* @param data - The raw data received from the API for the audit log.
*/
public constructor(data: Partialize<APIAuditLog, Omitted>) {
super(data);
}
}
75 changes: 75 additions & 0 deletions packages/structures/src/auditlog/AuditLogEntry.ts
Original file line number Diff line number Diff line change
@@ -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[]`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AuditLogChange should be a substructure too and is missing completely

* @remarks Has substructure `AuditLogOptions`, which needs to be instantiated and stored by an extending class using it.
*/
export class AuditLogEntry<Omitted extends keyof APIAuditLogEntry | '' = ''> extends Structure<
APIAuditLogEntry,
Omitted
> {
/**
* @param data - The raw data received from the API for the audit log entry.
*/
public constructor(data: Partialize<APIAuditLogEntry, Omitted>) {
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;
}
}
100 changes: 100 additions & 0 deletions packages/structures/src/auditlog/AuditLogOptions.ts
Original file line number Diff line number Diff line change
@@ -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<Omitted extends keyof APIAuditLogOptions | '' = ''> extends Structure<
APIAuditLogOptions,
Omitted
> {
/**
* @param data - The raw data received from the API for the audit log options.
*/
public constructor(data: Partialize<APIAuditLogOptions, Omitted>) {
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;
}
}
3 changes: 3 additions & 0 deletions packages/structures/src/auditlog/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './AuditLog.js';
export * from './AuditLogEntry.js';
export * from './AuditLogOptions.js';
1 change: 1 addition & 0 deletions packages/structures/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './auditlog/index.js';
export * from './automoderation/index.js';
export * from './bitfields/index.js';
export * from './channels/index.js';
Expand Down