Files
discordeno/helpers/guilds/getAuditLogs.ts
Skillz4Killz a0a1554756 refactor: typings using ReturnType (#2105)
* fix: check new types idea

* fix: type errors

* fix: new style

* fix: more cleanup

* fix: more cleanup

* fix: cleanup audit logs

* fix: cleanup stickers

* fix: cleanup integrations

* fix: more cleanup

* fix: organize into 1 place

* fix: few errors

* fix: some broken import fixes

* fix: quite a lot of fixes across the board

* fix: more fixes for broken imports

* fix: more fixes for broken imports

* fix: handler imports

* fix: all remaining import errors

* fix: more errors needing fixes

* fix: clearing up transformers

* fix: few moer types

* fix: more cleanup of extra types

* fix: fmt

* fix: cleanup discordeno file

* Nuke Base Types (#2102)

* fix: cleanup snake stuff

* convert camelCase to snake_case (#2103)

* fix: add camelize

* fix: finalize remaining errors

* fix: imports in test

Co-authored-by: LTS20050703 <87189679+lts20050703@users.noreply.github.com>
2022-03-14 22:11:22 -04:00

67 lines
3.2 KiB
TypeScript

import type { Bot } from "../../bot.ts";
import { DiscordAuditLog } from "../../types/discord.ts";
import { AuditLogEvents } from "../../types/shared.ts";
/** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */
export async function getAuditLogs(bot: Bot, guildId: bigint, options?: GetGuildAuditLog) {
if (options?.userId) options.userId = options.userId.toString();
if (options?.before) options.before = options.before.toString();
if (options?.limit) options.limit = options.limit >= 1 && options.limit <= 100 ? options.limit : 50;
const auditlog = await bot.rest.runMethod<DiscordAuditLog>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_AUDIT_LOGS(guildId),
options,
);
return {
users: auditlog.users.map((user) => bot.transformers.user(bot, user)),
webhook: auditlog.webhooks.map((hook) => bot.transformers.webhook(bot, hook)),
auditLogEntries: auditlog.audit_log_entries.map((entry) => bot.transformers.auditlogEntry(bot, entry)),
integrations: auditlog.integrations.map((integration) => ({
id: integration.id ? bot.transformers.snowflake(integration.id) : undefined,
name: integration.name,
type: integration.type,
enabled: integration.enabled,
syncing: integration.syncing,
roleId: integration.role_id ? bot.transformers.snowflake(integration.role_id) : undefined,
enableEmoticons: integration.enable_emoticons,
expireBehavior: integration.expire_behavior,
expireGracePeriod: integration.expire_grace_period,
user: integration.user ? bot.transformers.user(bot, integration.user) : undefined,
account: {
id: integration.account?.id ? bot.transformers.snowflake(integration.account.id) : undefined,
name: integration.account?.name,
},
syncedAt: integration.synced_at ? Date.parse(integration.synced_at) : undefined,
subscriberCount: integration.subscriber_count,
revoked: integration.revoked,
application: integration.application
? {
id: bot.transformers.snowflake(integration.application.id),
name: integration.application.name,
icon: integration.application.icon ? bot.utils.iconHashToBigInt(integration.application.icon) : undefined,
description: integration.application.description,
summary: integration.application.summary,
bot: integration.application.bot ? bot.transformers.user(bot, integration.application.bot) : undefined,
}
: undefined,
})),
threads: auditlog.threads.map((thread) => bot.transformers.channel(bot, { channel: thread, guildId })),
scheduledEvents: auditlog.guild_scheduled_events?.map((event) => bot.transformers.scheduledEvent(bot, event)),
};
}
/** https://discord.com/developers/docs/resources/audit-log#get-guild-audit-log-query-string-parameters */
export interface GetGuildAuditLog {
/** Filter the log for actions made by a user */
userId?: bigint | string;
/** The type of audit log event */
actionType?: AuditLogEvents;
/** Filter the log before a certain entry id */
before?: bigint | string;
/** How many entries are returned (default 50, minimum 1, maximum 100) */
limit?: number;
}