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
+8 -4
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
if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000;
const result = await rest.runMethod<Channel>("post", endpoints.GUILD_CHANNELS(guildId), {
...snakelize<DiscordCreateGuildChannel>(options ?? {}),
permission_overwrites: options?.permissionOverwrites?.map((perm) => ({
const result = await rest.runMethod<Channel>(
"post",
endpoints.GUILD_CHANNELS(guildId),
snakelize<DiscordCreateGuildChannel>({
...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);
+8 -9
View File
@@ -72,10 +72,12 @@ export async function editChannel(channelId: bigint, options: ModifyChannel | Mo
}
}
const payload = {
...snakelize<Record<string, unknown>>(options),
// deno-lint-ignore camelcase
permission_overwrites: hasOwnProperty<ModifyChannel>(options, "permissionOverwrites")
const result = await rest.runMethod<Channel>(
"patch",
endpoints.CHANNEL_BASE(channelId),
snakelize({
...options,
permissionOverwrites: hasOwnProperty<ModifyChannel>(options, "permissionOverwrites")
? options.permissionOverwrites?.map((overwrite) => {
return {
...overwrite,
@@ -84,12 +86,9 @@ export async function editChannel(channelId: bigint, options: ModifyChannel | Mo
};
})
: undefined,
};
const result = await rest.runMethod<Channel>("patch", endpoints.CHANNEL_BASE(channelId), {
...payload,
reason,
});
})
);
return await structures.createDiscordenoChannel(result);
}
+2 -1
View File
@@ -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<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) {
await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
return await rest.runMethod<Emoji>("patch", endpoints.GUILD_EMOJI(guildId, id), {
name: options.name,
roles: options.roles,
});
return await rest.runMethod<Emoji>("patch", endpoints.GUILD_EMOJI(guildId, id), options);
}
+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 { 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<Guild>("post", endpoints.GUILDS, options);
const result = await rest.runMethod<Guild>("post", endpoints.GUILDS, snakelize(options));
const guild = await structures.createDiscordenoGuild(result, 0);
// 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 { 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<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);
return structures.createDiscordenoGuild(
@@ -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
+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 { 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<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 { 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<StatusUpdate, "afk" | "since">) {
@@ -12,7 +13,7 @@ export function editBotStatus(data: Omit<StatusUpdate, "afk" | "since">) {
d: {
since: null,
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 { 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<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 { Webhook } from "../../types/webhooks/webhook.ts";
import { endpoints } from "../../util/constants.ts";
import { snakelize } from "../../util/utils.ts";
/** Edit a webhook. Returns the updated webhook object on success. */
export async function editWebhookWithToken(
@@ -9,5 +10,5 @@ export async function editWebhookWithToken(
webhookToken: string,
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 type { ExecuteWebhook } from "../../types/webhooks/execute_webhook.ts";
import { endpoints } from "../../util/constants.ts";
import { snakelize } from "../../util/utils.ts";
/** Send a webhook with webhook Id and webhook token */
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}${
options.threadId ? `&thread_id=${options.threadId}` : ""
}`,
{
...options,
allowed_mentions: options.allowedMentions,
avatar_url: options.avatarUrl,
}
snakelize(options)
);
if (!options.wait) return;