fix: snakelize request body

This commit is contained in:
ITOH
2021-05-31 19:48:13 +02:00
parent 220ea9ebb8
commit 83b7a708e7
12 changed files with 54 additions and 46 deletions
+14 -10
View File
@@ -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 // BITRATES ARE IN THOUSANDS SO IF USER PROVIDES 32 WE CONVERT TO 32000
if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000; if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000;
const result = await rest.runMethod<Channel>("post", endpoints.GUILD_CHANNELS(guildId), { const result = await rest.runMethod<Channel>(
...snakelize<DiscordCreateGuildChannel>(options ?? {}), "post",
permission_overwrites: options?.permissionOverwrites?.map((perm) => ({ endpoints.GUILD_CHANNELS(guildId),
...perm, snakelize<DiscordCreateGuildChannel>({
allow: calculateBits(perm.allow), ...options,
deny: calculateBits(perm.deny), permissionOverwrites: options?.permissionOverwrites?.map((perm) => ({
})), ...perm,
type: options?.type || DiscordChannelTypes.GuildText, allow: calculateBits(perm.allow),
reason, deny: calculateBits(perm.deny),
}); })),
type: options?.type || DiscordChannelTypes.GuildText,
reason,
})
);
const discordenoChannel = await structures.createDiscordenoChannel(result); const discordenoChannel = await structures.createDiscordenoChannel(result);
await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel); await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel);
+17 -18
View File
@@ -72,24 +72,23 @@ export async function editChannel(channelId: bigint, options: ModifyChannel | Mo
} }
} }
const payload = { const result = await rest.runMethod<Channel>(
...snakelize<Record<string, unknown>>(options), "patch",
// deno-lint-ignore camelcase endpoints.CHANNEL_BASE(channelId),
permission_overwrites: hasOwnProperty<ModifyChannel>(options, "permissionOverwrites") snakelize({
? options.permissionOverwrites?.map((overwrite) => { ...options,
return { permissionOverwrites: hasOwnProperty<ModifyChannel>(options, "permissionOverwrites")
...overwrite, ? options.permissionOverwrites?.map((overwrite) => {
allow: calculateBits(overwrite.allow), return {
deny: calculateBits(overwrite.deny), ...overwrite,
}; allow: calculateBits(overwrite.allow),
}) deny: calculateBits(overwrite.deny),
: undefined, };
}; })
: undefined,
const result = await rest.runMethod<Channel>("patch", endpoints.CHANNEL_BASE(channelId), { reason,
...payload, })
reason, );
});
return await structures.createDiscordenoChannel(result); return await structures.createDiscordenoChannel(result);
} }
+2 -1
View File
@@ -1,6 +1,7 @@
import { rest } from "../../rest/rest.ts"; import { rest } from "../../rest/rest.ts";
import type { ModifyGuildChannelPositions } from "../../types/guilds/modify_guild_channel_position.ts"; import type { ModifyGuildChannelPositions } from "../../types/guilds/modify_guild_channel_position.ts";
import { endpoints } from "../../util/constants.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. */ /** Modify the positions of channels on the guild. Requires MANAGE_CHANNELS permisison. */
export async function swapChannels(guildId: bigint, channelPositions: ModifyGuildChannelPositions[]) { 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."; throw "You must provide at least two channels to be swapped.";
} }
return await rest.runMethod<undefined>("patch", endpoints.GUILD_CHANNELS(guildId), channelPositions); return await rest.runMethod<undefined>("patch", endpoints.GUILD_CHANNELS(guildId), snakelize(channelPositions));
} }
+1 -4
View File
@@ -8,8 +8,5 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function editEmoji(guildId: bigint, id: bigint, options: ModifyGuildEmoji) { export async function editEmoji(guildId: bigint, id: bigint, options: ModifyGuildEmoji) {
await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]); await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
return await rest.runMethod<Emoji>("patch", endpoints.GUILD_EMOJI(guildId, id), { return await rest.runMethod<Emoji>("patch", endpoints.GUILD_EMOJI(guildId, id), options);
name: options.name,
roles: options.roles,
});
} }
+2 -1
View File
@@ -5,11 +5,12 @@ import { structures } from "../../structures/mod.ts";
import type { CreateGuild } from "../../types/guilds/create_guild.ts"; import type { CreateGuild } from "../../types/guilds/create_guild.ts";
import type { Guild } from "../../types/guilds/guild.ts"; import type { Guild } from "../../types/guilds/guild.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { snakelize } from "../../util/utils.ts";
import { getMember } from "../members/get_member.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. */ /** 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) { export async function createGuild(options: CreateGuild) {
const result = await rest.runMethod<Guild>("post", endpoints.GUILDS, options); const result = await rest.runMethod<Guild>("post", endpoints.GUILDS, snakelize(options));
const guild = await structures.createDiscordenoGuild(result, 0); const guild = await structures.createDiscordenoGuild(result, 0);
// MANUALLY CACHE THE GUILD // MANUALLY CACHE THE GUILD
+2 -2
View File
@@ -5,7 +5,7 @@ import type { Guild } from "../../types/guilds/guild.ts";
import type { ModifyGuild } from "../../types/guilds/modify_guild.ts"; import type { ModifyGuild } from "../../types/guilds/modify_guild.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.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"; import { ws } from "../../ws/ws.ts";
/** Modify a guilds settings. Requires the MANAGE_GUILD permission. */ /** 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); options.splash = await urlToBase64(options.splash);
} }
const result = await rest.runMethod<Guild>("patch", endpoints.GUILDS_BASE(guildId), options); const result = await rest.runMethod<Guild>("patch", endpoints.GUILDS_BASE(guildId), snakelize(options));
const cached = await cacheHandlers.get("guilds", guildId); const cached = await cacheHandlers.get("guilds", guildId);
return structures.createDiscordenoGuild( return structures.createDiscordenoGuild(
@@ -5,6 +5,7 @@ import type { DiscordenoEditWebhookMessage } from "../../../types/discordeno/edi
import { Errors } from "../../../types/discordeno/errors.ts"; import { Errors } from "../../../types/discordeno/errors.ts";
import { DiscordAllowedMentionsTypes } from "../../../types/messages/allowed_mentions_types.ts"; import { DiscordAllowedMentionsTypes } from "../../../types/messages/allowed_mentions_types.ts";
import { endpoints } from "../../../util/constants.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. */ /** 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) { 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); throw Error(Errors.MESSAGE_MAX_LENGTH);
} }
if (options.components?.length) {
validateComponents(options.components);
}
if (options.embeds && options.embeds.length > 10) { if (options.embeds && options.embeds.length > 10) {
options.embeds.splice(10); options.embeds.splice(10);
} }
@@ -43,7 +48,7 @@ export async function editSlashResponse(token: string, options: DiscordenoEditWe
options.messageId options.messageId
? endpoints.WEBHOOK_MESSAGE(applicationId, token, options.messageId) ? endpoints.WEBHOOK_MESSAGE(applicationId, token, options.messageId)
: endpoints.INTERACTION_ORIGINAL_ID_TOKEN(applicationId, token), : endpoints.INTERACTION_ORIGINAL_ID_TOKEN(applicationId, token),
options snakelize(options)
); );
// If the original message was edited, this will not return a message // If the original message was edited, this will not return a message
+2 -1
View File
@@ -4,6 +4,7 @@ import type { InviteMetadata } from "../../types/invites/invite_metadata.ts";
import { Errors } from "../../types/discordeno/errors.ts"; import { Errors } from "../../types/discordeno/errors.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { snakelize } from "../../util/utils.ts";
/** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */ /** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */
export async function createInvite(channelId: bigint, options: CreateChannelInvite = {}) { 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); throw new Error(Errors.INVITE_MAX_USES_INVALID);
} }
return await rest.runMethod<InviteMetadata>("post", endpoints.CHANNEL_INVITES(channelId), options); return await rest.runMethod<InviteMetadata>("post", endpoints.CHANNEL_INVITES(channelId), snakelize(options));
} }
+2 -1
View File
@@ -1,6 +1,7 @@
import { eventHandlers } from "../../bot.ts"; import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayOpcodes } from "../../types/codes/gateway_opcodes.ts"; import { DiscordGatewayOpcodes } from "../../types/codes/gateway_opcodes.ts";
import type { StatusUpdate } from "../../types/gateway/status_update.ts"; import type { StatusUpdate } from "../../types/gateway/status_update.ts";
import { snakelize } from "../../util/utils.ts";
import { ws } from "../../ws/ws.ts"; import { ws } from "../../ws/ws.ts";
export function editBotStatus(data: Omit<StatusUpdate, "afk" | "since">) { export function editBotStatus(data: Omit<StatusUpdate, "afk" | "since">) {
@@ -12,7 +13,7 @@ export function editBotStatus(data: Omit<StatusUpdate, "afk" | "since">) {
d: { d: {
since: null, since: null,
afk: false, afk: false,
...data, ...snakelize<Omit<StatusUpdate, "afk" | "since">>(data),
}, },
}); });
}); });
@@ -2,6 +2,7 @@ import { rest } from "../../rest/rest.ts";
import type { Template } from "../../types/templates/template.ts"; import type { Template } from "../../types/templates/template.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakelize } from "../../util/utils.ts";
/** /**
* Creates a template for the guild. * 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."); throw new Error("The description can only be in between 0-120 characters.");
} }
return await rest.runMethod<Template>("post", endpoints.GUILD_TEMPLATES(guildId), data); return await rest.runMethod<Template>("post", endpoints.GUILD_TEMPLATES(guildId), snakelize(data));
} }
@@ -2,6 +2,7 @@ import { rest } from "../../rest/rest.ts";
import type { ModifyWebhook } from "../../types/webhooks/modify_webhook.ts"; import type { ModifyWebhook } from "../../types/webhooks/modify_webhook.ts";
import type { Webhook } from "../../types/webhooks/webhook.ts"; import type { Webhook } from "../../types/webhooks/webhook.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { snakelize } from "../../util/utils.ts";
/** Edit a webhook. Returns the updated webhook object on success. */ /** Edit a webhook. Returns the updated webhook object on success. */
export async function editWebhookWithToken( export async function editWebhookWithToken(
@@ -9,5 +10,5 @@ export async function editWebhookWithToken(
webhookToken: string, webhookToken: string,
options: Omit<ModifyWebhook, "channelId"> options: Omit<ModifyWebhook, "channelId">
) { ) {
return await rest.runMethod<Webhook>("patch", endpoints.WEBHOOK(webhookId, webhookToken), options); return await rest.runMethod<Webhook>("patch", endpoints.WEBHOOK(webhookId, webhookToken), snakelize(options));
} }
+2 -5
View File
@@ -5,6 +5,7 @@ import type { Message } from "../../types/messages/message.ts";
import { Errors } from "../../types/discordeno/errors.ts"; import { Errors } from "../../types/discordeno/errors.ts";
import type { ExecuteWebhook } from "../../types/webhooks/execute_webhook.ts"; import type { ExecuteWebhook } from "../../types/webhooks/execute_webhook.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { snakelize } from "../../util/utils.ts";
/** Send a webhook with webhook Id and webhook token */ /** Send a webhook with webhook Id and webhook token */
export async function sendWebhook(webhookId: bigint, webhookToken: string, options: ExecuteWebhook) { export async function sendWebhook(webhookId: bigint, webhookToken: string, options: ExecuteWebhook) {
@@ -47,11 +48,7 @@ export async function sendWebhook(webhookId: bigint, webhookToken: string, optio
`${endpoints.WEBHOOK(webhookId, webhookToken)}?wait=${options.wait ?? false}${ `${endpoints.WEBHOOK(webhookId, webhookToken)}?wait=${options.wait ?? false}${
options.threadId ? `&thread_id=${options.threadId}` : "" options.threadId ? `&thread_id=${options.threadId}` : ""
}`, }`,
{ snakelize(options)
...options,
allowed_mentions: options.allowedMentions,
avatar_url: options.avatarUrl,
}
); );
if (!options.wait) return; if (!options.wait) return;