diff --git a/src/helpers/channels/create_channel.ts b/src/helpers/channels/create_channel.ts index 28e2aa9a1..48ac3b3d8 100644 --- a/src/helpers/channels/create_channel.ts +++ b/src/helpers/channels/create_channel.ts @@ -24,10 +24,11 @@ export async function createChannel(bot: Bot, guildId: bigint, options?: CreateG userLimit: options.userLimit, rateLimitPerUser: options.rateLimitPerUser, position: options.position, - parentId: options.parentId, + parentId: options.parentId?.toString(), nsfw: options.nsfw, permission_overwrites: options?.permissionOverwrites?.map((perm) => ({ - ...perm, + id: perm.id.toString(), + type: perm.type, allow: bot.utils.calculateBits(perm.allow), deny: bot.utils.calculateBits(perm.deny), })), diff --git a/src/types/channels/overwrite.ts b/src/types/channels/overwrite.ts index 3e3e5f21d..9c498c385 100644 --- a/src/types/channels/overwrite.ts +++ b/src/types/channels/overwrite.ts @@ -3,7 +3,7 @@ import { DiscordOverwriteTypes } from "./overwrite_types.ts"; export interface Overwrite { /** Role or user id */ - id: string; + id: bigint; /** Either 0 (role) or 1 (member) */ type: DiscordOverwriteTypes; /** Permission bit set */ @@ -13,7 +13,8 @@ export interface Overwrite { } /** https://discord.com/developers/docs/resources/channel#overwrite-object */ -export interface DiscordOverwrite extends Omit { +export interface DiscordOverwrite extends Omit { + id: string; allow: string; deny: string; } diff --git a/tests/helpers/channels/createChannel.ts b/tests/helpers/channels/createChannel.ts new file mode 100644 index 000000000..c6716b0fc --- /dev/null +++ b/tests/helpers/channels/createChannel.ts @@ -0,0 +1,34 @@ +import { Bot } from "../../../src/bot.ts"; +import { CreateGuildChannel } from "../../../src/types/guilds/create_guild_channel.ts"; +import { DiscordChannelTypes } from "../../../src/types/mod.ts"; +import { assertExists,assertEquals } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function createChannelTests(bot: Bot, guildId: bigint, options: CreateGuildChannel, t: Deno.TestContext) { + const channel = await bot.helpers.createChannel(guildId, options); + + // Assertions + assertExists(channel); + assertEquals(channel.type, options.type || DiscordChannelTypes.GuildText); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => bot.cache.channels.has(channel.id)); + + if (!bot.cache.channels.has(channel.id)) { + throw new Error("The channel seemed to be created but it was not cached."); + } + + if (options.topic && channel.topic !== options.topic) { + throw new Error("The channel was supposed to have a topic but it does not appear to be the same topic."); + } + + if (options.bitrate && channel.bitrate !== options.bitrate) { + throw new Error("The channel was supposed to have a bitrate but it does not appear to be the same bitrate."); + } + + if (options.permissionOverwrites && channel.permissionOverwrites?.length !== options.permissionOverwrites.length) { + throw new Error( + "The channel was supposed to have a permissionOverwrites but it does not appear to be the same permissionOverwrites." + ); + } + } \ No newline at end of file diff --git a/tests/mod.ts b/tests/mod.ts index 66e00a511..baa68791c 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -1,5 +1,5 @@ import { TOKEN } from "../configs.ts"; -import { createBot, createEventHandlers, DiscordChannelTypes, startBot, stopBot } from "../mod.ts"; +import { createBot, createEventHandlers, DiscordChannelTypes, DiscordOverwriteTypes, startBot, stopBot } from "../mod.ts"; import { assertEquals, assertExists } from "./deps.ts"; import { deleteMessageWithReasonTest, deleteMessageWithoutReasonTest } from "./helpers/messages/deleteMessage.ts"; import { getMessagesTest } from "./helpers/messages/getMessages.ts"; @@ -19,6 +19,7 @@ import { editMessageTest } from "./helpers/messages/editMessage.ts"; import { fetchSingleMemberTest } from "./helpers/members/fetchMembers.ts"; import { pinMessageTests } from "./helpers/messages/pin.ts"; import { removeAllReactionTests, removeReactionEmojiTest, removeReactionTest } from "./helpers/messages/reactions.ts"; +import { createChannelTests } from "./helpers/channels/createChannel.ts"; Deno.test("[Bot] - Starting Tests", async (t) => { // CHANGE TO TRUE WHEN DEBUGGING SANITIZATION ERRORS @@ -237,6 +238,149 @@ Deno.test("[Bot] - Starting Tests", async (t) => { }), ]); }); + + // ALL CHANNEL RELATED TESTS CAN GO HERE + await Promise.all([ + t.step({ + name: "[channel] send message with text", + fn: async (t) => { + await sendMessageWithTextTest(bot, channel.id, t); + }, + ...sanitizeMode, + }), + + t.step({ + name: "[channel] create a new text channel", + async fn() { + await createChannelTests(bot, guild.id, { name: "Discordeno-test" }, t); + }, + ...sanitizeMode, + }), + + t.step({ + name: "[channel] create a new category channel", + async fn() { + await createChannelTests(bot, guild.id, { + name: "Discordeno-test", + type: DiscordChannelTypes.GuildCategory, + }, t); + }, + ...sanitizeMode, + }), + + // t.step({ + // name: "[channel] create a new news channel", + // async fn() { + // await createChannelTests(bot, guild.id,{ name: "Discordeno-test", type: DiscordChannelTypes.GUILD_NEWS}, t); + // }, + // ...sanitizeMode, + // }), + + // t.step({ + // name: "[channel] create a new store channel", + // async fn() { + // await createChannelTests(bot, guild.id,{ name: "Discordeno-test", type: DiscordChannelTypes.GUILD_STORE}, t); + // }, + // ...sanitizeMode, + // }), + + t.step({ + name: "[channel] create a new voice channel", + async fn() { + await createChannelTests( + bot, + guild.id, + { + name: "Discordeno-test", + type: DiscordChannelTypes.GuildVoice, + }, + t + ); + }, + ...sanitizeMode, + }), + + t.step({ + name: "[channel] create a new voice channel with a bitrate", + async fn() { + await createChannelTests( + bot, + guild.id, + { + name: "discordeno-test", + type: DiscordChannelTypes.GuildVoice, + bitrate: 32000, + }, + t + ); + }, + ...sanitizeMode, + }), + + t.step({ + name: "[channel] create a new voice channel with a user limit", + async fn() { + await createChannelTests( + bot, + guild.id, + { + name: "Discordeno-test", + type: DiscordChannelTypes.GuildVoice, + userLimit: 32, + }, + t + ); + }, + ...sanitizeMode, + }), + + t.step({ + name: "[channel] create a new text channel with a rate limit per user", + async fn() { + await createChannelTests( + bot, + guild.id, + { + name: "Discordeno-test", + rateLimitPerUser: 2423, + }, + t + ); + }, + ...sanitizeMode, + }), + + t.step({ + name: "[channel] create a new text channel with NSFW", + async fn() { + await createChannelTests(bot, guild.id, { name: "Discordeno-test", nsfw: true }, t); + }, + ...sanitizeMode, + }), + + t.step({ + name: "[channel] create a new text channel with permission overwrites", + async fn() { + await createChannelTests( + bot, + guild.id, + { + name: "Discordeno-test", + permissionOverwrites: [ + { + id: bot.id, + type: DiscordOverwriteTypes.Member, + allow: ["VIEW_CHANNEL"], + deny: [], + }, + ], + }, + t + ); + }, + ...sanitizeMode, + }), + ]); }); // MEMBER TESTS GROUPED