From c2f2afb211e6f154de59af0bba7f7f938d86fa78 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Tue, 13 Apr 2021 17:53:49 +0000 Subject: [PATCH 1/3] fix: return types [] => Collection --- src/helpers/channels/get_channel_webhooks.ts | 16 +++++--- src/helpers/channels/get_channels.ts | 39 ++++++++++++-------- src/helpers/emojis/get_emojis.ts | 3 +- src/helpers/invites/get_channel_invites.ts | 15 ++++++-- src/helpers/invites/get_invites.ts | 14 ++++++- src/helpers/roles/get_roles.ts | 14 ++++++- src/helpers/templates/get_guild_templates.ts | 10 ++++- src/helpers/webhooks/get_webhooks.ts | 14 ++++++- 8 files changed, 91 insertions(+), 34 deletions(-) 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]) + ); } From 1cca9a6cb400de262bcea18ebd98d0a29af7fafc Mon Sep 17 00:00:00 2001 From: Skillz4Killz Date: Tue, 13 Apr 2021 17:54:13 +0000 Subject: [PATCH 2/3] --- src/helpers/channels/get_channel_webhooks.ts | 4 ++-- src/helpers/channels/get_channels.ts | 10 +++++----- src/helpers/emojis/get_emojis.ts | 2 +- src/helpers/invites/get_channel_invites.ts | 4 ++-- src/helpers/invites/get_invites.ts | 4 ++-- src/helpers/roles/get_roles.ts | 4 ++-- src/helpers/templates/get_guild_templates.ts | 4 ++-- src/helpers/webhooks/get_webhooks.ts | 4 ++-- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/helpers/channels/get_channel_webhooks.ts b/src/helpers/channels/get_channel_webhooks.ts index 0496a8516..78bf1caca 100644 --- a/src/helpers/channels/get_channel_webhooks.ts +++ b/src/helpers/channels/get_channel_webhooks.ts @@ -11,12 +11,12 @@ export async function getChannelWebhooks(channelId: string) { const result = (await rest.runMethod( "get", - endpoints.CHANNEL_WEBHOOKS(channelId) + endpoints.CHANNEL_WEBHOOKS(channelId), )) as DiscordWebhook[]; return new Collection( result .map((webhook) => snakeKeysToCamelCase(webhook)) - .map((webhook) => [webhook.id, webhook]) + .map((webhook) => [webhook.id, webhook]), ); } diff --git a/src/helpers/channels/get_channels.ts b/src/helpers/channels/get_channels.ts index 0a10da39a..df7543b7e 100644 --- a/src/helpers/channels/get_channels.ts +++ b/src/helpers/channels/get_channels.ts @@ -12,7 +12,7 @@ import { endpoints } from "../../util/constants.ts"; export async function getChannels(guildId: string, addToCache = true) { const result = (await rest.runMethod( "get", - endpoints.GUILD_CHANNELS(guildId) + endpoints.GUILD_CHANNELS(guildId), )) as DiscordChannel[]; return new Collection( @@ -21,19 +21,19 @@ export async function getChannels(guildId: string, addToCache = true) { result.map(async (res) => { const discordenoChannel = await structures.createDiscordenoChannel( res, - guildId + guildId, ); if (addToCache) { await cacheHandlers.set( "channels", discordenoChannel.id, - discordenoChannel + discordenoChannel, ); } return discordenoChannel; - }) + }), ) - ).map((c) => [c.id, c]) + ).map((c) => [c.id, c]), ); } diff --git a/src/helpers/emojis/get_emojis.ts b/src/helpers/emojis/get_emojis.ts index fede7fda8..140244d0c 100644 --- a/src/helpers/emojis/get_emojis.ts +++ b/src/helpers/emojis/get_emojis.ts @@ -30,5 +30,5 @@ export async function getEmojis(guildId: string, addToCache = true) { cacheHandlers.set("guilds", guildId, guild); } - return new Collection(result.map(e => [e.id!, e])); + 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 0b072f50d..b75b831f9 100644 --- a/src/helpers/invites/get_channel_invites.ts +++ b/src/helpers/invites/get_channel_invites.ts @@ -11,12 +11,12 @@ export async function getChannelInvites(channelId: string) { const result = (await rest.runMethod( "get", - endpoints.CHANNEL_INVITES(channelId) + endpoints.CHANNEL_INVITES(channelId), )) as DiscordInvite[]; return new Collection( result .map((invite) => snakeKeysToCamelCase(invite)) - .map((invite) => [invite.code, invite]) + .map((invite) => [invite.code, invite]), ); } diff --git a/src/helpers/invites/get_invites.ts b/src/helpers/invites/get_invites.ts index 1f7ea3b16..e8aa0d594 100644 --- a/src/helpers/invites/get_invites.ts +++ b/src/helpers/invites/get_invites.ts @@ -11,12 +11,12 @@ export async function getInvites(guildId: string) { const result = (await rest.runMethod( "get", - endpoints.GUILD_INVITES(guildId) + endpoints.GUILD_INVITES(guildId), )) as DiscordInvite[]; return new Collection( result .map((invite) => snakeKeysToCamelCase(invite)) - .map((invite) => [invite.code, invite]) + .map((invite) => [invite.code, invite]), ); } diff --git a/src/helpers/roles/get_roles.ts b/src/helpers/roles/get_roles.ts index 64bfd8fe8..2edeb648e 100644 --- a/src/helpers/roles/get_roles.ts +++ b/src/helpers/roles/get_roles.ts @@ -14,12 +14,12 @@ export async function getRoles(guildId: string) { const result = (await rest.runMethod( "get", - endpoints.GUILD_ROLES(guildId) + endpoints.GUILD_ROLES(guildId), )) as DiscordRole[]; return new Collection( result .map((role) => snakeKeysToCamelCase(role)) - .map((role) => [role.id, 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 6f9bc6423..1a2ad579d 100644 --- a/src/helpers/templates/get_guild_templates.ts +++ b/src/helpers/templates/get_guild_templates.ts @@ -14,13 +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 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 5fa3d335d..ff8cbf224 100644 --- a/src/helpers/webhooks/get_webhooks.ts +++ b/src/helpers/webhooks/get_webhooks.ts @@ -11,12 +11,12 @@ export async function getWebhooks(guildId: string) { const result = (await rest.runMethod( "get", - endpoints.GUILD_WEBHOOKS(guildId) + endpoints.GUILD_WEBHOOKS(guildId), )) as DiscordWebhook[]; return new Collection( result .map((webhook) => snakeKeysToCamelCase(webhook)) - .map((webhook) => [webhook.id, webhook]) + .map((webhook) => [webhook.id, webhook]), ); } From 9446960bb07062823ed8c93e18b5ed18b4a706b6 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Tue, 13 Apr 2021 17:59:19 +0000 Subject: [PATCH 3/3] deno fmt --- src/helpers/channels/get_channel_webhooks.ts | 4 ++-- src/helpers/channels/get_channels.ts | 10 +++++----- src/helpers/emojis/get_emojis.ts | 2 +- src/helpers/invites/get_channel_invites.ts | 4 ++-- src/helpers/invites/get_invites.ts | 4 ++-- src/helpers/roles/get_roles.ts | 4 ++-- src/helpers/templates/get_guild_templates.ts | 4 ++-- src/helpers/webhooks/get_webhooks.ts | 4 ++-- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/helpers/channels/get_channel_webhooks.ts b/src/helpers/channels/get_channel_webhooks.ts index 0496a8516..78bf1caca 100644 --- a/src/helpers/channels/get_channel_webhooks.ts +++ b/src/helpers/channels/get_channel_webhooks.ts @@ -11,12 +11,12 @@ export async function getChannelWebhooks(channelId: string) { const result = (await rest.runMethod( "get", - endpoints.CHANNEL_WEBHOOKS(channelId) + endpoints.CHANNEL_WEBHOOKS(channelId), )) as DiscordWebhook[]; return new Collection( result .map((webhook) => snakeKeysToCamelCase(webhook)) - .map((webhook) => [webhook.id, webhook]) + .map((webhook) => [webhook.id, webhook]), ); } diff --git a/src/helpers/channels/get_channels.ts b/src/helpers/channels/get_channels.ts index 0a10da39a..df7543b7e 100644 --- a/src/helpers/channels/get_channels.ts +++ b/src/helpers/channels/get_channels.ts @@ -12,7 +12,7 @@ import { endpoints } from "../../util/constants.ts"; export async function getChannels(guildId: string, addToCache = true) { const result = (await rest.runMethod( "get", - endpoints.GUILD_CHANNELS(guildId) + endpoints.GUILD_CHANNELS(guildId), )) as DiscordChannel[]; return new Collection( @@ -21,19 +21,19 @@ export async function getChannels(guildId: string, addToCache = true) { result.map(async (res) => { const discordenoChannel = await structures.createDiscordenoChannel( res, - guildId + guildId, ); if (addToCache) { await cacheHandlers.set( "channels", discordenoChannel.id, - discordenoChannel + discordenoChannel, ); } return discordenoChannel; - }) + }), ) - ).map((c) => [c.id, c]) + ).map((c) => [c.id, c]), ); } diff --git a/src/helpers/emojis/get_emojis.ts b/src/helpers/emojis/get_emojis.ts index fede7fda8..140244d0c 100644 --- a/src/helpers/emojis/get_emojis.ts +++ b/src/helpers/emojis/get_emojis.ts @@ -30,5 +30,5 @@ export async function getEmojis(guildId: string, addToCache = true) { cacheHandlers.set("guilds", guildId, guild); } - return new Collection(result.map(e => [e.id!, e])); + 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 0b072f50d..b75b831f9 100644 --- a/src/helpers/invites/get_channel_invites.ts +++ b/src/helpers/invites/get_channel_invites.ts @@ -11,12 +11,12 @@ export async function getChannelInvites(channelId: string) { const result = (await rest.runMethod( "get", - endpoints.CHANNEL_INVITES(channelId) + endpoints.CHANNEL_INVITES(channelId), )) as DiscordInvite[]; return new Collection( result .map((invite) => snakeKeysToCamelCase(invite)) - .map((invite) => [invite.code, invite]) + .map((invite) => [invite.code, invite]), ); } diff --git a/src/helpers/invites/get_invites.ts b/src/helpers/invites/get_invites.ts index 1f7ea3b16..e8aa0d594 100644 --- a/src/helpers/invites/get_invites.ts +++ b/src/helpers/invites/get_invites.ts @@ -11,12 +11,12 @@ export async function getInvites(guildId: string) { const result = (await rest.runMethod( "get", - endpoints.GUILD_INVITES(guildId) + endpoints.GUILD_INVITES(guildId), )) as DiscordInvite[]; return new Collection( result .map((invite) => snakeKeysToCamelCase(invite)) - .map((invite) => [invite.code, invite]) + .map((invite) => [invite.code, invite]), ); } diff --git a/src/helpers/roles/get_roles.ts b/src/helpers/roles/get_roles.ts index 64bfd8fe8..2edeb648e 100644 --- a/src/helpers/roles/get_roles.ts +++ b/src/helpers/roles/get_roles.ts @@ -14,12 +14,12 @@ export async function getRoles(guildId: string) { const result = (await rest.runMethod( "get", - endpoints.GUILD_ROLES(guildId) + endpoints.GUILD_ROLES(guildId), )) as DiscordRole[]; return new Collection( result .map((role) => snakeKeysToCamelCase(role)) - .map((role) => [role.id, 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 6f9bc6423..1a2ad579d 100644 --- a/src/helpers/templates/get_guild_templates.ts +++ b/src/helpers/templates/get_guild_templates.ts @@ -14,13 +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 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 5fa3d335d..ff8cbf224 100644 --- a/src/helpers/webhooks/get_webhooks.ts +++ b/src/helpers/webhooks/get_webhooks.ts @@ -11,12 +11,12 @@ export async function getWebhooks(guildId: string) { const result = (await rest.runMethod( "get", - endpoints.GUILD_WEBHOOKS(guildId) + endpoints.GUILD_WEBHOOKS(guildId), )) as DiscordWebhook[]; return new Collection( result .map((webhook) => snakeKeysToCamelCase(webhook)) - .map((webhook) => [webhook.id, webhook]) + .map((webhook) => [webhook.id, webhook]), ); }