From dc9615bb8ad2ae8b264d01d0da38934379322b64 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Tue, 13 Apr 2021 13:28:31 -0400 Subject: [PATCH 1/7] feat: slash dm (#846) * chore: cleanup comments * feat: slash in dm and better event args Co-authored-by: Skillz4Killz --- .../interactions/INTERACTION_CREATE.ts | 25 +- src/types/discordeno/eventHandlers.ts | 16 +- src/ws/tell_cluster_to_identify.ts | 1 - tests/channels/create_channel.ts | 2 - tests/ws/start_bot.ts | 234 ------------------ 5 files changed, 25 insertions(+), 253 deletions(-) diff --git a/src/handlers/interactions/INTERACTION_CREATE.ts b/src/handlers/interactions/INTERACTION_CREATE.ts index 510f46a60..78c292cd4 100644 --- a/src/handlers/interactions/INTERACTION_CREATE.ts +++ b/src/handlers/interactions/INTERACTION_CREATE.ts @@ -1,4 +1,3 @@ -// TODO: DM support idk need to discuss how we solve this import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; import { structures } from "../../structures/mod.ts"; @@ -8,16 +7,18 @@ import { DiscordInteraction } from "../../types/interactions/interaction.ts"; export async function handleInteractionCreate(data: DiscordGatewayPayload) { const payload = data.d as DiscordInteraction; - const discordenoMember = await structures.createDiscordenoMember( - payload.member as DiscordGuildMemberWithUser, - payload.guild_id ?? "", - ); - await cacheHandlers.set("members", discordenoMember.id, discordenoMember); + const discordenoMember = payload.guild_id + ? await structures.createDiscordenoMember( + payload.member as DiscordGuildMemberWithUser, + payload.guild_id, + ) + : undefined; + if (discordenoMember) { + await cacheHandlers.set("members", discordenoMember.id, discordenoMember); + eventHandlers.interactionGuildCreate?.(payload, discordenoMember); + } else { + eventHandlers.interactionDMCreate?.(payload); + } - eventHandlers.interactionCreate?.( - { - ...payload, - member: discordenoMember, - }, - ); + eventHandlers.interactionCreate?.(payload, discordenoMember); } diff --git a/src/types/discordeno/eventHandlers.ts b/src/types/discordeno/eventHandlers.ts index 4933c4e92..f60fda6e2 100644 --- a/src/types/discordeno/eventHandlers.ts +++ b/src/types/discordeno/eventHandlers.ts @@ -121,11 +121,19 @@ export interface EventHandlers { member: DiscordenoMember, oldMember?: DiscordenoMember, ) => unknown; - // TODO: remove this? - //heartbeat?: () => unknown; - /** Sent when a user in a guild uses a Slash Command. */ + /** Sent when a user uses a Slash Command. */ interactionCreate?: ( - data: Omit & { member: DiscordenoMember }, + data: Omit, + member?: DiscordenoMember, + ) => unknown; + /** Sent when a user uses a Slash Command in a guild. */ + interactionGuildCreate?: ( + data: Omit, + member: DiscordenoMember, + ) => unknown; + /** Sent when a user uses a Slash Command in a dm. */ + interactionDMCreate?: ( + data: Omit, ) => unknown; /** Sent when a message is created. */ messageCreate?: (message: DiscordenoMessage) => unknown; diff --git a/src/ws/tell_cluster_to_identify.ts b/src/ws/tell_cluster_to_identify.ts index d1c4e25e7..ec212fcc7 100644 --- a/src/ws/tell_cluster_to_identify.ts +++ b/src/ws/tell_cluster_to_identify.ts @@ -8,7 +8,6 @@ export async function tellClusterToIdentify( ) { // When resharding this may exist already const oldShard = ws.shards.get(shardId); - // TODO: Use workers await ws.identify(shardId, ws.maxShards); if (oldShard) { diff --git a/tests/channels/create_channel.ts b/tests/channels/create_channel.ts index 275fb8d6d..02c952fb4 100644 --- a/tests/channels/create_channel.ts +++ b/tests/channels/create_channel.ts @@ -173,5 +173,3 @@ Deno.test({ // TODO: Need to validate tests for these options // /** Sorting position of the channel */ // position?: number; -// /** The channel's permission overwrites */ -// permissionOverwrites?: Overwrite[]; diff --git a/tests/ws/start_bot.ts b/tests/ws/start_bot.ts index 1790f81e7..d0e77c55a 100644 --- a/tests/ws/start_bot.ts +++ b/tests/ws/start_bot.ts @@ -58,237 +58,3 @@ Deno.test({ }, ...defaultTestOptions, }); - -// // Role - -// Deno.test({ -// name: "[role] create a role in a guild", -// async fn() { -// if (!tempData.guildId) { -// throw new Error("guildId not present in temporary data"); -// } - -// const name = "Discordeno Test"; -// const role = await createRole(tempData.guildId, { -// name, -// }); - -// // Assertions -// assertExists(role); -// assertEquals(role.name, name); - -// tempData.roleId = role.id; -// }, -// ...defaultTestOptions, -// }); - -// Deno.test({ -// name: "[role] edit a role in a guild", -// async fn() { -// const name = "Discordeno Test Edited"; -// const color = 4320244; -// const role = await editRole(tempData.guildId, tempData.roleId, { -// name, -// color, -// hoist: false, -// mentionable: false, -// }) as Role; - -// // Assertions -// assertExists(role); -// assertEquals(role.name, name); -// assertEquals(role.color, color); -// assertEquals(role.hoist, false); -// assertEquals(role.mentionable, false); - -// tempData.roleId = role.id; -// }, -// ...defaultTestOptions, -// }); - -// // Channel - -// Deno.test({ -// name: "[channel] create a channel in a guild", -// async fn() { -// const channel = await createChannel(tempData.guildId, "test"); - -// // Assertions -// assertExists(channel); - -// tempData.channelId = channel.id; -// }, -// ...defaultTestOptions, -// }); - -// Deno.test({ -// name: "[channel] get a channel in a guild", -// async fn() { -// const channel = await getChannel(tempData.channelId); - -// // Assertions -// assertExists(channel); -// assertEquals(channel.id, tempData.channelId); -// }, -// ...defaultTestOptions, -// }); - -// Deno.test({ -// name: "[channel] edit a channel in a guild", -// async fn() { -// const channel = await editChannel(tempData.channelId, { -// name: "discordeno-test-edited", -// overwrites: [ -// { -// id: tempData.roleId, -// type: OverwriteType.ROLE, -// allow: ["VIEW_CHANNEL", "SEND_MESSAGES"], -// deny: ["USE_EXTERNAL_EMOJIS"], -// }, -// ], -// }) as Channel; - -// // Wait 5s for CHANNEL_UPDATE to fire -// await delay(3000); - -// // Assertions -// assertExists(channel); -// assertEquals(channel.name, "discordeno-test-edited"); -// }, -// ...defaultTestOptions, -// }); - -// Deno.test({ -// name: "[channel] channel overwrite has permission", -// fn() { -// const channel = cache.channels.get(tempData.channelId); -// if (!channel) throw new Error("Channel not found"); -// if (!channel.permissionOverwrites) { -// throw new Error("permissionOverwrites not found"); -// } - -// const hasPerm = channelOverwriteHasPermission( -// tempData.guildId, -// tempData.roleId, -// channel.permissionOverwrites, -// ["VIEW_CHANNEL", "SEND_MESSAGES"], -// ); -// const missingPerm = channelOverwriteHasPermission( -// tempData.guildId, -// tempData.roleId, -// channel.permissionOverwrites, -// ["USE_EXTERNAL_EMOJIS"], -// ); - -// assertEquals(hasPerm, true); -// assertEquals(missingPerm, false); -// }, -// ...defaultTestOptions, -// }); - -// // Message - -// Deno.test({ -// name: "[message] send a message in a text channel", -// async fn() { -// const message = await sendMessage(tempData.channelId, { -// embed: { -// title: "Discordeno Test", -// }, -// }); - -// // Assertions -// assertExists(message); -// assertEquals(message.embeds[0].title, "Discordeno Test"); - -// tempData.messageId = message.id; -// }, -// ...defaultTestOptions, -// }); - -// Deno.test({ -// name: "[message] get a message in a guild", -// async fn() { -// const message = await getMessage(tempData.channelId, tempData.messageId); - -// // Assertions -// assertExists(message); -// assertEquals(message.embeds[0].title, "Discordeno Test"); -// }, -// ...defaultTestOptions, -// }); - -// Deno.test({ -// name: "[message] pin a message in a channel", -// async fn() { -// await pinMessage(tempData.channelId, tempData.messageId); -// }, -// ...defaultTestOptions, -// }); - -// Deno.test({ -// name: "[message] get pinned message in a channel", -// async fn() { -// const [msg] = await getPins(tempData.channelId); - -// // Assertions -// assertExists(msg); -// assertEquals(msg.id, tempData.messageId); -// assertEquals(msg.pinned, true); -// }, -// ...defaultTestOptions, -// }); - -// Deno.test({ -// name: "[message] unpin a message", -// async fn() { -// await unpinMessage(tempData.channelId, tempData.messageId); -// }, -// ...defaultTestOptions, -// }); - -// Deno.test({ -// name: "[message] add a reaction to a message", -// async fn() { -// // TODO: add tests for a guild emoji ― <:name:id> - -// await addReaction(tempData.channelId, tempData.messageId, "👍"); -// }, -// ...defaultTestOptions, -// }); - -// // TODO(ayntee): add unit tests for getReactions() - -// Deno.test({ -// name: "[message] remove a reaction to a message", -// async fn() { -// await removeReaction(tempData.channelId, tempData.messageId, "👍"); -// }, -// ...defaultTestOptions, -// }); - -// // Cleanup - -// Deno.test({ -// name: "[message] delete a message by channel Id", -// async fn() { -// await deleteMessage(tempData.channelId, tempData.messageId); -// }, -// ...defaultTestOptions, -// }); - -// Deno.test({ -// name: "[channel] delete a channel in a guild", -// async fn() { -// await deleteChannel(tempData.guildId, tempData.channelId); -// }, -// ...defaultTestOptions, -// }); - -// Deno.test({ -// name: "[role] delete a role in a guild", -// async fn() { -// await deleteRole(tempData.guildId, tempData.roleId); -// }, -// ...defaultTestOptions, -// }); 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 2/7] 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 3/7] --- 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 4/7] 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]), ); } From c85b76cd185efb35936d958ed30df57c3cc77d2d Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Tue, 13 Apr 2021 14:36:23 -0400 Subject: [PATCH 5/7] fix: remove extra loops (#848) * fix: remove extra loops Co-authored-by: Skillz4Killz --- src/helpers/channels/get_channel_webhooks.ts | 7 ++++--- src/helpers/invites/get_channel_invites.ts | 4 +--- src/helpers/invites/get_invites.ts | 4 +--- src/helpers/roles/get_roles.ts | 4 +--- src/helpers/webhooks/get_webhooks.ts | 7 ++++--- 5 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/helpers/channels/get_channel_webhooks.ts b/src/helpers/channels/get_channel_webhooks.ts index 78bf1caca..bdea2baf5 100644 --- a/src/helpers/channels/get_channel_webhooks.ts +++ b/src/helpers/channels/get_channel_webhooks.ts @@ -15,8 +15,9 @@ export async function getChannelWebhooks(channelId: string) { )) as DiscordWebhook[]; return new Collection( - result - .map((webhook) => snakeKeysToCamelCase(webhook)) - .map((webhook) => [webhook.id, webhook]), + result.map((webhook) => [ + webhook.id, + snakeKeysToCamelCase(webhook), + ]), ); } diff --git a/src/helpers/invites/get_channel_invites.ts b/src/helpers/invites/get_channel_invites.ts index b75b831f9..f1a489693 100644 --- a/src/helpers/invites/get_channel_invites.ts +++ b/src/helpers/invites/get_channel_invites.ts @@ -15,8 +15,6 @@ export async function getChannelInvites(channelId: string) { )) as DiscordInvite[]; return new Collection( - result - .map((invite) => snakeKeysToCamelCase(invite)) - .map((invite) => [invite.code, invite]), + result.map((invite) => [invite.code, snakeKeysToCamelCase(invite)]), ); } diff --git a/src/helpers/invites/get_invites.ts b/src/helpers/invites/get_invites.ts index e8aa0d594..778a243d6 100644 --- a/src/helpers/invites/get_invites.ts +++ b/src/helpers/invites/get_invites.ts @@ -15,8 +15,6 @@ export async function getInvites(guildId: string) { )) as DiscordInvite[]; return new Collection( - result - .map((invite) => snakeKeysToCamelCase(invite)) - .map((invite) => [invite.code, invite]), + result.map((invite) => [invite.code, snakeKeysToCamelCase(invite)]), ); } diff --git a/src/helpers/roles/get_roles.ts b/src/helpers/roles/get_roles.ts index 2edeb648e..29b08998b 100644 --- a/src/helpers/roles/get_roles.ts +++ b/src/helpers/roles/get_roles.ts @@ -18,8 +18,6 @@ export async function getRoles(guildId: string) { )) as DiscordRole[]; return new Collection( - result - .map((role) => snakeKeysToCamelCase(role)) - .map((role) => [role.id, role]), + result.map((role) => [role.id, snakeKeysToCamelCase(role)]), ); } diff --git a/src/helpers/webhooks/get_webhooks.ts b/src/helpers/webhooks/get_webhooks.ts index ff8cbf224..32fe734da 100644 --- a/src/helpers/webhooks/get_webhooks.ts +++ b/src/helpers/webhooks/get_webhooks.ts @@ -15,8 +15,9 @@ export async function getWebhooks(guildId: string) { )) as DiscordWebhook[]; return new Collection( - result - .map((webhook) => snakeKeysToCamelCase(webhook)) - .map((webhook) => [webhook.id, webhook]), + result.map((webhook) => [ + webhook.id, + snakeKeysToCamelCase(webhook), + ]), ); } From c358d4f932235df28c92690fd6ad8f84808184d6 Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Tue, 13 Apr 2021 21:58:57 +0200 Subject: [PATCH 6/7] use proper invite types --- src/helpers/invites/create_invite.ts | 4 ++-- src/types/invites/create_channel_invite.ts | 18 ++++++++++++++++++ src/types/invites/invite_target_types.ts | 5 +++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/types/invites/create_channel_invite.ts create mode 100644 src/types/invites/invite_target_types.ts diff --git a/src/helpers/invites/create_invite.ts b/src/helpers/invites/create_invite.ts index 3aae2e41b..09e13633d 100644 --- a/src/helpers/invites/create_invite.ts +++ b/src/helpers/invites/create_invite.ts @@ -1,12 +1,12 @@ import { rest } from "../../rest/rest.ts"; -import { InviteCreate } from "../../types/invites/invite_create.ts"; +import { CreateChannelInvite } from "../../types/invites/create_channel_invite.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; /** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */ export async function createInvite( channelId: string, - options: InviteCreate, + options: CreateChannelInvite, ) { await requireBotChannelPermissions(channelId, ["CREATE_INSTANT_INVITE"]); diff --git a/src/types/invites/create_channel_invite.ts b/src/types/invites/create_channel_invite.ts new file mode 100644 index 000000000..50ac50762 --- /dev/null +++ b/src/types/invites/create_channel_invite.ts @@ -0,0 +1,18 @@ +import { DiscordInviteTargetTypes } from "./invite_target_types.ts"; + +export interface CreateChannelInvite { + /** Durationi of invite in seconds before expiry, or 0 for never. Between 0 and 604800 (7 days). Default: 86400 (24 hours) */ + maxAge?: number; + /** Max number of users or 0 for unlimited. Between 0 and 100. Default: 0 */ + maxUses?: number; + /** Whether this invite only grants temporary membership. Default: false */ + temporary?: boolean; + /** If true, don't try to reuse simmilar invite (useful for creating many unique one time use invites). Default: false */ + unique?: boolean; + /** The type of target for this voice channel invite */ + targetType?: DiscordInviteTargetTypes; + /** The id of the user whose stream to display for this invite, required if `target_type` is 1, the user must be streaming in the channel */ + targetUserId?: string; + /** The id of the embedded application to open for this invite, required if `target_type` is 2, the application must have the `EMBEDDED` flag */ + targetApplicationId?: string; +} diff --git a/src/types/invites/invite_target_types.ts b/src/types/invites/invite_target_types.ts new file mode 100644 index 000000000..b645e9b96 --- /dev/null +++ b/src/types/invites/invite_target_types.ts @@ -0,0 +1,5 @@ +/** https://discord.com/developers/docs/resources/invite#invite-object-invite-target-types */ +export enum DiscordInviteTargetTypes { + Stream = 1, + EmbeddedApplication, +} From f31216d0a4a8e9700307677f449f128b661422b5 Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Tue, 13 Apr 2021 21:59:13 +0200 Subject: [PATCH 7/7] add DISCORDENO_VERSION --- src/util/constants.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/util/constants.ts b/src/util/constants.ts index 28e0125c4..adaa4b86e 100644 --- a/src/util/constants.ts +++ b/src/util/constants.ts @@ -7,9 +7,12 @@ export const API_VERSION = 8; /** https://discord.com/developers/docs/topics/gateway#gateways-gateway-versions */ export const GATEWAY_VERSION = 8; +/** https://github.com/discordeno/discordeno/releases */ +export const DISCORDENO_VERSION = 11; + /** https://discord.com/developers/docs/reference#user-agent */ export const USER_AGENT = - "DiscordBot (https://github.com/discordeno/discordeno, v10)"; + `DiscordBot (https://github.com/discordeno/discordeno, v${DISCORDENO_VERSION})`; /** https://discord.com/developers/docs/reference#image-formatting-image-base-url */ export const IMAGE_BASE_URL = "https://cdn.discordapp.com";