diff --git a/src/helpers/channels/get_channel_webhooks.ts b/src/helpers/channels/get_channel_webhooks.ts index 2fd0ecd4b..0496a8516 100644 --- a/src/helpers/channels/get_channel_webhooks.ts +++ b/src/helpers/channels/get_channel_webhooks.ts @@ -1,16 +1,22 @@ import { rest } from "../../rest/rest.ts"; -import { DiscordWebhook } from "../../types/webhooks/webhook.ts"; +import { DiscordWebhook, Webhook } from "../../types/webhooks/webhook.ts"; +import { Collection } from "../../util/collection.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; +import { snakeKeysToCamelCase } from "../../util/utils.ts"; /** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */ export async function getChannelWebhooks(channelId: string) { await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]); - const result = await rest.runMethod( + const result = (await rest.runMethod( "get", - endpoints.CHANNEL_WEBHOOKS(channelId), - ); + endpoints.CHANNEL_WEBHOOKS(channelId) + )) as DiscordWebhook[]; - return result as DiscordWebhook[]; + return new Collection( + result + .map((webhook) => snakeKeysToCamelCase(webhook)) + .map((webhook) => [webhook.id, webhook]) + ); } diff --git a/src/helpers/channels/get_channels.ts b/src/helpers/channels/get_channels.ts index 22e22c245..0a10da39a 100644 --- a/src/helpers/channels/get_channels.ts +++ b/src/helpers/channels/get_channels.ts @@ -2,6 +2,7 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; import { DiscordChannel } from "../../types/channels/channel.ts"; +import { Collection } from "../../util/collection.ts"; import { endpoints } from "../../util/constants.ts"; /** Returns a list of guild channel objects. @@ -11,22 +12,28 @@ import { endpoints } from "../../util/constants.ts"; export async function getChannels(guildId: string, addToCache = true) { const result = (await rest.runMethod( "get", - endpoints.GUILD_CHANNELS(guildId), - ) as DiscordChannel[]); + endpoints.GUILD_CHANNELS(guildId) + )) as DiscordChannel[]; - return Promise.all(result.map(async (res) => { - const discordenoChannel = await structures.createDiscordenoChannel( - res, - guildId, - ); - if (addToCache) { - await cacheHandlers.set( - "channels", - discordenoChannel.id, - discordenoChannel, - ); - } + return new Collection( + ( + await Promise.all( + result.map(async (res) => { + const discordenoChannel = await structures.createDiscordenoChannel( + res, + guildId + ); + if (addToCache) { + await cacheHandlers.set( + "channels", + discordenoChannel.id, + discordenoChannel + ); + } - return discordenoChannel; - })); + return discordenoChannel; + }) + ) + ).map((c) => [c.id, c]) + ); } diff --git a/src/helpers/emojis/get_emojis.ts b/src/helpers/emojis/get_emojis.ts index 4269a7c07..fede7fda8 100644 --- a/src/helpers/emojis/get_emojis.ts +++ b/src/helpers/emojis/get_emojis.ts @@ -3,6 +3,7 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; import { Emoji } from "../../types/emojis/emoji.ts"; import { Errors } from "../../types/misc/errors.ts"; +import { Collection } from "../../util/collection.ts"; import { endpoints } from "../../util/constants.ts"; /** @@ -29,5 +30,5 @@ export async function getEmojis(guildId: string, addToCache = true) { cacheHandlers.set("guilds", guildId, guild); } - return result; + return new Collection(result.map(e => [e.id!, e])); } diff --git a/src/helpers/invites/get_channel_invites.ts b/src/helpers/invites/get_channel_invites.ts index f5c1fe1b3..0b072f50d 100644 --- a/src/helpers/invites/get_channel_invites.ts +++ b/src/helpers/invites/get_channel_invites.ts @@ -1,15 +1,22 @@ import { rest } from "../../rest/rest.ts"; +import { DiscordInvite, Invite } from "../../types/invites/invite.ts"; +import { Collection } from "../../util/collection.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; +import { snakeKeysToCamelCase } from "../../util/utils.ts"; /** Gets the invites for this channel. Requires MANAGE_CHANNEL */ export async function getChannelInvites(channelId: string) { await requireBotChannelPermissions(channelId, ["MANAGE_CHANNELS"]); - const result = await rest.runMethod( + const result = (await rest.runMethod( "get", - endpoints.CHANNEL_INVITES(channelId), - ); + endpoints.CHANNEL_INVITES(channelId) + )) as DiscordInvite[]; - return result; + return new Collection( + result + .map((invite) => snakeKeysToCamelCase(invite)) + .map((invite) => [invite.code, invite]) + ); } diff --git a/src/helpers/invites/get_invites.ts b/src/helpers/invites/get_invites.ts index c43ff3482..1f7ea3b16 100644 --- a/src/helpers/invites/get_invites.ts +++ b/src/helpers/invites/get_invites.ts @@ -1,12 +1,22 @@ import { rest } from "../../rest/rest.ts"; +import { DiscordInvite, Invite } from "../../types/invites/invite.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"; /** Get all the invites for this guild. Requires MANAGE_GUILD permission */ export async function getInvites(guildId: string) { await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); - const result = await rest.runMethod("get", endpoints.GUILD_INVITES(guildId)); + const result = (await rest.runMethod( + "get", + endpoints.GUILD_INVITES(guildId) + )) as DiscordInvite[]; - return result; + return new Collection( + result + .map((invite) => snakeKeysToCamelCase(invite)) + .map((invite) => [invite.code, invite]) + ); } diff --git a/src/helpers/roles/get_roles.ts b/src/helpers/roles/get_roles.ts index 18a757cae..64bfd8fe8 100644 --- a/src/helpers/roles/get_roles.ts +++ b/src/helpers/roles/get_roles.ts @@ -1,6 +1,9 @@ import { rest } from "../../rest/rest.ts"; +import { DiscordRole, Role } from "../../types/permissions/role.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 role objects for the guild. * @@ -9,7 +12,14 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts"; export async function getRoles(guildId: string) { await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); - const result = await rest.runMethod("get", endpoints.GUILD_ROLES(guildId)); + const result = (await rest.runMethod( + "get", + endpoints.GUILD_ROLES(guildId) + )) as DiscordRole[]; - return result; + return new Collection( + result + .map((role) => snakeKeysToCamelCase(role)) + .map((role) => [role.id, role]) + ); } diff --git a/src/helpers/templates/get_guild_templates.ts b/src/helpers/templates/get_guild_templates.ts index f31de3a45..6f9bc6423 100644 --- a/src/helpers/templates/get_guild_templates.ts +++ b/src/helpers/templates/get_guild_templates.ts @@ -3,6 +3,7 @@ import { structures } from "../../structures/mod.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { DiscordTemplate } from "../../types/templates/template.ts"; +import { Collection } from "../../util/collection.ts"; /** * Returns an array of templates. @@ -13,8 +14,13 @@ export async function getGuildTemplates(guildId: string) { const templates = (await rest.runMethod( "get", - endpoints.GUILD_TEMPLATES(guildId), + endpoints.GUILD_TEMPLATES(guildId) )) as DiscordTemplate[]; - return templates.map((template) => structures.createTemplateStruct(template)); + return new Collection( + templates.map((template) => [ + template.code, + structures.createTemplateStruct(template), + ]) + ); } diff --git a/src/helpers/webhooks/get_webhooks.ts b/src/helpers/webhooks/get_webhooks.ts index 4a2929cd2..5fa3d335d 100644 --- a/src/helpers/webhooks/get_webhooks.ts +++ b/src/helpers/webhooks/get_webhooks.ts @@ -1,12 +1,22 @@ import { rest } from "../../rest/rest.ts"; +import { DiscordWebhook, Webhook } from "../../types/webhooks/webhook.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 guild webhooks objects. Requires the MANAGE_WEBHOOKs permission. */ export async function getWebhooks(guildId: string) { await requireBotGuildPermissions(guildId, ["MANAGE_WEBHOOKS"]); - const result = await rest.runMethod("get", endpoints.GUILD_WEBHOOKS(guildId)); + const result = (await rest.runMethod( + "get", + endpoints.GUILD_WEBHOOKS(guildId) + )) as DiscordWebhook[]; - return result; + return new Collection( + result + .map((webhook) => snakeKeysToCamelCase(webhook)) + .map((webhook) => [webhook.id, webhook]) + ); }