Skip to content

Commit

Permalink
feat: v13 guildAuditLogEntryCreate event (#9092)
Browse files Browse the repository at this point in the history
* feat: guildAuditLogEntryCreate event

* Update src/client/actions/GuildAuditLogEntryCreate.js

Co-authored-by: Elysia <71698422+aiko-chan-ai@users.noreply.github.com>

* Update src/client/actions/GuildAuditLogEntryCreate.js

Co-authored-by: space <spaceeec@yahoo.com>

---------

Co-authored-by: Elysia <71698422+aiko-chan-ai@users.noreply.github.com>
Co-authored-by: space <spaceeec@yahoo.com>
  • Loading branch information
3 people committed Feb 17, 2023
1 parent 7737bbe commit 84d34dc
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/client/actions/ActionsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ActionsManager {
this.register(require('./ChannelCreate'));
this.register(require('./ChannelDelete'));
this.register(require('./ChannelUpdate'));
this.register(require('./GuildAuditLogEntryCreate'));
this.register(require('./GuildBanAdd'));
this.register(require('./GuildBanRemove'));
this.register(require('./GuildChannelsPositionUpdate'));
Expand Down
29 changes: 29 additions & 0 deletions src/client/actions/GuildAuditLogEntryCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const Action = require('./Action');
const GuildAuditLogsEntry = require('../../structures/GuildAuditLogs').Entry;
const { Events } = require('../../util/Constants');

class GuildAuditLogEntryCreateAction extends Action {
handle(data) {
const client = this.client;
const guild = client.guilds.cache.get(data.guild_id);
let auditLogEntry;

if (guild) {
auditLogEntry = new GuildAuditLogsEntry(guild, data);

/**
* Emitted whenever a guild audit log entry is created.
* @event Client#guildAuditLogEntryCreate
* @param {GuildAuditLogsEntry} auditLogEntry The entry that was created
* @param {Guild} guild The guild where the entry was created
*/
client.emit(Events.GUILD_AUDIT_LOG_ENTRY_CREATE, auditLogEntry, guild);
}

return { auditLogEntry };
}
}

module.exports = GuildAuditLogEntryCreateAction;
5 changes: 5 additions & 0 deletions src/client/websocket/handlers/GUILD_AUDIT_LOG_ENTRY_CREATE.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = (client, packet) => {
client.actions.GuildAuditLogEntryCreate.handle(packet.d);
};
1 change: 1 addition & 0 deletions src/client/websocket/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const handlers = Object.fromEntries([
['AUTO_MODERATION_RULE_CREATE', require('./AUTO_MODERATION_RULE_CREATE')],
['AUTO_MODERATION_RULE_DELETE', require('./AUTO_MODERATION_RULE_DELETE')],
['AUTO_MODERATION_RULE_UPDATE', require('./AUTO_MODERATION_RULE_UPDATE')],
['GUILD_AUDIT_LOG_ENTRY_CREATE', require('./GUILD_AUDIT_LOG_ENTRY_CREATE')],
['GUILD_CREATE', require('./GUILD_CREATE')],
['GUILD_DELETE', require('./GUILD_DELETE')],
['GUILD_UPDATE', require('./GUILD_UPDATE')],
Expand Down
26 changes: 19 additions & 7 deletions src/structures/GuildAuditLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class GuildAuditLogs {
*/
this.entries = new Collection();
for (const item of data.audit_log_entries) {
const entry = new GuildAuditLogsEntry(this, guild, item);
const entry = new GuildAuditLogsEntry(guild, item, this);
this.entries.set(entry.id, entry);
}
}
Expand Down Expand Up @@ -403,7 +403,7 @@ class GuildAuditLogs {
* Audit logs entry.
*/
class GuildAuditLogsEntry {
constructor(logs, guild, data) {
constructor(guild, data, logs) {
const targetType = GuildAuditLogs.targetType(data.action_type);
/**
* The target type of this entry
Expand All @@ -429,14 +429,20 @@ class GuildAuditLogsEntry {
*/
this.reason = data.reason ?? null;

/**
* The id of the user that executed this entry
* @type {?Snowflake}
*/
this.executorId = data.user_id;

/**
* The user that executed this entry
* @type {?User}
*/
this.executor = data.user_id
? guild.client.options.partials.includes(PartialTypes.USER)
? guild.client.users._add({ id: data.user_id })
: guild.client.users.cache.get(data.user_id)
: guild.client.users.cache.get(data.user_id) ?? null
: null;

/**
Expand Down Expand Up @@ -542,6 +548,12 @@ class GuildAuditLogsEntry {
break;
}

/**
* The id of the target of this entry
* @type {?Snowflake}
*/
this.targetId = data.target_id;

/**
* The target of this entry
* @type {?AuditLogEntryTarget}
Expand All @@ -557,12 +569,12 @@ class GuildAuditLogsEntry {
} else if (targetType === Targets.USER && data.target_id) {
this.target = guild.client.options.partials.includes(PartialTypes.USER)
? guild.client.users._add({ id: data.target_id })
: guild.client.users.cache.get(data.target_id);
: guild.client.users.cache.get(data.target_id) ?? null;
} else if (targetType === Targets.GUILD) {
this.target = guild.client.guilds.cache.get(data.target_id);
} else if (targetType === Targets.WEBHOOK) {
this.target =
logs.webhooks.get(data.target_id) ??
logs?.webhooks.get(data.target_id) ??
new Webhook(
guild.client,
this.changes.reduce(
Expand Down Expand Up @@ -597,10 +609,10 @@ class GuildAuditLogsEntry {
this.target =
data.action_type === Actions.MESSAGE_BULK_DELETE
? guild.channels.cache.get(data.target_id) ?? { id: data.target_id }
: guild.client.users.cache.get(data.target_id);
: guild.client.users.cache.get(data.target_id) ?? null;
} else if (targetType === Targets.INTEGRATION) {
this.target =
logs.integrations.get(data.target_id) ??
logs?.integrations.get(data.target_id) ??
new Integration(
guild.client,
this.changes.reduce(
Expand Down
4 changes: 4 additions & 0 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ exports.Opcodes = {
* * GUILD_SCHEDULED_EVENT_DELETE: guildScheduledEventDelete
* * GUILD_SCHEDULED_EVENT_USER_ADD: guildScheduledEventUserAdd
* * GUILD_SCHEDULED_EVENT_USER_REMOVE: guildScheduledEventUserRemove
* * GUILD_AUDIT_LOG_ENTRY_CREATE: guildAuditLogEntryCreate
* @typedef {Object<string, string>} Events
*/
exports.Events = {
Expand Down Expand Up @@ -329,6 +330,7 @@ exports.Events = {
GUILD_SCHEDULED_EVENT_DELETE: 'guildScheduledEventDelete',
GUILD_SCHEDULED_EVENT_USER_ADD: 'guildScheduledEventUserAdd',
GUILD_SCHEDULED_EVENT_USER_REMOVE: 'guildScheduledEventUserRemove',
GUILD_AUDIT_LOG_ENTRY_CREATE: 'guildAuditLogEntryCreate',
};

/**
Expand Down Expand Up @@ -426,6 +428,7 @@ exports.PartialTypes = keyMirror(['USER', 'CHANNEL', 'GUILD_MEMBER', 'MESSAGE',
* * GUILD_SCHEDULED_EVENT_DELETE
* * GUILD_SCHEDULED_EVENT_USER_ADD
* * GUILD_SCHEDULED_EVENT_USER_REMOVE
* * GUILD_AUDIT_LOG_ENTRY_CREATE
* @typedef {string} WSEventType
* @see {@link https://discord.com/developers/docs/topics/gateway#commands-and-events-gateway-events}
*/
Expand Down Expand Up @@ -490,6 +493,7 @@ exports.WSEvents = keyMirror([
'GUILD_SCHEDULED_EVENT_DELETE',
'GUILD_SCHEDULED_EVENT_USER_ADD',
'GUILD_SCHEDULED_EVENT_USER_REMOVE',
'GUILD_AUDIT_LOG_ENTRY_CREATE',
]);

/**
Expand Down
6 changes: 5 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,16 +1105,18 @@ export class GuildAuditLogsEntry<
? GuildAuditLogsTypes[TAction][0]
: 'UNKNOWN',
> {
private constructor(logs: GuildAuditLogs, guild: Guild, data: RawGuildAuditLogEntryData);
private constructor(guild: Guild, data: RawGuildAuditLogEntryData, logs?: GuildAuditLogs);
public action: TAction;
public actionType: TActionType;
public changes: AuditLogChange[];
public readonly createdAt: Date;
public readonly createdTimestamp: number;
public executorId: Snowflake | null;
public executor: User | null;
public extra: TAction extends keyof GuildAuditLogsEntryExtraField ? GuildAuditLogsEntryExtraField[TAction] : null;
public id: Snowflake;
public reason: string | null;
public targetId: Snowflake | null;
public target: TTargetType extends keyof GuildAuditLogsEntryTargetField<TActionType>
? GuildAuditLogsEntryTargetField<TActionType>[TTargetType]
: Role | GuildEmoji | { id: Snowflake } | null;
Expand Down Expand Up @@ -4581,6 +4583,7 @@ export interface ClientEvents extends BaseClientEvents {
guildScheduledEventDelete: [guildScheduledEvent: GuildScheduledEvent];
guildScheduledEventUserAdd: [guildScheduledEvent: GuildScheduledEvent, user: User];
guildScheduledEventUserRemove: [guildScheduledEvent: GuildScheduledEvent, user: User];
guildAuditLogEntryCreate: [auditLogEntry: GuildAuditLogsEntry, guild: Guild];
}

export interface ClientFetchInviteOptions {
Expand Down Expand Up @@ -4844,6 +4847,7 @@ export interface ConstantsEvents {
GUILD_SCHEDULED_EVENT_DELETE: 'guildScheduledEventDelete';
GUILD_SCHEDULED_EVENT_USER_ADD: 'guildScheduledEventUserAdd';
GUILD_SCHEDULED_EVENT_USER_REMOVE: 'guildScheduledEventUserRemove';
GUILD_AUDIT_LOG_ENTRY_CREATE: 'guildAuditLogEntryCreate';
}

export interface ConstantsOpcodes {
Expand Down

0 comments on commit 84d34dc

Please sign in to comment.