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

Audit Log Entry Create Event #36

Merged
merged 6 commits into from
Jan 18, 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
3 changes: 2 additions & 1 deletion lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export default class Client<E extends ClientEvents = ClientEvents> extends Typed
},
auth: options?.auth ?? null,
collectionLimits: {
members: options?.collectionLimits?.members === undefined ? Infinity : (typeof options.collectionLimits.members === "object" ? {
auditLogEntries: options?.collectionLimits?.auditLogEntries ?? 50,
members: options?.collectionLimits?.members === undefined ? Infinity : (typeof options.collectionLimits.members === "object" ? {
unknown: Infinity,
...options.collectionLimits.members
} : options.collectionLimits.members),
Expand Down
2 changes: 2 additions & 0 deletions lib/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,9 @@ export enum InteractionResponseTypes {
export enum Intents {
GUILDS = 1 << 0,
GUILD_MEMBERS = 1 << 1,
/** @deprecated {@see Constants~Intents.GUILD_MODERATION | GUILD_MODERATION}. This will be removed in `1.6.0`. */
GUILD_BANS = 1 << 2,
GUILD_MODERATION = 1 << 2,
GUILD_EMOJIS_AND_STICKERS = 1 << 3,
GUILD_INTEGRATIONS = 1 << 4,
GUILD_WEBHOOKS = 1 << 5,
Expand Down
7 changes: 7 additions & 0 deletions lib/gateway/Shard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import type PublicThreadChannel from "../structures/PublicThreadChannel";
import Role from "../structures/Role";
import Integration from "../structures/Integration";
import VoiceState from "../structures/VoiceState";
import AuditLogEntry from "../structures/AuditLogEntry";
import WebSocket, { type Data } from "ws";
import type Pako from "pako";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down Expand Up @@ -338,6 +339,12 @@ export default class Shard extends TypedEmitter<ShardEvents> {
break;
}

case "GUILD_AUDIT_LOG_ENTRY_CREATE": {
const guild = this.client.guilds.get(packet.d.guild_id);
this.client.emit("guildAuditLogEntryCreate", guild ?? { id: packet.d.guild_id }, guild?.auditLogEntries.update(packet.d) ?? new AuditLogEntry(packet.d, this.client));
break;
}

case "GUILD_BAN_ADD": {
this.client.emit("guildBanAdd", this.client.guilds.get(packet.d.guild_id) ?? { id: packet.d.guild_id }, this.client.users.update(packet.d.user));
break;
Expand Down
6 changes: 5 additions & 1 deletion lib/structures/Guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type GuildTemplate from "./GuildTemplate";
import type GuildPreview from "./GuildPreview";
import type Invite from "./Invite";
import type Webhook from "./Webhook";
import AuditLogEntry from "./AuditLogEntry";
import {
AllPermissions,
Permissions,
Expand Down Expand Up @@ -98,7 +99,7 @@ import type {
ScheduledEventUser
} from "../types/scheduled-events";
import type { CreateAutoModerationRuleOptions, EditAutoModerationRuleOptions, RawAutoModerationRule } from "../types/auto-moderation";
import type { AuditLog, GetAuditLogOptions } from "../types/audit-log";
import type { AuditLog, GetAuditLogOptions, RawAuditLogEntry } from "../types/audit-log";
import type { CreateTemplateOptions, EditGuildTemplateOptions } from "../types/guild-template";
import type { JoinVoiceChannelOptions, RawVoiceState, VoiceRegion } from "../types/voice";
import type { JSONGuild } from "../types/json";
Expand Down Expand Up @@ -127,6 +128,8 @@ export default class Guild extends Base {
approximateMemberCount?: number;
/** The approximate number of non-offline members in this guild (if retrieved with counts). */
approximatePresenceCount?: number;
/** The cached audit log entries. This requires both the {@link Constants~Intents.GUILD_MODERATION | GUILD_MODERATION} intent, as well as the {@link Constants~Permissions.VIEW_AUDIT_LOG | VIEW_AUDIT_LOG } permission. */
auditLogEntries: TypedCollection<string, RawAuditLogEntry, AuditLogEntry>;
/** The auto moderation rules in this guild. */
autoModerationRules: TypedCollection<string, RawAutoModerationRule, AutoModerationRule>;
/** The hash of this guild's banner. */
Expand Down Expand Up @@ -239,6 +242,7 @@ export default class Guild extends Base {
this.afkChannelID = null;
this.afkTimeout = 0;
this.applicationID = data.application_id;
this.auditLogEntries = new TypedCollection(AuditLogEntry, client, client.options.collectionLimits.auditLogEntries);
this.autoModerationRules = new TypedCollection(AutoModerationRule, client);
this.banner = null;
this.channels = new TypedCollection(GuildChannel, client) as TypedCollection<string, RawGuildChannel, AnyGuildChannelWithoutThreads>;
Expand Down
5 changes: 5 additions & 0 deletions lib/types/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export interface RESTOptions {
}

export interface CollectionLimitsOptions {
/**
* The maximum number of audit log entries to keep cached. This applies to all guilds. Entries are only cached if recieved via the `GUILD_AUDIT_LOG_ENTRY_CREATE` gateway event.
* @defaultValue 50
*/
auditLogEntries?: number;
/**
* The maximum number of members to cache. A number to apply to all guilds individually, or a dictionary of guild IDs to member limits. The key `unknown` can be used to set the limit for all guilds not specified.
*
Expand Down
3 changes: 3 additions & 0 deletions lib/types/events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import type Message from "../structures/Message";
import type PrivateChannel from "../structures/PrivateChannel";
import type StageInstance from "../structures/StageInstance";
import type ForumChannel from "../structures/ForumChannel";
import type AuditLogEntry from "../structures/AuditLogEntry";


export interface ClientEvents {
Expand Down Expand Up @@ -95,6 +96,8 @@ export interface ClientEvents {
disconnect: [];
/** @event Emitted when an error happens. If an error is emitted and no handlers are present, the error will be thrown. */
error: [info: Error | string, shard?: number];
/** @event Emitted when an audit log entry is created. Requires both the `GUILD_MODERATION` intent, as well as the `VIEW_AUDIT_LOG` permission. */
guildAuditLogEntryCreate: [guild: Guild | Uncached, auditLogEntry: AuditLogEntry];
/** @event Emitted when a guild becomes available. Requires the `GUILDS` intent. */
guildAvailable: [guild: Guild];
/** @event Emitted when a guild ban is created. Requires the `GUILD_BANS` intent. */
Expand Down
8 changes: 7 additions & 1 deletion lib/types/gateway-raw.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { RawGuildChannel, RawMessage, RawThreadChannel, RawThreadMember } f
import type { RawScheduledEvent } from "./scheduled-events";
import type { RawVoiceState } from "./voice";
import type { RawInteraction } from "./interactions";
import type { RawAuditLogEntry } from "./audit-log";
import type { GatewayOPCodes, InviteTargetTypes } from "../Constants";

export type AnyReceivePacket = AnyDispatchPacket | HeartbeatPacket | ReconnectPacket | InvalidSessionPacket | HelloPacket | HeartbeatAckPacket;
Expand Down Expand Up @@ -503,8 +504,13 @@ export interface StageInstanceUpdatePacket extends BaseDispatchPacket {
t: "STAGE_INSTANCE_UPDATE";
}

export interface GuildAuditLogEntryCreatePacket extends BaseDispatchPacket {
d: RawAuditLogEntry & { guild_id: string; };
t: "GUILD_AUDIT_LOG_ENTRY_CREATE";
}

export type AnyDispatchPacket = PresenceUpdatePacket | ReadyPacket | ResumedPacket |
GuildCreatePacket | GuildDeletePacket | GuildUpdatePacket | ApplicationCommandPermissionsUpdatePacket |
GuildCreatePacket | GuildDeletePacket | GuildUpdatePacket | ApplicationCommandPermissionsUpdatePacket | GuildAuditLogEntryCreatePacket |
AutoModerationRuleCreatePacket | AutoModerationRuleDeletePacket | AutoModerationRuleUpdatePacket | AutoModerationActionExecutionPacket |
ChannelCreatePacket | ChannelDeletePacket | ChannelUpdatePacket | ChannelPinsUpdatePacket |
ThreadCreatePacket | ThreadDeletePacket | ThreadUpdatePacket | ThreadListSyncPacket | ThreadMemberUpdatePacket | ThreadMembersUpdatePacket |
Expand Down