mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 03:53:29 +01:00
feat(Client): guildAuditLogEntryCreate event (#9058)
* feat(Client): `guildAuditLogEntryCreate` event * feat: add `targetId` and `executorId` * types: `ApplicationCommand` fix --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -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'));
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
const Action = require('./Action');
|
||||
const GuildAuditLogsEntry = require('../../structures/GuildAuditLogsEntry');
|
||||
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;
|
||||
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = (client, packet) => {
|
||||
client.actions.GuildAuditLogEntryCreate.handle(packet.d);
|
||||
};
|
||||
@@ -10,6 +10,7 @@ const handlers = Object.fromEntries([
|
||||
['CHANNEL_DELETE', require('./CHANNEL_DELETE')],
|
||||
['CHANNEL_PINS_UPDATE', require('./CHANNEL_PINS_UPDATE')],
|
||||
['CHANNEL_UPDATE', require('./CHANNEL_UPDATE')],
|
||||
['GUILD_AUDIT_LOG_ENTRY_CREATE', require('./GUILD_AUDIT_LOG_ENTRY_CREATE')],
|
||||
['GUILD_BAN_ADD', require('./GUILD_BAN_ADD')],
|
||||
['GUILD_BAN_REMOVE', require('./GUILD_BAN_REMOVE')],
|
||||
['GUILD_CREATE', require('./GUILD_CREATE')],
|
||||
|
||||
@@ -12,7 +12,7 @@ const { GuildMember } = require('../structures/GuildMember');
|
||||
let deprecationEmittedForDeleteMessageDays = false;
|
||||
|
||||
/**
|
||||
* Manages API methods for GuildBans and stores their cache.
|
||||
* Manages API methods for guild bans and stores their cache.
|
||||
* @extends {CachedManager}
|
||||
*/
|
||||
class GuildBanManager extends CachedManager {
|
||||
|
||||
@@ -78,7 +78,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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ class GuildAuditLogsEntry {
|
||||
*/
|
||||
static Targets = Targets;
|
||||
|
||||
constructor(logs, guild, data) {
|
||||
constructor(guild, data, logs) {
|
||||
/**
|
||||
* The target type of this entry
|
||||
* @type {AuditLogTargetType}
|
||||
@@ -120,6 +120,12 @@ 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}
|
||||
@@ -127,7 +133,7 @@ class GuildAuditLogsEntry {
|
||||
this.executor = data.user_id
|
||||
? guild.client.options.partials.includes(Partials.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;
|
||||
|
||||
/**
|
||||
@@ -239,6 +245,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}
|
||||
@@ -254,12 +266,12 @@ class GuildAuditLogsEntry {
|
||||
} else if (targetType === Targets.User && data.target_id) {
|
||||
this.target = guild.client.options.partials.includes(Partials.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(
|
||||
@@ -294,10 +306,10 @@ class GuildAuditLogsEntry {
|
||||
this.target =
|
||||
data.action_type === AuditLogEvent.MessageBulkDelete
|
||||
? 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(
|
||||
@@ -363,7 +375,7 @@ class GuildAuditLogsEntry {
|
||||
),
|
||||
);
|
||||
} else if (targetType === Targets.ApplicationCommand) {
|
||||
this.target = logs.applicationCommands.get(data.target_id) ?? { id: data.target_id };
|
||||
this.target = logs?.applicationCommands.get(data.target_id) ?? { id: data.target_id };
|
||||
} else if (targetType === Targets.AutoModeration) {
|
||||
this.target =
|
||||
guild.autoModerationRules.cache.get(data.target_id) ??
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
* @property {string} ClientReady ready
|
||||
* @property {string} Debug debug
|
||||
* @property {string} Error error
|
||||
* @property {string} GuildAuditLogEntryCreate guildAuditLogEntryCreate
|
||||
* @property {string} GuildBanAdd guildBanAdd
|
||||
* @property {string} GuildBanRemove guildBanRemove
|
||||
* @property {string} GuildCreate guildCreate
|
||||
@@ -91,6 +92,7 @@ module.exports = {
|
||||
ClientReady: 'ready',
|
||||
Debug: 'debug',
|
||||
Error: 'error',
|
||||
GuildAuditLogEntryCreate: 'guildAuditLogEntryCreate',
|
||||
GuildBanAdd: 'guildBanAdd',
|
||||
GuildBanRemove: 'guildBanRemove',
|
||||
GuildCreate: 'guildCreate',
|
||||
|
||||
Reference in New Issue
Block a user