Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Client): guildAuditLogEntryCreate event #9058

Merged
merged 6 commits into from
Feb 17, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ body:
- No Intents
- Guilds
- GuildMembers
- GuildBans
- GuildModeration
- GuildEmojisAndStickers
- GuildIntegrations
- GuildWebhooks
Expand Down
1 change: 1 addition & 0 deletions packages/discord.js/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
Original file line number Diff line number Diff line change
@@ -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;
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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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')],
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/managers/GuildBanManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/GuildAuditLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
26 changes: 19 additions & 7 deletions packages/discord.js/src/structures/GuildAuditLogsEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -120,14 +120,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(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
almeidx marked this conversation as resolved.
Show resolved Hide resolved
: null;

/**
Expand Down Expand Up @@ -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}
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) ??
Expand Down
2 changes: 2 additions & 0 deletions packages/discord.js/src/util/Events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,6 +92,7 @@ module.exports = {
ClientReady: 'ready',
Debug: 'debug',
Error: 'error',
GuildAuditLogEntryCreate: 'guildAuditLogEntryCreate',
GuildBanAdd: 'guildBanAdd',
GuildBanRemove: 'guildBanRemove',
GuildCreate: 'guildCreate',
Expand Down
8 changes: 6 additions & 2 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1387,19 +1387,21 @@ export class GuildAuditLogsEntry<
: GuildAuditLogsTargetType,
TResolvedType = TAction extends null ? AuditLogEvent : TAction,
> {
private constructor(logs: GuildAuditLogs, guild: Guild, data: RawGuildAuditLogEntryData);
private constructor(guild: Guild, data: RawGuildAuditLogEntryData, logs?: GuildAuditLogs);
public static Targets: GuildAuditLogsTargets;
public action: TResolvedType;
public actionType: TActionType;
public changes: AuditLogChange[];
public get createdAt(): Date;
public get createdTimestamp(): number;
public executorId: Snowflake | null;
public executor: User | null;
public extra: TResolvedType extends keyof GuildAuditLogsEntryExtraField
? GuildAuditLogsEntryExtraField[TResolvedType]
: 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 @@ -4691,6 +4693,7 @@ export interface ClientEvents {
emojiDelete: [emoji: GuildEmoji];
emojiUpdate: [oldEmoji: GuildEmoji, newEmoji: GuildEmoji];
error: [error: Error];
guildAuditLogEntryCreate: [auditLogEntry: GuildAuditLogsEntry, guild: Guild];
guildBanAdd: [ban: GuildBan];
guildBanRemove: [ban: GuildBan];
guildCreate: [guild: Guild];
Expand Down Expand Up @@ -4897,6 +4900,7 @@ export enum Events {
AutoModerationRuleDelete = 'autoModerationRuleDelete',
AutoModerationRuleUpdate = 'autoModerationRuleUpdate',
ClientReady = 'ready',
GuildAuditLogEntryCreate = 'guildAuditLogEntryCreate',
GuildCreate = 'guildCreate',
GuildDelete = 'guildDelete',
GuildUpdate = 'guildUpdate',
Expand Down Expand Up @@ -5307,7 +5311,7 @@ export interface GuildAuditLogsEntryTargetField<TActionType extends GuildAuditLo
StageInstance: StageInstance;
Sticker: Sticker;
GuildScheduledEvent: GuildScheduledEvent;
ApplicationCommand: ApplicationCommand;
ApplicationCommand: ApplicationCommand | { id: Snowflake };
AutoModerationRule: AutoModerationRule;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2138,3 +2138,8 @@ if (anySelectMenu.isStringSelectMenu()) {
} else if (anySelectMenu.isMentionableSelectMenu()) {
expectType<MentionableSelectMenuInteraction>(anySelectMenu);
}

client.on('guildAuditLogEntryCreate', (auditLogEntry, guild) => {
expectType<GuildAuditLogsEntry>(auditLogEntry);
expectType<Guild>(guild);
});