feat: v13 guildAuditLogEntryCreate event

#9092 djs
This commit is contained in:
Elysia 2023-02-12 15:52:03 +07:00
parent f28ea5c031
commit 7329a53b34
7 changed files with 64 additions and 8 deletions

View File

@ -11,6 +11,7 @@ class ActionsManager {
this.register(require('./ChannelCreate')); this.register(require('./ChannelCreate'));
this.register(require('./ChannelDelete')); this.register(require('./ChannelDelete'));
this.register(require('./ChannelUpdate')); this.register(require('./ChannelUpdate'));
this.register(require('./GuildAuditLogEntryCreate'));
this.register(require('./GuildBanAdd')); this.register(require('./GuildBanAdd'));
this.register(require('./GuildBanRemove')); this.register(require('./GuildBanRemove'));
this.register(require('./GuildChannelsPositionUpdate')); this.register(require('./GuildChannelsPositionUpdate'));

View File

@ -0,0 +1,29 @@
'use strict';
const Action = require('./Action');
const GuildAuditLogsEntry = require('../../structures/GuildAuditLogs').Entry;
const Events = require('../../util/Events');
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.GuildAuditLogEntryCreate, auditLogEntry, guild);
}
return { auditLogEntry };
}
}
module.exports = GuildAuditLogEntryCreateAction;

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = (client, packet) => {
client.actions.GuildAuditLogEntryCreate.handle(packet.d);
};

View File

