From 313e7d2d353cc75ec2f1e109e5aebccc6f6c9194 Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Wed, 7 Apr 2021 10:27:44 +0200 Subject: [PATCH] fir more things --- src/helpers/guilds/get_audit_logs.ts | 17 +++++++++------- .../guilds/get_available_voice_regions.ts | 3 ++- src/helpers/guilds/get_ban.ts | 4 +++- src/helpers/guilds/get_bans.ts | 8 +++++--- src/helpers/guilds/get_guild.ts | 4 +++- src/helpers/guilds/get_guild_preview.ts | 4 +++- src/helpers/guilds/get_prune_count.ts | 10 +++++++--- src/helpers/guilds/get_vainty_url.ts | 8 ++++++-- src/helpers/guilds/get_voice_regions.ts | 17 ++++++++++++++-- src/helpers/guilds/get_widget_image_url.ts | 6 ++---- src/helpers/guilds/guild_banner_url.ts | 2 +- src/helpers/guilds/guild_icon_url.ts | 2 +- src/helpers/guilds/guild_splash_url.ts | 2 +- src/helpers/guilds/leave_guild.ts | 2 +- src/types/invites/invite_metadata.ts | 3 ++- src/util/utils.ts | 20 +++++++++++-------- 16 files changed, 74 insertions(+), 38 deletions(-) diff --git a/src/helpers/guilds/get_audit_logs.ts b/src/helpers/guilds/get_audit_logs.ts index 63ee31d2f..241c21ff4 100644 --- a/src/helpers/guilds/get_audit_logs.ts +++ b/src/helpers/guilds/get_audit_logs.ts @@ -1,27 +1,30 @@ import { rest } from "../../rest/rest.ts"; +import { AuditLog } from "../../types/audit_log/audit_log.ts"; +import { GetGuildAuditLog } from "../../types/audit_log/get_guild_audit_log.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import { + camelKeysToSnakeCase, + snakeKeysToCamelCase, +} from "../../util/utils.ts"; /** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */ export async function getAuditLogs( guildId: string, - options: GetAuditLogsOptions, + options: GetGuildAuditLog, ) { await requireBotGuildPermissions(guildId, ["VIEW_AUDIT_LOG"]); const result = await rest.runMethod( "get", endpoints.GUILD_AUDIT_LOGS(guildId), - { + camelKeysToSnakeCase({ ...options, - action_type: options.action_type - ? AuditLogs[options.action_type] - : undefined, limit: options.limit && options.limit >= 1 && options.limit <= 100 ? options.limit : 50, - }, + }), ); - return result; + return snakeKeysToCamelCase(result) as AuditLog; } diff --git a/src/helpers/guilds/get_available_voice_regions.ts b/src/helpers/guilds/get_available_voice_regions.ts index b1f2b0c67..944973477 100644 --- a/src/helpers/guilds/get_available_voice_regions.ts +++ b/src/helpers/guilds/get_available_voice_regions.ts @@ -1,9 +1,10 @@ import { rest } from "../../rest/rest.ts"; +import { VoiceRegion } from "../../types/voice/voice_region.ts"; import { endpoints } from "../../util/constants.ts"; /** Returns an array of voice regions that can be used when creating servers. */ export async function getAvailableVoiceRegions() { const result = await rest.runMethod("get", endpoints.VOICE_REGIONS); - return result; + return result as VoiceRegion; } diff --git a/src/helpers/guilds/get_ban.ts b/src/helpers/guilds/get_ban.ts index 8d3751965..1cac4ac1d 100644 --- a/src/helpers/guilds/get_ban.ts +++ b/src/helpers/guilds/get_ban.ts @@ -1,6 +1,8 @@ import { rest } from "../../rest/rest.ts"; +import { Ban } from "../../types/guilds/ban.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import { snakeKeysToCamelCase } from "../../util/utils.ts"; /** Returns a ban object for the given user or a 404 not found if the ban cannot be found. Requires the BAN_MEMBERS permission. */ export async function getBan(guildId: string, memberId: string) { @@ -11,5 +13,5 @@ export async function getBan(guildId: string, memberId: string) { endpoints.GUILD_BAN(guildId, memberId), ); - return result as BannedUser; + return snakeKeysToCamelCase(result) as Ban; } diff --git a/src/helpers/guilds/get_bans.ts b/src/helpers/guilds/get_bans.ts index c32820525..c55e27e5d 100644 --- a/src/helpers/guilds/get_bans.ts +++ b/src/helpers/guilds/get_bans.ts @@ -1,7 +1,9 @@ import { rest } from "../../rest/rest.ts"; +import { Ban, DiscordBan } from "../../types/guilds/ban.ts"; import { Collection } from "../../util/collection.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import { snakeKeysToCamelCase } from "../../util/utils.ts"; /** Returns a list of ban objects for the users banned from this guild. Requires the BAN_MEMBERS permission. */ export async function getBans(guildId: string) { @@ -10,9 +12,9 @@ export async function getBans(guildId: string) { const results = (await rest.runMethod( "get", endpoints.GUILD_BANS(guildId), - )) as BannedUser[]; + )) as DiscordBan[]; - return new Collection( - results.map((res) => [res.user.id, res]), + return new Collection( + results.map((res) => [res.user.id, snakeKeysToCamelCase(res) as Ban]), ); } diff --git a/src/helpers/guilds/get_guild.ts b/src/helpers/guilds/get_guild.ts index 0a46b7d5b..b0b39700b 100644 --- a/src/helpers/guilds/get_guild.ts +++ b/src/helpers/guilds/get_guild.ts @@ -1,5 +1,7 @@ import { rest } from "../../rest/rest.ts"; +import { Guild } from "../../types/guilds/guild.ts"; import { endpoints } from "../../util/constants.ts"; +import { snakeKeysToCamelCase } from "../../util/utils.ts"; /** * ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get() @@ -13,5 +15,5 @@ export async function getGuild(guildId: string, counts = true) { with_counts: counts, }); - return result as UpdateGuildPayload; + return snakeKeysToCamelCase(result) as Guild; } diff --git a/src/helpers/guilds/get_guild_preview.ts b/src/helpers/guilds/get_guild_preview.ts index 6d7cc9386..dfd398273 100644 --- a/src/helpers/guilds/get_guild_preview.ts +++ b/src/helpers/guilds/get_guild_preview.ts @@ -1,9 +1,11 @@ import { rest } from "../../rest/rest.ts"; +import { GuildPreview } from "../../types/guilds/guild_preview.ts"; import { endpoints } from "../../util/constants.ts"; +import { snakeKeysToCamelCase } from "../../util/utils.ts"; /** Returns the guild preview object for the given id. If the bot is not in the guild, then the guild must be Discoverable. */ export async function getGuildPreview(guildId: string) { const result = await rest.runMethod("get", endpoints.GUILD_PREVIEW(guildId)); - return result; + return snakeKeysToCamelCase(result) as GuildPreview; } diff --git a/src/helpers/guilds/get_prune_count.ts b/src/helpers/guilds/get_prune_count.ts index 1d81987ba..12b35b418 100644 --- a/src/helpers/guilds/get_prune_count.ts +++ b/src/helpers/guilds/get_prune_count.ts @@ -1,11 +1,15 @@ import { rest } from "../../rest/rest.ts"; +import { GetGuildPruneCountQuery } from "../../types/guilds/get_guild_prune_count.ts"; import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { camelKeysToSnakeCase } from "../../util/utils.ts"; /** Check how many members would be removed from the server in a prune operation. Requires the KICK_MEMBERS permission */ -export async function getPruneCount(guildId: string, options?: PruneOptions) { +export async function getPruneCount( + guildId: string, + options?: GetGuildPruneCountQuery, +) { if (options?.days && options.days < 1) throw new Error(Errors.PRUNE_MIN_DAYS); if (options?.days && options.days > 30) { throw new Error(Errors.PRUNE_MAX_DAYS); @@ -17,7 +21,7 @@ export async function getPruneCount(guildId: string, options?: PruneOptions) { "get", endpoints.GUILD_PRUNE(guildId), camelKeysToSnakeCase(options ?? {}), - ) as PrunePayload; + ); - return result.pruned; + return result.pruned as number; } diff --git a/src/helpers/guilds/get_vainty_url.ts b/src/helpers/guilds/get_vainty_url.ts index 1ccac2a89..c80c65a81 100644 --- a/src/helpers/guilds/get_vainty_url.ts +++ b/src/helpers/guilds/get_vainty_url.ts @@ -1,12 +1,16 @@ import { rest } from "../../rest/rest.ts"; +import { InviteMetadata } from "../../types/invites/invite_metadata.ts"; import { endpoints } from "../../util/constants.ts"; +import { snakeKeysToCamelCase } from "../../util/utils.ts"; -/** Returns the code and uses of the vanity url for this server if it is enabled. Requires the MANAGE_GUILD permission. */ +/** Returns the code and uses of the vanity url for this server if it is enabled else `code` will be null. Requires the `MANAGE_GUILD` permission. */ export async function getVanityURL(guildId: string) { const result = await rest.runMethod( "get", endpoints.GUILD_VANITY_URL(guildId), ); - return result; + return snakeKeysToCamelCase(result) as + | (Partial & Pick) + | { code: null }; } diff --git a/src/helpers/guilds/get_voice_regions.ts b/src/helpers/guilds/get_voice_regions.ts index 8ac73d10a..166c7fa2a 100644 --- a/src/helpers/guilds/get_voice_regions.ts +++ b/src/helpers/guilds/get_voice_regions.ts @@ -1,9 +1,22 @@ import { rest } from "../../rest/rest.ts"; +import { + DiscordVoiceRegion, + VoiceRegion, +} from "../../types/voice/voice_region.ts"; +import { Collection } from "../../util/collection.ts"; import { endpoints } from "../../util/constants.ts"; +import { snakeKeysToCamelCase } from "../../util/utils.ts"; /** Returns a list of voice region objects for the guild. Unlike the similar /voice route, this returns VIP servers when the guild is VIP-enabled. */ export async function getVoiceRegions(guildId: string) { - const result = await rest.runMethod("get", endpoints.GUILD_REGIONS(guildId)); + const result = await rest.runMethod( + "get", + endpoints.GUILD_REGIONS(guildId), + ) as DiscordVoiceRegion[]; - return result; + const convertedRegions = snakeKeysToCamelCase(result); + + return new Collection( + convertedRegions.map((region) => [region.id, region]), + ); } diff --git a/src/helpers/guilds/get_widget_image_url.ts b/src/helpers/guilds/get_widget_image_url.ts index 6e9eb01b8..f552a2326 100644 --- a/src/helpers/guilds/get_widget_image_url.ts +++ b/src/helpers/guilds/get_widget_image_url.ts @@ -1,14 +1,12 @@ import { cacheHandlers } from "../../cache.ts"; +import { GetGuildWidgetImageQuery } from "../../types/guilds/get_guild_widget_image.ts"; import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; /** Returns the widget image URL for the guild. */ export async function getWidgetImageURL( guildId: string, - options?: { - style?: "shield" | "banner1" | "banner2" | "banner3" | "banner4"; - force?: boolean; - }, + options?: GetGuildWidgetImageQuery & { force?: boolean }, ) { if (!options?.force) { const guild = await cacheHandlers.get("guilds", guildId); diff --git a/src/helpers/guilds/guild_banner_url.ts b/src/helpers/guilds/guild_banner_url.ts index 89e07efe4..db027d15b 100644 --- a/src/helpers/guilds/guild_banner_url.ts +++ b/src/helpers/guilds/guild_banner_url.ts @@ -6,7 +6,7 @@ import { formatImageURL } from "../../util/utils.ts"; /** The full URL of the banner from Discords CDN. Undefined if no banner is set. */ export function guildBannerURL( id: string, - banner: string, + banner?: string, size: DiscordImageSize = 128, format?: DiscordImageFormat, ) { diff --git a/src/helpers/guilds/guild_icon_url.ts b/src/helpers/guilds/guild_icon_url.ts index 5acb8408f..b1e02e6f2 100644 --- a/src/helpers/guilds/guild_icon_url.ts +++ b/src/helpers/guilds/guild_icon_url.ts @@ -6,7 +6,7 @@ import { formatImageURL } from "../../util/utils.ts"; /** The full URL of the icon from Discords CDN. Undefined when no icon is set. */ export function guildIconURL( id: string, - icon: string, + icon?: string, size: DiscordImageSize = 128, format?: DiscordImageFormat, ) { diff --git a/src/helpers/guilds/guild_splash_url.ts b/src/helpers/guilds/guild_splash_url.ts index 0c51fb682..9409a0e99 100644 --- a/src/helpers/guilds/guild_splash_url.ts +++ b/src/helpers/guilds/guild_splash_url.ts @@ -6,7 +6,7 @@ import { formatImageURL } from "../../util/utils.ts"; /** The full URL of the splash from Discords CDN. Undefined if no splash is set. */ export function guildSplashURL( id: string, - splash: string, + splash?: string, size: DiscordImageSize = 128, format?: DiscordImageFormat, ) { diff --git a/src/helpers/guilds/leave_guild.ts b/src/helpers/guilds/leave_guild.ts index d103a0df3..805b56885 100644 --- a/src/helpers/guilds/leave_guild.ts +++ b/src/helpers/guilds/leave_guild.ts @@ -2,7 +2,7 @@ import { rest } from "../../rest/rest.ts"; import { endpoints } from "../../util/constants.ts"; /** Leave a guild */ -export async function leaveGuild(guildId: string) { +export async function leaveGuild(guildId: string): Promise { const result = await rest.runMethod("delete", endpoints.GUILD_LEAVE(guildId)); return result; diff --git a/src/types/invites/invite_metadata.ts b/src/types/invites/invite_metadata.ts index 40d654ba5..e897bdc3c 100644 --- a/src/types/invites/invite_metadata.ts +++ b/src/types/invites/invite_metadata.ts @@ -1,6 +1,7 @@ import { SnakeCaseProps } from "../util.ts"; +import { Invite } from "./invite.ts"; -export interface InviteMetadata { +export interface InviteMetadata extends Invite { /** Number of times this invite has been used */ uses: number; /** Max number of times this invite can be used */ diff --git a/src/util/utils.ts b/src/util/utils.ts index f06d38b7e..5c012433e 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -72,7 +72,9 @@ function isObject(obj: unknown) { } // deno-lint-ignore no-explicit-any -export function camelKeysToSnakeCase(obj: Record) { +export function camelKeysToSnakeCase( + obj: Record | Record[], +): T { if (isObject(obj)) { // deno-lint-ignore no-explicit-any const convertedObject: Record = {}; @@ -80,20 +82,22 @@ export function camelKeysToSnakeCase(obj: Record) { Object.keys(obj) .forEach((key) => { convertedObject[camelToSnakeCase(key)] = camelKeysToSnakeCase( - obj[key], + (obj as Record)[key], ); }); - return convertedObject; + return convertedObject as T; } else if (Array.isArray(obj)) { obj = obj.map((element) => camelKeysToSnakeCase(element)); } - return obj; + return obj as T; } // deno-lint-ignore no-explicit-any -export function snakeKeysToCamelCase(obj: Record) { +export function snakeKeysToCamelCase( + obj: Record | Record[], +): T { if (isObject(obj)) { // deno-lint-ignore no-explicit-any const convertedObject: Record = {}; @@ -101,16 +105,16 @@ export function snakeKeysToCamelCase(obj: Record) { Object.keys(obj) .forEach((key) => { convertedObject[snakeToCamelCase(key)] = snakeKeysToCamelCase( - obj[key], + (obj as Record)[key], ); }); - return convertedObject; + return convertedObject as T; } else if (Array.isArray(obj)) { obj = obj.map((element) => snakeKeysToCamelCase(element)); } - return obj; + return obj as T; } /** @private */