fix: errors (#2496)

* fix: errors

* fix: err can be undefined

* tests: fix sticker tests

* fix: create channel require name

* fix: sticker helpers and tests

* tests: delete channel after tested guild stickers
This commit is contained in:
LTS20050703
2022-10-01 23:14:59 +07:00
committed by GitHub
parent e410da016e
commit 39a806ff9c
11 changed files with 19 additions and 14 deletions

View File

@@ -24,7 +24,7 @@ import { BigString, ChannelTypes } from "../../types/shared.ts";
*
* @see {@link https://discord.com/developers/docs/resources/guild#create-guild-channel}
*/
export async function createChannel(bot: Bot, guildId: BigString, options?: CreateGuildChannel): Promise<Channel> {
export async function createChannel(bot: Bot, guildId: BigString, options: CreateGuildChannel): Promise<Channel> {
// BITRATE IS IN THOUSANDS SO IF USER PROVIDES 32 WE CONVERT TO 32000
if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000;

View File

@@ -22,6 +22,10 @@ export async function createGuildSticker(
guildId: bigint,
options: CreateGuildStickerOptions,
): Promise<Sticker> {
if (options.file && !options.file.startsWith("data:image/")) {
options.file = await bot.utils.urlToBase64(options.file);
}
const result = await bot.rest.runMethod(bot.rest, "POST", bot.constants.routes.GUILD_STICKERS(guildId), {
name: options.name,
description: options.description,
@@ -40,5 +44,5 @@ export interface CreateGuildStickerOptions extends WithReason {
/** Autocomplete/suggestion tags for the sticker (max 200 characters) */
tags: string;
/** The sticker file to upload, must be a PNG, APNG, or Lottie JSON file, max 500 KB */
file: FileContent;
file: string;
}

View File

@@ -41,7 +41,7 @@ export async function runMethod<T = any>(
if (!result.ok) {
const err = await result.json().catch(() => {});
// Legacy Handling to not break old code or when body is missing
if (!err.body) throw new Error(`Error: ${err.message ?? result.statusText}`);
if (!err?.body) throw new Error(`Error: ${err.message ?? result.statusText}`);
throw rest.convertRestError(errorStack, err);
}

View File

@@ -3,7 +3,7 @@ import { CACHED_COMMUNITY_GUILD_ID } from "../utils.ts";
Deno.test("[Misc] Rate Limit Test", async () => {
const bot = loadBot();
const channel = await bot.helpers.createChannel(CACHED_COMMUNITY_GUILD_ID);
const channel = await bot.helpers.createChannel(CACHED_COMMUNITY_GUILD_ID, { name: "test" });
await Promise.all(Array(10).map(() => bot.helpers.sendMessage(channel.id, { content: "Rate Limit Test" })));
await bot.helpers.deleteChannel(channel.id);
});

View File

@@ -1,4 +1,4 @@
import { createBot, createRestManager, runMethod } from "../mod.ts";
import { createBot, createRestManager } from "../mod.ts";
import { dotenv } from "./deps.ts";
dotenv({ export: true, path: `${Deno.cwd()}/.env` });

View File

@@ -9,14 +9,14 @@ Deno.test("[stickers] Create guild sticker", async () => {
name: "sticker name",
description: "sticker description",
tags: "sticker tags",
file: { blob: new Blob(), name: "test.png" },
file: "https://cdn.discordapp.com/emojis/785403373817823272.png",
});
assertEquals(sticker.name, "sticker name");
assertEquals(sticker.description, "sticker description");
assertEquals(sticker.tags, "sticker tags");
const channel = await bot.helpers.createChannel(CACHED_COMMUNITY_GUILD_ID);
const channel = await bot.helpers.createChannel(CACHED_COMMUNITY_GUILD_ID, { name: "test" });
const message = await bot.helpers.sendMessage(channel.id, { stickerIds: [sticker.id] });
assertEquals(message.stickerItems?.[0].formatType, StickerFormatTypes.Png);
@@ -24,4 +24,5 @@ Deno.test("[stickers] Create guild sticker", async () => {
assertEquals(message.stickerItems?.[0].name, sticker.name);
await bot.helpers.deleteGuildSticker(CACHED_COMMUNITY_GUILD_ID, sticker.id);
await bot.helpers.deleteChannel(channel.id);
});

View File

@@ -8,7 +8,7 @@ Deno.test("[stickers] Delete guild sticker", async () => {
name: "sticker name",
description: "sticker description",
tags: "sticker tags",
file: { blob: new Blob(), name: "sticker file name" },
file: "https://cdn.discordapp.com/emojis/785403373817823272.png",
});
await bot.helpers.deleteGuildSticker(CACHED_COMMUNITY_GUILD_ID, sticker.id);
await assertRejects(() => bot.helpers.getGuildSticker(CACHED_COMMUNITY_GUILD_ID, sticker.id));

View File

@@ -8,7 +8,7 @@ Deno.test("[stickers] Edit guild sticker", async () => {
name: "test",
description: "test",
tags: "test",
file: { blob: new Blob(), name: "test.png" },
file: "https://cdn.discordapp.com/emojis/785403373817823272.png",
});
const editSticker = await bot.helpers.editGuildSticker(CACHED_COMMUNITY_GUILD_ID, createSticker.id, {
name: "sticker name",

View File

@@ -8,7 +8,7 @@ Deno.test("[stickers] Get guild sticker", async () => {
name: "sticker name",
description: "sticker description",
tags: "sticker tags",
file: { blob: new Blob(), name: "sticker file name" },
file: "https://cdn.discordapp.com/emojis/785403373817823272.png",
});
const getSticker = await bot.helpers.getGuildSticker(CACHED_COMMUNITY_GUILD_ID, createSticker.id);
assertEquals(getSticker.name, "sticker name");

View File

@@ -8,13 +8,13 @@ Deno.test("[stickers] Get guild stickers", async () => {
name: "sticker 1",
description: "sticker 1",
tags: "sticker tags 1",
file: { blob: new Blob(), name: "test1.png" },
file: "https://cdn.discordapp.com/emojis/785403373817823272.png",
});
const sticker2 = await bot.helpers.createGuildSticker(CACHED_COMMUNITY_GUILD_ID, {
name: "sticker 2",
description: "sticker 2",
tags: "sticker tags 2",
file: { blob: new Blob(), name: "test2.png" },
file: "https://cdn.discordapp.com/emojis/785403373817823272.png",
});
const stickers = await bot.helpers.getGuildStickers(CACHED_COMMUNITY_GUILD_ID);
assertEquals(stickers.size > 1, true);

View File

@@ -1260,8 +1260,6 @@ export interface DiscordInputTextComponent {
min_length?: number;
/** The maximum length of the text the user has to provide */
max_length?: number;
/** Whether or not this input is required. */
required?: boolean;
/** Pre-filled value for input text. */
value?: string;
}
@@ -2103,6 +2101,8 @@ export interface DiscordComponent {
max_length?: number;
/** a list of child components */
components?: DiscordComponent[];
/** whether this component is required to be filled, default true */
required?: boolean;
}
/** https://discord.com/developers/docs/topics/gateway#channel-pins-update */