From 83b7a708e71818eb52827a0179ca16162057d4a5 Mon Sep 17 00:00:00 2001 From: ITOH Date: Mon, 31 May 2021 19:48:13 +0200 Subject: [PATCH] fix: snakelize request body --- src/helpers/channels/create_channel.ts | 24 +++++++------ src/helpers/channels/edit_channel.ts | 35 +++++++++---------- src/helpers/channels/swap_channels.ts | 3 +- src/helpers/emojis/edit_emoji.ts | 5 +-- src/helpers/guilds/create_guild.ts | 3 +- src/helpers/guilds/edit_guild.ts | 4 +-- .../commands/edit_slash_response.ts | 7 +++- src/helpers/invites/create_invite.ts | 3 +- src/helpers/misc/edit_bot_status.ts | 3 +- .../templates/create_guild_template.ts | 3 +- .../webhooks/edit_webhook_with_token.ts | 3 +- src/helpers/webhooks/send_webhook.ts | 7 ++-- 12 files changed, 54 insertions(+), 46 deletions(-) diff --git a/src/helpers/channels/create_channel.ts b/src/helpers/channels/create_channel.ts index 8d6e251c3..0561f95c1 100644 --- a/src/helpers/channels/create_channel.ts +++ b/src/helpers/channels/create_channel.ts @@ -17,16 +17,20 @@ export async function createChannel(guildId: bigint, options?: CreateGuildChanne // BITRATES ARE IN THOUSANDS SO IF USER PROVIDES 32 WE CONVERT TO 32000 if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000; - const result = await rest.runMethod("post", endpoints.GUILD_CHANNELS(guildId), { - ...snakelize(options ?? {}), - permission_overwrites: options?.permissionOverwrites?.map((perm) => ({ - ...perm, - allow: calculateBits(perm.allow), - deny: calculateBits(perm.deny), - })), - type: options?.type || DiscordChannelTypes.GuildText, - reason, - }); + const result = await rest.runMethod( + "post", + endpoints.GUILD_CHANNELS(guildId), + snakelize({ + ...options, + permissionOverwrites: options?.permissionOverwrites?.map((perm) => ({ + ...perm, + allow: calculateBits(perm.allow), + deny: calculateBits(perm.deny), + })), + type: options?.type || DiscordChannelTypes.GuildText, + reason, + }) + ); const discordenoChannel = await structures.createDiscordenoChannel(result); await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel); diff --git a/src/helpers/channels/edit_channel.ts b/src/helpers/channels/edit_channel.ts index 75e59c266..0d61d4574 100644 --- a/src/helpers/channels/edit_channel.ts +++ b/src/helpers/channels/edit_channel.ts @@ -72,24 +72,23 @@ export async function editChannel(channelId: bigint, options: ModifyChannel | Mo } } - const payload = { - ...snakelize>(options), - // deno-lint-ignore camelcase - permission_overwrites: hasOwnProperty(options, "permissionOverwrites") - ? options.permissionOverwrites?.map((overwrite) => { - return { - ...overwrite, - allow: calculateBits(overwrite.allow), - deny: calculateBits(overwrite.deny), - }; - }) - : undefined, - }; - - const result = await rest.runMethod("patch", endpoints.CHANNEL_BASE(channelId), { - ...payload, - reason, - }); + const result = await rest.runMethod( + "patch", + endpoints.CHANNEL_BASE(channelId), + snakelize({ + ...options, + permissionOverwrites: hasOwnProperty(options, "permissionOverwrites") + ? options.permissionOverwrites?.map((overwrite) => { + return { + ...overwrite, + allow: calculateBits(overwrite.allow), + deny: calculateBits(overwrite.deny), + }; + }) + : undefined, + reason, + }) + ); return await structures.createDiscordenoChannel(result); } diff --git a/src/helpers/channels/swap_channels.ts b/src/helpers/channels/swap_channels.ts index 92cd14462..cc515e159 100644 --- a/src/helpers/channels/swap_channels.ts +++ b/src/helpers/channels/swap_channels.ts @@ -1,6 +1,7 @@ import { rest } from "../../rest/rest.ts"; import type { ModifyGuildChannelPositions } from "../../types/guilds/modify_guild_channel_position.ts"; import { endpoints } from "../../util/constants.ts"; +import { snakelize } from "../../util/utils.ts"; /** Modify the positions of channels on the guild. Requires MANAGE_CHANNELS permisison. */ export async function swapChannels(guildId: bigint, channelPositions: ModifyGuildChannelPositions[]) { @@ -8,5 +9,5 @@ export async function swapChannels(guildId: bigint, channelPositions: ModifyGuil throw "You must provide at least two channels to be swapped."; } - return await rest.runMethod("patch", endpoints.GUILD_CHANNELS(guildId), channelPositions); + return await rest.runMethod("patch", endpoints.GUILD_CHANNELS(guildId), snakelize(channelPositions)); } diff --git a/src/helpers/emojis/edit_emoji.ts b/src/helpers/emojis/edit_emoji.ts index 669efeed5..c1917ebef 100644 --- a/src/helpers/emojis/edit_emoji.ts +++ b/src/helpers/emojis/edit_emoji.ts @@ -8,8 +8,5 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts"; export async function editEmoji(guildId: bigint, id: bigint, options: ModifyGuildEmoji) { await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]); - return await rest.runMethod("patch", endpoints.GUILD_EMOJI(guildId, id), { - name: options.name, - roles: options.roles, - }); + return await rest.runMethod("patch", endpoints.GUILD_EMOJI(guildId, id), options); } diff --git a/src/helpers/guilds/create_guild.ts b/src/helpers/guilds/create_guild.ts index dc065d839..38bd03165 100644 --- a/src/helpers/guilds/create_guild.ts +++ b/src/helpers/guilds/create_guild.ts @@ -5,11 +5,12 @@ import { structures } from "../../structures/mod.ts"; import type { CreateGuild } from "../../types/guilds/create_guild.ts"; import type { Guild } from "../../types/guilds/guild.ts"; import { endpoints } from "../../util/constants.ts"; +import { snakelize } from "../../util/utils.ts"; import { getMember } from "../members/get_member.ts"; /** Create a new guild. Returns a guild object on success. Fires a Guild Create Gateway event. This endpoint can be used only by bots in less than 10 guilds. */ export async function createGuild(options: CreateGuild) { - const result = await rest.runMethod("post", endpoints.GUILDS, options); + const result = await rest.runMethod("post", endpoints.GUILDS, snakelize(options)); const guild = await structures.createDiscordenoGuild(result, 0); // MANUALLY CACHE THE GUILD diff --git a/src/helpers/guilds/edit_guild.ts b/src/helpers/guilds/edit_guild.ts index 145efbd69..760e387ce 100644 --- a/src/helpers/guilds/edit_guild.ts +++ b/src/helpers/guilds/edit_guild.ts @@ -5,7 +5,7 @@ import type { Guild } from "../../types/guilds/guild.ts"; import type { ModifyGuild } from "../../types/guilds/modify_guild.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; -import { urlToBase64 } from "../../util/utils.ts"; +import { snakelize, urlToBase64 } from "../../util/utils.ts"; import { ws } from "../../ws/ws.ts"; /** Modify a guilds settings. Requires the MANAGE_GUILD permission. */ @@ -24,7 +24,7 @@ export async function editGuild(guildId: bigint, options: ModifyGuild) { options.splash = await urlToBase64(options.splash); } - const result = await rest.runMethod("patch", endpoints.GUILDS_BASE(guildId), options); + const result = await rest.runMethod("patch", endpoints.GUILDS_BASE(guildId), snakelize(options)); const cached = await cacheHandlers.get("guilds", guildId); return structures.createDiscordenoGuild( diff --git a/src/helpers/interactions/commands/edit_slash_response.ts b/src/helpers/interactions/commands/edit_slash_response.ts index 62920889c..22b058875 100644 --- a/src/helpers/interactions/commands/edit_slash_response.ts +++ b/src/helpers/interactions/commands/edit_slash_response.ts @@ -5,6 +5,7 @@ import type { DiscordenoEditWebhookMessage } from "../../../types/discordeno/edi import { Errors } from "../../../types/discordeno/errors.ts"; import { DiscordAllowedMentionsTypes } from "../../../types/messages/allowed_mentions_types.ts"; import { endpoints } from "../../../util/constants.ts"; +import { snakelize, validateComponents } from "../../../util/utils.ts"; /** To edit your response to a slash command. If a messageId is not provided it will default to editing the original response. */ export async function editSlashResponse(token: string, options: DiscordenoEditWebhookMessage) { @@ -12,6 +13,10 @@ export async function editSlashResponse(token: string, options: DiscordenoEditWe throw Error(Errors.MESSAGE_MAX_LENGTH); } + if (options.components?.length) { + validateComponents(options.components); + } + if (options.embeds && options.embeds.length > 10) { options.embeds.splice(10); } @@ -43,7 +48,7 @@ export async function editSlashResponse(token: string, options: DiscordenoEditWe options.messageId ? endpoints.WEBHOOK_MESSAGE(applicationId, token, options.messageId) : endpoints.INTERACTION_ORIGINAL_ID_TOKEN(applicationId, token), - options + snakelize(options) ); // If the original message was edited, this will not return a message diff --git a/src/helpers/invites/create_invite.ts b/src/helpers/invites/create_invite.ts index c3354a3fe..9060a0822 100644 --- a/src/helpers/invites/create_invite.ts +++ b/src/helpers/invites/create_invite.ts @@ -4,6 +4,7 @@ import type { InviteMetadata } from "../../types/invites/invite_metadata.ts"; import { Errors } from "../../types/discordeno/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; +import { snakelize } from "../../util/utils.ts"; /** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */ export async function createInvite(channelId: bigint, options: CreateChannelInvite = {}) { @@ -16,5 +17,5 @@ export async function createInvite(channelId: bigint, options: CreateChannelInvi throw new Error(Errors.INVITE_MAX_USES_INVALID); } - return await rest.runMethod("post", endpoints.CHANNEL_INVITES(channelId), options); + return await rest.runMethod("post", endpoints.CHANNEL_INVITES(channelId), snakelize(options)); } diff --git a/src/helpers/misc/edit_bot_status.ts b/src/helpers/misc/edit_bot_status.ts index 5c05cabce..0b2664085 100644 --- a/src/helpers/misc/edit_bot_status.ts +++ b/src/helpers/misc/edit_bot_status.ts @@ -1,6 +1,7 @@ import { eventHandlers } from "../../bot.ts"; import { DiscordGatewayOpcodes } from "../../types/codes/gateway_opcodes.ts"; import type { StatusUpdate } from "../../types/gateway/status_update.ts"; +import { snakelize } from "../../util/utils.ts"; import { ws } from "../../ws/ws.ts"; export function editBotStatus(data: Omit) { @@ -12,7 +13,7 @@ export function editBotStatus(data: Omit) { d: { since: null, afk: false, - ...data, + ...snakelize>(data), }, }); }); diff --git a/src/helpers/templates/create_guild_template.ts b/src/helpers/templates/create_guild_template.ts index 7dc10961c..bc09064b8 100644 --- a/src/helpers/templates/create_guild_template.ts +++ b/src/helpers/templates/create_guild_template.ts @@ -2,6 +2,7 @@ import { rest } from "../../rest/rest.ts"; import type { Template } from "../../types/templates/template.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import { snakelize } from "../../util/utils.ts"; /** * Creates a template for the guild. @@ -20,5 +21,5 @@ export async function createGuildTemplate(guildId: bigint, data: Template) { throw new Error("The description can only be in between 0-120 characters."); } - return await rest.runMethod