mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
tests: add channel tests (#807)
This commit is contained in:
@@ -15,14 +15,14 @@ import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
|
|||||||
/** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
|
/** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
|
||||||
export async function createChannel(
|
export async function createChannel(
|
||||||
guildId: string,
|
guildId: string,
|
||||||
options?: CreateGuildChannel,
|
options?: CreateGuildChannel
|
||||||
) {
|
) {
|
||||||
const requiredPerms: Set<PermissionStrings> = new Set(["MANAGE_CHANNELS"]);
|
const requiredPerms: Set<PermissionStrings> = new Set(["MANAGE_CHANNELS"]);
|
||||||
|
|
||||||
options?.permissionOverwrites?.forEach((overwrite) => {
|
options?.permissionOverwrites?.forEach((overwrite) => {
|
||||||
eventHandlers.debug?.(
|
eventHandlers.debug?.(
|
||||||
"loop",
|
"loop",
|
||||||
`Running forEach loop in create_channel file.`,
|
`Running forEach loop in create_channel file.`
|
||||||
);
|
);
|
||||||
overwrite.allow.forEach(requiredPerms.add, requiredPerms);
|
overwrite.allow.forEach(requiredPerms.add, requiredPerms);
|
||||||
overwrite.deny.forEach(requiredPerms.add, requiredPerms);
|
overwrite.deny.forEach(requiredPerms.add, requiredPerms);
|
||||||
@@ -30,6 +30,9 @@ export async function createChannel(
|
|||||||
|
|
||||||
await requireBotGuildPermissions(guildId, [...requiredPerms]);
|
await requireBotGuildPermissions(guildId, [...requiredPerms]);
|
||||||
|
|
||||||
|
// 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(
|
const result = (await rest.runMethod(
|
||||||
"post",
|
"post",
|
||||||
endpoints.GUILD_CHANNELS(guildId),
|
endpoints.GUILD_CHANNELS(guildId),
|
||||||
@@ -42,7 +45,7 @@ export async function createChannel(
|
|||||||
deny: calculateBits(perm.deny),
|
deny: calculateBits(perm.deny),
|
||||||
})),
|
})),
|
||||||
type: options?.type || DiscordChannelTypes.GUILD_TEXT,
|
type: options?.type || DiscordChannelTypes.GUILD_TEXT,
|
||||||
},
|
}
|
||||||
)) as DiscordChannel;
|
)) as DiscordChannel;
|
||||||
|
|
||||||
const discordenoChannel = await structures.createDiscordenoChannel(result);
|
const discordenoChannel = await structures.createDiscordenoChannel(result);
|
||||||
|
|||||||
@@ -1,18 +1,19 @@
|
|||||||
import { cache, createChannel, delay } from "../../mod.ts";
|
|
||||||
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
||||||
import { assertExists } from "../deps.ts";
|
import { assertEquals, assertExists } from "../deps.ts";
|
||||||
|
import { cache } from "../../src/cache.ts";
|
||||||
|
import { DiscordChannelTypes } from "../../src/types/channels/channel_types.ts";
|
||||||
|
import { CreateGuildChannel } from "../../src/types/guilds/create_guild_channel.ts";
|
||||||
|
import { delay } from "../../src/util/utils.ts";
|
||||||
|
import { createChannel } from "../../src/helpers/channels/create_channel.ts";
|
||||||
|
|
||||||
Deno.test({
|
async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) {
|
||||||
name: "[channel] create a new channel",
|
const channel = await createChannel(tempData.guildId, options);
|
||||||
async fn() {
|
|
||||||
const channel = await createChannel(tempData.guildId, {
|
|
||||||
name: "Discordeno-test",
|
|
||||||
});
|
|
||||||
|
|
||||||
// Assertions
|
// Assertions
|
||||||
assertExists(channel);
|
assertExists(channel);
|
||||||
|
assertEquals(channel.type, options.type || DiscordChannelTypes.GUILD_TEXT);
|
||||||
|
|
||||||
tempData.channelId = channel.id;
|
if (save) tempData.channelId = channel.id;
|
||||||
|
|
||||||
// Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed
|
// Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed
|
||||||
await delay(5000);
|
await delay(5000);
|
||||||
@@ -22,6 +23,95 @@ Deno.test({
|
|||||||
"The channel seemed to be created but it was not cached.",
|
"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.",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "[channel] create a new text channel",
|
||||||
|
async fn() {
|
||||||
|
await ifItFailsBlameWolf({ name: "Discordeno-test"}, true);
|
||||||
},
|
},
|
||||||
...defaultTestOptions,
|
...defaultTestOptions,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "[channel] create a new category channel",
|
||||||
|
async fn() {
|
||||||
|
await ifItFailsBlameWolf({ name: "Discordeno-test", type: DiscordChannelTypes.GUILD_CATEGORY}, true);
|
||||||
|
},
|
||||||
|
...defaultTestOptions,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Deno.test({
|
||||||
|
// name: "[channel] create a new news channel",
|
||||||
|
// async fn() {
|
||||||
|
// await ifItFailsBlameWolf({ name: "Discordeno-test", type: DiscordChannelTypes.GUILD_NEWS}, true);
|
||||||
|
// },
|
||||||
|
// ...defaultTestOptions,
|
||||||
|
// });
|
||||||
|
|
||||||
|
// Deno.test({
|
||||||
|
// name: "[channel] create a new store channel",
|
||||||
|
// async fn() {
|
||||||
|
// await ifItFailsBlameWolf({ name: "Discordeno-test", type: DiscordChannelTypes.GUILD_STORE}, true);
|
||||||
|
// },
|
||||||
|
// ...defaultTestOptions,
|
||||||
|
// });
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "[channel] create a new voice channel",
|
||||||
|
async fn() {
|
||||||
|
await ifItFailsBlameWolf({ name: "Discordeno-test", type: DiscordChannelTypes.GUILD_VOICE}, true);
|
||||||
|
},
|
||||||
|
...defaultTestOptions,
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "[channel] create a new voice channel with a bitrate",
|
||||||
|
async fn() {
|
||||||
|
await ifItFailsBlameWolf({ name: "discordeno-test", type: DiscordChannelTypes.GUILD_VOICE, bitrate: 32000 }, true);
|
||||||
|
},
|
||||||
|
...defaultTestOptions,
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "[channel] create a new voice channel with a user limit",
|
||||||
|
async fn() {
|
||||||
|
await ifItFailsBlameWolf({ name: "Discordeno-test", type: DiscordChannelTypes.GUILD_VOICE, userLimit: 32 }, true);
|
||||||
|
},
|
||||||
|
...defaultTestOptions,
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "[channel] create a new text channel with a rate limit per user",
|
||||||
|
async fn() {
|
||||||
|
await ifItFailsBlameWolf({ name: "Discordeno-test", rateLimitPerUser: 2423 }, true);
|
||||||
|
},
|
||||||
|
...defaultTestOptions,
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "[channel] create a new text channel with NSFW",
|
||||||
|
async fn() {
|
||||||
|
await ifItFailsBlameWolf({ name: "Discordeno-test", nsfw: true }, true);
|
||||||
|
},
|
||||||
|
...defaultTestOptions,
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: Need to validate tests for these options
|
||||||
|
// /** Sorting position of the channel */
|
||||||
|
// position?: number;
|
||||||
|
// /** The channel's permission overwrites */
|
||||||
|
// permissionOverwrites?: Overwrite[];
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
import { cache, createChannel, delay, deleteChannel } from "../../mod.ts";
|
import { cache } from "../../src/cache.ts";
|
||||||
|
import { createChannel } from "../../src/helpers/channels/create_channel.ts";
|
||||||
|
import { deleteChannel } from "../../src/helpers/channels/delete_channel.ts";
|
||||||
|
import { delay } from "../../src/util/utils.ts";
|
||||||
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { cache, createGuild, delay } from "../../mod.ts";
|
|
||||||
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
||||||
import { assertExists } from "../deps.ts";
|
import { assertExists } from "../deps.ts";
|
||||||
|
import { cache } from "../../src/cache.ts";
|
||||||
|
import { delay } from "../../src/util/utils.ts";
|
||||||
|
import { createGuild } from "../../src/helpers/guilds/create_guild.ts";
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "[guild] create a new guild",
|
name: "[guild] create a new guild",
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { cache, delay, deleteServer } from "../../mod.ts";
|
import { cache } from "../../src/cache.ts";
|
||||||
|
import { deleteServer } from "../../src/helpers/guilds/delete_server.ts";
|
||||||
|
import { delay } from "../../src/util/utils.ts";
|
||||||
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
import {
|
|
||||||
addReaction,
|
|
||||||
cache,
|
|
||||||
createEmoji,
|
|
||||||
delay,
|
|
||||||
DiscordReaction,
|
|
||||||
sendMessage,
|
|
||||||
} from "../../mod.ts";
|
|
||||||
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
||||||
import { assertEquals, assertExists } from "../deps.ts";
|
import { assertEquals, assertExists } from "../deps.ts";
|
||||||
|
import { cache } from "../../src/cache.ts";
|
||||||
|
import { DiscordReaction } from "../../src/types/messages/reaction.ts";
|
||||||
|
import { delay } from "../../src/util/utils.ts";
|
||||||
|
import { sendMessage } from "../../src/helpers/messages/send_message.ts";
|
||||||
|
import { addReaction } from "../../src/helpers/messages/add_reaction.ts";
|
||||||
|
import { createEmoji } from "../../src/helpers/emojis/create_emoji.ts"
|
||||||
|
|
||||||
async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) {
|
async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) {
|
||||||
const message = await sendMessage(tempData.channelId, "Hello World!");
|
const message = await sendMessage(tempData.channelId, "Hello World!");
|
||||||
@@ -19,16 +17,15 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) {
|
|||||||
await delay(5000);
|
await delay(5000);
|
||||||
|
|
||||||
if (!cache.messages.has(message.id)) {
|
if (!cache.messages.has(message.id)) {
|
||||||
throw new Error(
|
throw new Error("The message seemed to be sent but it was not cached.");
|
||||||
"The message seemed to be sent but it was not cached.",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let emojiId = "❤";
|
let emojiId = "❤";
|
||||||
|
|
||||||
if (custom) {
|
if (custom) {
|
||||||
emojiId = `<:blamewolf:${
|
emojiId = `<:blamewolf:${
|
||||||
(await createEmoji(
|
(
|
||||||
|
await createEmoji(
|
||||||
tempData.guildId,
|
tempData.guildId,
|
||||||
"blamewolf",
|
"blamewolf",
|
||||||
"https://cdn.discordapp.com/emojis/814955268123000832.png",
|
"https://cdn.discordapp.com/emojis/814955268123000832.png",
|
||||||
@@ -36,8 +33,9 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) {
|
|||||||
name: "blamewolf",
|
name: "blamewolf",
|
||||||
image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
|
image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
|
||||||
roles: [],
|
roles: [],
|
||||||
},
|
}
|
||||||
)).id
|
)
|
||||||
|
).id
|
||||||
}>`;
|
}>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,10 +48,13 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) {
|
|||||||
await delay(5000);
|
await delay(5000);
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
await cache.messages.get(message.id)?.reactions?.filter((
|
await cache.messages
|
||||||
reaction: DiscordReaction,
|
.get(message.id)
|
||||||
) => reaction.emoji?.name === (custom ? "blamewolf" : "❤")).length,
|
?.reactions?.filter(
|
||||||
1,
|
(reaction: DiscordReaction) =>
|
||||||
|
reaction.emoji?.name === (custom ? "blamewolf" : "❤")
|
||||||
|
).length,
|
||||||
|
1
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,23 @@
|
|||||||
import { cache, delay, sendMessage } from "../../mod.ts";
|
|
||||||
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
||||||
import { assertExists } from "../deps.ts";
|
import { assertExists } from "../deps.ts";
|
||||||
|
import { cache } from "../../src/cache.ts";
|
||||||
|
import { delay } from "../../src/util/utils.ts";
|
||||||
|
import { sendMessage } from "../../src/helpers/messages/send_message.ts";
|
||||||
|
import { createChannel } from "../../src/helpers/channels/create_channel.ts";
|
||||||
|
|
||||||
async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||||
let message;
|
const channel = await createChannel(tempData.guildId, {
|
||||||
if (type === "raw") {
|
name: "Discordeno-test",
|
||||||
message = await sendMessage(tempData.channelId, "Hello World!");
|
});
|
||||||
} else {
|
|
||||||
const channel = await cache.channels.get(tempData.channelId);
|
|
||||||
|
|
||||||
assertExists(channel);
|
assertExists(channel);
|
||||||
|
// Wait few seconds for the channel create event to arrive and cache it
|
||||||
|
await delay(5000);
|
||||||
|
|
||||||
message = await channel.send("Hello World!");
|
const message =
|
||||||
}
|
type === "raw"
|
||||||
|
? await sendMessage(channel.id, "Hello World!")
|
||||||
|
: await channel.send("Hello World!");
|
||||||
|
|
||||||
// Assertions
|
// Assertions
|
||||||
assertExists(message);
|
assertExists(message);
|
||||||
@@ -21,9 +26,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
|||||||
await delay(5000);
|
await delay(5000);
|
||||||
|
|
||||||
if (!cache.messages.has(message.id)) {
|
if (!cache.messages.has(message.id)) {
|
||||||
throw new Error(
|
throw new Error("The message seemed to be sent but it was not cached.");
|
||||||
"The message seemed to be sent but it was not cached.",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
import { cache, delay, deleteMessage, sendMessage } from "../../mod.ts";
|
|
||||||
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
||||||
import { assertExists } from "../deps.ts";
|
import { assertExists } from "../deps.ts";
|
||||||
|
import { cache } from "../../src/cache.ts";
|
||||||
|
import { delay } from "../../src/util/utils.ts";
|
||||||
|
import { sendMessage } from "../../src/helpers/messages/send_message.ts";
|
||||||
|
import { deleteMessage } from "../../src/helpers/messages/delete_message.ts";
|
||||||
|
|
||||||
async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
|
async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
|
||||||
const message = await sendMessage(tempData.channelId, "Hello World!");
|
const message = await sendMessage(tempData.channelId, "Hello World!");
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
import { cache, delay, editMessage, sendMessage } from "../../mod.ts";
|
|
||||||
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
||||||
import { assertEquals, assertExists } from "../deps.ts";
|
import { assertEquals, assertExists } from "../deps.ts";
|
||||||
|
import { cache } from "../../src/cache.ts";
|
||||||
|
import { delay } from "../../src/util/utils.ts";
|
||||||
|
import { sendMessage } from "../../src/helpers/messages/send_message.ts";
|
||||||
|
import { editMessage } from "../../src/helpers/messages/edit_message.ts";
|
||||||
|
|
||||||
async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||||
const message = await sendMessage(tempData.channelId, "Hello World!");
|
const message = await sendMessage(tempData.channelId, "Hello World!");
|
||||||
@@ -11,9 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
|||||||
await delay(5000);
|
await delay(5000);
|
||||||
// Make sure the message was created.
|
// Make sure the message was created.
|
||||||
if (!cache.messages.has(message.id)) {
|
if (!cache.messages.has(message.id)) {
|
||||||
throw new Error(
|
throw new Error("The message seemed to be sent but it was not cached.");
|
||||||
"The message seemed to be sent but it was not cached.",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Edit the message now
|
// Edit the message now
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
import { camelKeysToSnakeCase, snakeKeysToCamelCase } from "../../mod.ts";
|
import { snakeKeysToCamelCase,camelKeysToSnakeCase } from "../../src/util/utils.ts";
|
||||||
import { assertEquals } from "../deps.ts";
|
import { assertEquals } from "../deps.ts";
|
||||||
|
|
||||||
const testSnakeObject = {
|
const testSnakeObject = {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { delay, ws } from "../../mod.ts";
|
import { delay } from "../../src/util/utils.ts";
|
||||||
|
import { ws } from "../../src/ws/ws.ts";
|
||||||
import { defaultTestOptions } from "./start_bot.ts";
|
import { defaultTestOptions } from "./start_bot.ts";
|
||||||
|
|
||||||
// Exit the Deno process once all tests are done.
|
// Exit the Deno process once all tests are done.
|
||||||
|
|||||||
Reference in New Issue
Block a user