Skip to content

Commit

Permalink
feat(GuildAuditLogs): Support after (#9011)
Browse files Browse the repository at this point in the history
* feat(GuildAuditLogs): support `after`

* refactor: remove variables

They were only being used once each.

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
Jiralite and kodiakhq[bot] committed Jan 6, 2023
1 parent 816aed4 commit 0076589
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
23 changes: 11 additions & 12 deletions packages/discord.js/src/structures/Guild.js
Expand Up @@ -5,7 +5,6 @@ const { makeURLSearchParams } = require('@discordjs/rest');
const { ChannelType, GuildPremiumTier, Routes, GuildFeature } = require('discord-api-types/v10');
const AnonymousGuild = require('./AnonymousGuild');
const GuildAuditLogs = require('./GuildAuditLogs');
const GuildAuditLogsEntry = require('./GuildAuditLogsEntry');
const GuildPreview = require('./GuildPreview');
const GuildTemplate = require('./GuildTemplate');
const Integration = require('./Integration');
Expand Down Expand Up @@ -700,7 +699,8 @@ class Guild extends AnonymousGuild {
/**
* Options used to fetch audit logs.
* @typedef {Object} GuildAuditLogsFetchOptions
* @property {Snowflake|GuildAuditLogsEntry} [before] Only return entries before this entry
* @property {Snowflake|GuildAuditLogsEntry} [before] Consider only entries before this entry
* @property {Snowflake|GuildAuditLogsEntry} [after] Consider only entries after this entry
* @property {number} [limit] The number of entries to return
* @property {UserResolvable} [user] Only return entries for actions made by this user
* @property {?AuditLogEvent} [type] Only return entries for this action type
Expand All @@ -716,19 +716,18 @@ class Guild extends AnonymousGuild {
* .then(audit => console.log(audit.entries.first()))
* .catch(console.error);
*/
async fetchAuditLogs(options = {}) {
if (options.before && options.before instanceof GuildAuditLogsEntry) options.before = options.before.id;

async fetchAuditLogs({ before, after, limit, user, type } = {}) {
const query = makeURLSearchParams({
before: options.before,
limit: options.limit,
action_type: options.type,
before: before?.id ?? before,
after: after?.id ?? after,
limit,
action_type: type,
});

if (options.user) {
const id = this.client.users.resolveId(options.user);
if (!id) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'user', 'UserResolvable');
query.set('user_id', id);
if (user) {
const userId = this.client.users.resolveId(user);
if (!userId) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'user', 'UserResolvable');
query.set('user_id', userId);
}

const data = await this.client.rest.get(Routes.guildAuditLog(this.id), { query });
Expand Down
1 change: 1 addition & 0 deletions packages/discord.js/typings/index.d.ts
Expand Up @@ -5322,6 +5322,7 @@ export interface GuildAuditLogsEntryTargetField<TActionType extends GuildAuditLo

export interface GuildAuditLogsFetchOptions<T extends GuildAuditLogsResolvable> {
before?: Snowflake | GuildAuditLogsEntry;
after?: Snowflake | GuildAuditLogsEntry;
limit?: number;
user?: UserResolvable;
type?: T;
Expand Down

0 comments on commit 0076589

Please sign in to comment.