diff --git a/src/helpers/guilds/edit_widget.ts b/src/helpers/guilds/edit_widget.ts index 8d0e0cf93..9259cf32d 100644 --- a/src/helpers/guilds/edit_widget.ts +++ b/src/helpers/guilds/edit_widget.ts @@ -2,7 +2,6 @@ import { rest } from "../../rest/rest.ts"; import { GuildWidget } from "../../types/guilds/guild_widget.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; -import { snakeKeysToCamelCase } from "../../util/utils.ts"; /** Modify a guild widget object for the guild. Requires the MANAGE_GUILD permission. */ export async function editWidget( @@ -12,7 +11,7 @@ export async function editWidget( ) { await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); - const result = await rest.runMethod( + return await rest.runMethod( "patch", endpoints.GUILD_WIDGET(guildId), { @@ -20,6 +19,4 @@ export async function editWidget( channel_id: channelId, }, ); - - return snakeKeysToCamelCase(result); } diff --git a/src/helpers/guilds/get_audit_logs.ts b/src/helpers/guilds/get_audit_logs.ts index 795e4bd7c..95b5b3aae 100644 --- a/src/helpers/guilds/get_audit_logs.ts +++ b/src/helpers/guilds/get_audit_logs.ts @@ -3,10 +3,7 @@ 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"; +import { camelKeysToSnakeCase } from "../../util/utils.ts"; /** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */ export async function getAuditLogs( @@ -15,7 +12,7 @@ export async function getAuditLogs( ) { await requireBotGuildPermissions(guildId, ["VIEW_AUDIT_LOG"]); - const result = await rest.runMethod( + return await rest.runMethod( "get", endpoints.GUILD_AUDIT_LOGS(guildId), camelKeysToSnakeCase({ @@ -25,6 +22,4 @@ export async function getAuditLogs( : 50, }), ); - - return snakeKeysToCamelCase(result); } diff --git a/src/helpers/guilds/get_available_voice_regions.ts b/src/helpers/guilds/get_available_voice_regions.ts index 944973477..5570e4916 100644 --- a/src/helpers/guilds/get_available_voice_regions.ts +++ b/src/helpers/guilds/get_available_voice_regions.ts @@ -4,7 +4,5 @@ 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 as VoiceRegion; + return await rest.runMethod("get", endpoints.VOICE_REGIONS); } diff --git a/src/helpers/guilds/get_ban.ts b/src/helpers/guilds/get_ban.ts index 7abe6d3b0..3fddcd1a8 100644 --- a/src/helpers/guilds/get_ban.ts +++ b/src/helpers/guilds/get_ban.ts @@ -2,16 +2,13 @@ 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) { await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]); - const result = await rest.runMethod( + return await rest.runMethod( "get", endpoints.GUILD_BAN(guildId, memberId), ); - - return snakeKeysToCamelCase(result); } diff --git a/src/helpers/guilds/get_bans.ts b/src/helpers/guilds/get_bans.ts index 1fdef4fcd..c9497f8b4 100644 --- a/src/helpers/guilds/get_bans.ts +++ b/src/helpers/guilds/get_bans.ts @@ -1,20 +1,19 @@ import { rest } from "../../rest/rest.ts"; -import { Ban, DiscordBan } from "../../types/guilds/ban.ts"; +import { Ban } 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) { await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]); - const results = (await rest.runMethod( + const results = await rest.runMethod( "get", endpoints.GUILD_BANS(guildId), - )) as DiscordBan[]; + ); return new Collection( - results.map((res) => [res.user.id, snakeKeysToCamelCase(res)]), + results.map((res) => [res.user.id, res]), ); } diff --git a/src/helpers/guilds/get_guild.ts b/src/helpers/guilds/get_guild.ts index 5652b979b..82b6e5c54 100644 --- a/src/helpers/guilds/get_guild.ts +++ b/src/helpers/guilds/get_guild.ts @@ -1,7 +1,9 @@ +import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; +import { structures } from "../../structures/mod.ts"; import { Guild } from "../../types/guilds/guild.ts"; import { endpoints } from "../../util/constants.ts"; -import { snakeKeysToCamelCase } from "../../util/utils.ts"; +import { ws } from "../../ws/ws.ts"; /** * ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get() @@ -10,10 +12,29 @@ import { snakeKeysToCamelCase } from "../../util/utils.ts"; * This function fetches a guild's data. This is not the same data as a GUILD_CREATE. * So it does not cache the guild, you must do it manually. * */ -export async function getGuild(guildId: string, counts = true) { - const result = await rest.runMethod("get", endpoints.GUILDS_BASE(guildId), { - with_counts: counts, - }); +export async function getGuild( + guildId: string, + options: { counts?: boolean; addToCache?: boolean } = { + counts: true, + addToCache: true, + }, +) { + const result = await rest.runMethod( + "get", + endpoints.GUILDS_BASE(guildId), + { + with_counts: options.counts, + }, + ); - return snakeKeysToCamelCase(result); + const structure = await structures.createDiscordenoGuild( + result, + (BigInt(guildId) >> 22n) % BigInt(ws.botGatewayData.shards), + ); + + if (options.addToCache) { + await cacheHandlers.set("guilds", guildId, structure); + } + + return structure; } diff --git a/src/helpers/guilds/get_guild_preview.ts b/src/helpers/guilds/get_guild_preview.ts index 274f20a28..40873cf0f 100644 --- a/src/helpers/guilds/get_guild_preview.ts +++ b/src/helpers/guilds/get_guild_preview.ts @@ -1,11 +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 snakeKeysToCamelCase(result); + return await rest.runMethod( + "get", + endpoints.GUILD_PREVIEW(guildId), + ); } diff --git a/src/helpers/guilds/get_vainty_url.ts b/src/helpers/guilds/get_vainty_url.ts index 999c34884..0dee86af7 100644 --- a/src/helpers/guilds/get_vainty_url.ts +++ b/src/helpers/guilds/get_vainty_url.ts @@ -1,18 +1,15 @@ 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 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 snakeKeysToCamelCase< + return await rest.runMethod< (Partial & Pick) | { code: null; } - >(result); + >( + "get", + endpoints.GUILD_VANITY_URL(guildId), + ); } diff --git a/src/helpers/guilds/get_welcome_screen.ts b/src/helpers/guilds/get_welcome_screen.ts index 4163c4f71..36950e3eb 100644 --- a/src/helpers/guilds/get_welcome_screen.ts +++ b/src/helpers/guilds/get_welcome_screen.ts @@ -1,13 +1,10 @@ import { rest } from "../../rest/rest.ts"; import { WelcomeScreen } from "../../types/mod.ts"; import { endpoints } from "../../util/constants.ts"; -import { snakeKeysToCamelCase } from "../../util/utils.ts"; export async function getWelcomeScreen(guildId: string) { - const result = await rest.runMethod( + return await rest.runMethod( "get", endpoints.GUILD_WELCOME_SCREEN(guildId), ); - - return snakeKeysToCamelCase(result); } diff --git a/src/helpers/guilds/get_widget.ts b/src/helpers/guilds/get_widget.ts index f44197d37..c685b8088 100644 --- a/src/helpers/guilds/get_widget.ts +++ b/src/helpers/guilds/get_widget.ts @@ -11,5 +11,6 @@ export async function getWidget(guildId: string, options?: { force: boolean }) { if (!guild?.widgetEnabled) throw new Error(Errors.GUILD_WIDGET_NOT_ENABLED); } - return rest.runMethod("get", `${endpoints.GUILD_WIDGET(guildId)}.json`); + // TODO: add return type + return await rest.runMethod("get", `${endpoints.GUILD_WIDGET(guildId)}.json`); } diff --git a/src/helpers/guilds/get_widget_settings.ts b/src/helpers/guilds/get_widget_settings.ts index 8d28e8c9c..960fb83ec 100644 --- a/src/helpers/guilds/get_widget_settings.ts +++ b/src/helpers/guilds/get_widget_settings.ts @@ -7,7 +7,8 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts"; export async function getWidgetSettings(guildId: string) { await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); - const result = await rest.runMethod("get", endpoints.GUILD_WIDGET(guildId)); - - return result as GuildWidget; + return await rest.runMethod( + "get", + endpoints.GUILD_WIDGET(guildId), + ); } diff --git a/src/helpers/guilds/leave_guild.ts b/src/helpers/guilds/leave_guild.ts index 805b56885..9552ffe1a 100644 --- a/src/helpers/guilds/leave_guild.ts +++ b/src/helpers/guilds/leave_guild.ts @@ -2,8 +2,9 @@ import { rest } from "../../rest/rest.ts"; import { endpoints } from "../../util/constants.ts"; /** Leave a guild */ -export async function leaveGuild(guildId: string): Promise { - const result = await rest.runMethod("delete", endpoints.GUILD_LEAVE(guildId)); - - return result; +export async function leaveGuild(guildId: string) { + return await rest.runMethod( + "delete", + endpoints.GUILD_LEAVE(guildId), + ); }