@ -14,6 +14,7 @@ const handlers = Object.fromEntries([
['AUTO_MODERATION_RULE_CREATE', require('./AUTO_MODERATION_RULE_CREATE')], ['AUTO_MODERATION_RULE_CREATE', require('./AUTO_MODERATION_RULE_CREATE')],
['AUTO_MODERATION_RULE_DELETE', require('./AUTO_MODERATION_RULE_DELETE')], ['AUTO_MODERATION_RULE_DELETE', require('./AUTO_MODERATION_RULE_DELETE')],
['AUTO_MODERATION_RULE_UPDATE', require('./AUTO_MODERATION_RULE_UPDATE')], ['AUTO_MODERATION_RULE_UPDATE', require('./AUTO_MODERATION_RULE_UPDATE')],
['GUILD_AUDIT_LOG_ENTRY_CREATE', require('./GUILD_AUDIT_LOG_ENTRY_CREATE')],
['CALL_CREATE', require('./CALL_CREATE')], ['CALL_CREATE', require('./CALL_CREATE')],
['CALL_UPDATE', require('./CALL_UPDATE')], ['CALL_UPDATE', require('./CALL_UPDATE')],
['CALL_DELETE', require('./CALL_DELETE')], ['CALL_DELETE', require('./CALL_DELETE')],

View File

@ -225,7 +225,7 @@ class GuildAuditLogs {
*/ */
this.entries = new Collection(); this.entries = new Collection();
for (const item of data.audit_log_entries) { 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); this.entries.set(entry.id, entry);
} }
} }
@ -384,7 +384,7 @@ class GuildAuditLogs {
* Audit logs entry. * Audit logs entry.
*/ */
class GuildAuditLogsEntry { class GuildAuditLogsEntry {
constructor(logs, guild, data) { constructor(guild, data, logs) {
const targetType = GuildAuditLogs.targetType(data.action_type); const targetType = GuildAuditLogs.targetType(data.action_type);
/** /**
* The target type of this entry * The target type of this entry
@ -410,6 +410,12 @@ class GuildAuditLogsEntry {
*/ */
this.reason = data.reason ?? null; 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 * The user that executed this entry
* @type {?User} * @type {?User}
@ -417,7 +423,7 @@ class GuildAuditLogsEntry {
this.executor = data.user_id this.executor = data.user_id
? guild.client.options.partials.includes(PartialTypes.USER) ? guild.client.options.partials.includes(PartialTypes.USER)
? guild.client.users._add({ id: data.user_id }) ? 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; : null;
/** /**
@ -521,6 +527,12 @@ class GuildAuditLogsEntry {
break; break;
} }
/**
* The id of the target of this entry
* @type {?Snowflake}
*/
this.targetId = data.target_id;
/** /**
* The target of this entry * The target of this entry
* @type {?AuditLogEntryTarget} * @type {?AuditLogEntryTarget}
@ -536,12 +548,12 @@ class GuildAuditLogsEntry {
} else if (targetType === Targets.USER && data.target_id) { } else if (targetType === Targets.USER && data.target_id) {
this.target = guild.client.options.partials.includes(PartialTypes.USER) this.target = guild.client.options.partials.includes(PartialTypes.USER)
? guild.client.users._add({ id: data.target_id }) ? 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) { } else if (targetType === Targets.GUILD) {
this.target = guild.client.guilds.cache.get(data.target_id); this.target = guild.client.guilds.cache.get(data.target_id);
} else if (targetType === Targets.WEBHOOK) { } else if (targetType === Targets.WEBHOOK) {
this.target = this.target =
logs.webhooks.get(data.target_id) ?? logs?.webhooks.get(data.target_id) ??
new Webhook( new Webhook(
guild.client, guild.client,
this.changes.reduce( this.changes.reduce(
@ -576,10 +588,10 @@ class GuildAuditLogsEntry {
this.target = this.target =
data.action_type === Actions.MESSAGE_BULK_DELETE data.action_type === Actions.MESSAGE_BULK_DELETE
? guild.channels.cache.get(data.target_id) ?? { id: data.target_id } ? 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) { } else if (targetType === Targets.INTEGRATION) {
this.target = this.target =
logs.integrations.get(data.target_id) ?? logs?.integrations.get(data.target_id) ??
new Integration( new Integration(
guild.client, guild.client,
this.changes.reduce( this.changes.reduce(

View File

@ -401,6 +401,7 @@ exports.Opcodes = {
* * GUILD_SCHEDULED_EVENT_DELETE: guildScheduledEventDelete * * GUILD_SCHEDULED_EVENT_DELETE: guildScheduledEventDelete
* * GUILD_SCHEDULED_EVENT_USER_ADD: guildScheduledEventUserAdd * * GUILD_SCHEDULED_EVENT_USER_ADD: guildScheduledEventUserAdd
* * GUILD_SCHEDULED_EVENT_USER_REMOVE: guildScheduledEventUserRemove * * GUILD_SCHEDULED_EVENT_USER_REMOVE: guildScheduledEventUserRemove
* * GUILD_AUDIT_LOG_ENTRY_CREATE: guildAuditLogEntryCreate
* @typedef {Object<string, string>} Events * @typedef {Object<string, string>} Events
*/ */
exports.Events = { exports.Events = {
@ -497,6 +498,7 @@ exports.Events = {
GUILD_SCHEDULED_EVENT_DELETE: 'guildScheduledEventDelete', GUILD_SCHEDULED_EVENT_DELETE: 'guildScheduledEventDelete',
GUILD_SCHEDULED_EVENT_USER_ADD: 'guildScheduledEventUserAdd', GUILD_SCHEDULED_EVENT_USER_ADD: 'guildScheduledEventUserAdd',
GUILD_SCHEDULED_EVENT_USER_REMOVE: 'guildScheduledEventUserRemove', GUILD_SCHEDULED_EVENT_USER_REMOVE: 'guildScheduledEventUserRemove',
GUILD_AUDIT_LOG_ENTRY_CREATE: 'guildAuditLogEntryCreate',
RELATIONSHIP_ADD: 'relationshipAdd', RELATIONSHIP_ADD: 'relationshipAdd',
RELATIONSHIP_REMOVE: 'relationshipRemove', RELATIONSHIP_REMOVE: 'relationshipRemove',
RELATIONSHIP_UPDATE: 'relationshipUpdate', RELATIONSHIP_UPDATE: 'relationshipUpdate',
@ -594,6 +596,7 @@ exports.PartialTypes = keyMirror(['USER', 'CHANNEL', 'GUILD_MEMBER', 'MESSAGE',
* * GUILD_SCHEDULED_EVENT_DELETE * * GUILD_SCHEDULED_EVENT_DELETE
* * GUILD_SCHEDULED_EVENT_USER_ADD * * GUILD_SCHEDULED_EVENT_USER_ADD
* * GUILD_SCHEDULED_EVENT_USER_REMOVE * * GUILD_SCHEDULED_EVENT_USER_REMOVE
* * GUILD_AUDIT_LOG_ENTRY_CREATE
* @typedef {string} WSEventType * @typedef {string} WSEventType
* @see {@link https://discord.com/developers/docs/topics/gateway#commands-and-events-gateway-events} * @see {@link https://discord.com/developers/docs/topics/gateway#commands-and-events-gateway-events}
*/ */
@ -654,6 +657,7 @@ exports.WSEvents = keyMirror([
'GUILD_SCHEDULED_EVENT_DELETE', 'GUILD_SCHEDULED_EVENT_DELETE',
'GUILD_SCHEDULED_EVENT_USER_ADD', 'GUILD_SCHEDULED_EVENT_USER_ADD',
'GUILD_SCHEDULED_EVENT_USER_REMOVE', 'GUILD_SCHEDULED_EVENT_USER_REMOVE',
'GUILD_AUDIT_LOG_ENTRY_CREATE',
]); ]);
/** /**

6
typings/index.d.ts vendored
View File

@ -1475,16 +1475,18 @@ export class GuildAuditLogsEntry<
? GuildAuditLogsTypes[TAction][0] ? GuildAuditLogsTypes[TAction][0]
: 'UNKNOWN', : 'UNKNOWN',
> { > {
private constructor(logs: GuildAuditLogs, guild: Guild, data: RawGuildAuditLogEntryData); private constructor(guild: Guild, data: RawGuildAuditLogEntryData, logs?: GuildAuditLogs);
public action: TAction; public action: TAction;
public actionType: TActionType; public actionType: TActionType;
public changes: AuditLogChange[]; public changes: AuditLogChange[];
public readonly createdAt: Date; public readonly createdAt: Date;
public readonly createdTimestamp: number; public readonly createdTimestamp: number;
public executorId: Snowflake | null;
public executor: User | null; public executor: User | null;
public extra: TAction extends keyof GuildAuditLogsEntryExtraField ? GuildAuditLogsEntryExtraField[TAction] : null; public extra: TAction extends keyof GuildAuditLogsEntryExtraField ? GuildAuditLogsEntryExtraField[TAction] : null;
public id: Snowflake; public id: Snowflake;
public reason: string | null; public reason: string | null;
public targetId: Snowflake | null;
public target: TTargetType extends keyof GuildAuditLogsEntryTargetField<TActionType> public target: TTargetType extends keyof GuildAuditLogsEntryTargetField<TActionType>
? GuildAuditLogsEntryTargetField<TActionType>[TTargetType] ? GuildAuditLogsEntryTargetField<TActionType>[TTargetType]
: Role | GuildEmoji | { id: Snowflake } | null; : Role | GuildEmoji | { id: Snowflake } | null;
@ -4671,6 +4673,7 @@ export interface ClientEvents extends BaseClientEvents {
guildScheduledEventDelete: [guildScheduledEvent: GuildScheduledEvent]; guildScheduledEventDelete: [guildScheduledEvent: GuildScheduledEvent];
guildScheduledEventUserAdd: [guildScheduledEvent: GuildScheduledEvent, user: User]; guildScheduledEventUserAdd: [guildScheduledEvent: GuildScheduledEvent, user: User];
guildScheduledEventUserRemove: [guildScheduledEvent: GuildScheduledEvent, user: User]; guildScheduledEventUserRemove: [guildScheduledEvent: GuildScheduledEvent, user: User];
guildAuditLogEntryCreate: [auditLogEntry: GuildAuditLogsEntry, guild: Guild];
relationshipAdd: [id: Snowflake, type: RelationshipTypes]; relationshipAdd: [id: Snowflake, type: RelationshipTypes];
relationshipRemove: [id: Snowflake]; relationshipRemove: [id: Snowflake];
relationshipUpdate: [id: Snowflake, type: RelationshipTypes, data: object]; relationshipUpdate: [id: Snowflake, type: RelationshipTypes, data: object];
@ -4770,6 +4773,7 @@ export interface ConstantsEvents {
GUILD_SCHEDULED_EVENT_DELETE: 'guildScheduledEventDelete'; GUILD_SCHEDULED_EVENT_DELETE: 'guildScheduledEventDelete';
GUILD_SCHEDULED_EVENT_USER_ADD: 'guildScheduledEventUserAdd'; GUILD_SCHEDULED_EVENT_USER_ADD: 'guildScheduledEventUserAdd';
GUILD_SCHEDULED_EVENT_USER_REMOVE: 'guildScheduledEventUserRemove'; GUILD_SCHEDULED_EVENT_USER_REMOVE: 'guildScheduledEventUserRemove';
GUILD_AUDIT_LOG_ENTRY_CREATE: 'guildAuditLogEntryCreate';
RELATIONSHIP_ADD: 'relationshipAdd'; RELATIONSHIP_ADD: 'relationshipAdd';
RELATIONSHIP_REMOVE: 'relationshipRemove'; RELATIONSHIP_REMOVE: 'relationshipRemove';
RELATIONSHIP_UPDATE: 'relationshipUpdate'; RELATIONSHIP_UPDATE: 'relationshipUpdate';