diff --git a/src/handlers/roles/GUILD_ROLE_CREATE.ts b/src/handlers/roles/GUILD_ROLE_CREATE.ts index 5259734e0..82119f043 100644 --- a/src/handlers/roles/GUILD_ROLE_CREATE.ts +++ b/src/handlers/roles/GUILD_ROLE_CREATE.ts @@ -8,7 +8,7 @@ export async function handleGuildRoleCreate(data: DiscordGatewayPayload) { const guild = await cacheHandlers.get("guilds", payload.guild_id); if (!guild) return; - const role = await structures.createDiscordenoRole(payload.role); + const role = await structures.createDiscordenoRole(payload); guild.roles = guild.roles.set(payload.role.id, role); await cacheHandlers.set("guilds", payload.guild_id, guild); diff --git a/src/handlers/roles/GUILD_ROLE_UPDATE.ts b/src/handlers/roles/GUILD_ROLE_UPDATE.ts index 22c78ba1a..23572e3e7 100644 --- a/src/handlers/roles/GUILD_ROLE_UPDATE.ts +++ b/src/handlers/roles/GUILD_ROLE_UPDATE.ts @@ -11,7 +11,7 @@ export async function handleGuildRoleUpdate(data: DiscordGatewayPayload) { const cachedRole = guild.roles.get(payload.role.id); if (!cachedRole) return; - const role = await structures.createDiscordenoRole(payload.role); + const role = await structures.createDiscordenoRole(payload); guild.roles.set(payload.role.id, role); await cacheHandlers.set("guilds", guild.id, guild); diff --git a/src/helpers/roles/create_role.ts b/src/helpers/roles/create_role.ts index 72797ff74..e5e52b6ca 100644 --- a/src/helpers/roles/create_role.ts +++ b/src/helpers/roles/create_role.ts @@ -22,7 +22,7 @@ export async function createRole( }); const roleData = result as RoleData; - const role = await structures.createDiscordenoRole(roleData); + const role = await structures.createDiscordenoRole({role: roleData, guild_id: guildId}); const guild = await cacheHandlers.get("guilds", guildId); guild?.roles.set(role.id, role); diff --git a/src/helpers/roles/swap_roles.ts b/src/helpers/roles/swap_roles.ts index 60a2f0cf9..b3c24e8ab 100644 --- a/src/helpers/roles/swap_roles.ts +++ b/src/helpers/roles/swap_roles.ts @@ -3,13 +3,13 @@ import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; /** Modify the positions of a set of role objects for the guild. Requires the MANAGE_ROLES permission. */ -export async function swapRoles(guildId: string, rolePositons: PositionSwap) { +export async function swapRoles(guildId: string, rolePositions: PositionSwap) { await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); const result = await rest.runMethod( "patch", endpoints.GUILD_ROLES(guildId), - rolePositons, + rolePositions, ); return result; diff --git a/tests/mod.ts b/tests/mod.ts index 216393c9f..f3015d8ce 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -40,6 +40,14 @@ import "./messages/get_reactions.ts"; import "./messages/pin_message.ts"; import "./messages/unpin_message.ts"; +// Roles tests +import "./roles/add_role.ts"; +import "./roles/create_role.ts"; +import "./roles/delete_role.ts"; +import "./roles/edit_role.ts"; +import "./roles/remove_role.ts"; +import "./roles/swap_roles.ts"; + // Final cleanup import "./guilds/delete_server.ts"; import "./ws/ws_close.ts"; diff --git a/tests/roles/add_role.ts b/tests/roles/add_role.ts new file mode 100644 index 000000000..6dcc56711 --- /dev/null +++ b/tests/roles/add_role.ts @@ -0,0 +1,65 @@ +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import {assertEquals, assertExists} from "../deps.ts"; +import { cache } from "../../src/cache.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import {addRole} from "../../src/helpers/roles/add_role.ts"; +import {createRole} from "../../src/helpers/roles/create_role.ts"; +import {botId} from "../../src/bot.ts"; + +async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) { + const role = await createRole(tempData.guildId, { + name: "hoti", + }); + + assertExists(role); + + // Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed + await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.has(role.id)); + + if (!cache.guilds.get(tempData.guildId)?.roles.has(role.id)) { + throw new Error(`The role seemed to be created but it was not cached.`); + } + + if (type === "raw") { + await addRole(tempData.guildId, botId, role.id, reason); + } else { + await cache.members.get(botId).addRole(tempData.guildId, role.id, reason); + } + + // Delay the execution by 5 seconds to allow GUILD_MEMBER_UPDATE event to be processed + await delayUntil(10000, () => cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(role.id)); + + assertEquals(cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(role.id), true); +} + +Deno.test({ + name: "[role] add a role without a reason", + async fn() { + await ifItFailsBlameWolf("raw") + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[role] add a role with a reason", + async fn() { + await ifItFailsBlameWolf("raw", "with a reason") + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[role] member.addRole() without a reason", + async fn() { + await ifItFailsBlameWolf("getter") + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[role] member.addRole() with a reason", + async fn() { + await ifItFailsBlameWolf("getter", "with a reason") + }, + ...defaultTestOptions, +}); diff --git a/tests/roles/create_role.ts b/tests/roles/create_role.ts new file mode 100644 index 000000000..dc93252a9 --- /dev/null +++ b/tests/roles/create_role.ts @@ -0,0 +1,36 @@ +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import {assertExists} from "../deps.ts"; +import { cache } from "../../src/cache.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import {createRole} from "../../src/helpers/roles/create_role.ts"; + +async function ifItFailsBlameWolf(reason?: string) { + const role = await createRole(tempData.guildId, { + name: "hoti", + }, reason); + + assertExists(role); + + // Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed + await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.has(role.id)); + + if (!cache.guilds.get(tempData.guildId)?.roles.has(role.id)) { + throw new Error(`The role seemed to be created but it was not cached.`); + } +} + +Deno.test({ + name: "[role] create a role without a reason", + async fn() { + await ifItFailsBlameWolf() + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[role] create a role with a reason", + async fn() { + await ifItFailsBlameWolf( "with a reason") + }, + ...defaultTestOptions, +}); diff --git a/tests/roles/delete_role.ts b/tests/roles/delete_role.ts new file mode 100644 index 000000000..deecd9e46 --- /dev/null +++ b/tests/roles/delete_role.ts @@ -0,0 +1,46 @@ +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import {assertExists} from "../deps.ts"; +import { cache } from "../../src/cache.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import {createRole} from "../../src/helpers/roles/create_role.ts"; +import {deleteRole} from "../../src/helpers/roles/delete_role.ts"; + +async function ifItFailsBlameWolf(reason?: string) { + const role = await createRole(tempData.guildId, { + name: "hoti", + }, reason); + + assertExists(role); + + // Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed + await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.has(role.id)); + + if (!cache.guilds.get(tempData.guildId)?.roles.has(role.id)) { + throw new Error(`The role seemed to be created but it was not cached.`); + } + + await deleteRole(tempData.guildId, role.id); + + // Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed + await delayUntil(10000, () => !cache.guilds.get(tempData.guildId)?.roles.has(role.id)); + + if (cache.guilds.get(tempData.guildId)?.roles.has(role.id)) { + throw new Error(`The role should have been deleted but it is still in cache.`); + } +} + +Deno.test({ + name: "[role] delete a role without a reason", + async fn() { + await ifItFailsBlameWolf() + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[role] delete a role with a reason", + async fn() { + await ifItFailsBlameWolf( "with a reason") + }, + ...defaultTestOptions, +}); diff --git a/tests/roles/edit_role.ts b/tests/roles/edit_role.ts new file mode 100644 index 000000000..0cb346f94 --- /dev/null +++ b/tests/roles/edit_role.ts @@ -0,0 +1,34 @@ +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import {assertEquals, assertExists} from "../deps.ts"; +import { cache } from "../../src/cache.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import {createRole} from "../../src/helpers/roles/create_role.ts"; +import {editRole} from "../../src/helpers/roles/edit_role.ts"; + +Deno.test({ + name: "[role] edit a role", + async fn() { + const role = await createRole(tempData.guildId, { + name: "hoti", + }); + + assertExists(role); + + // Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed + await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.has(role.id)); + + if (!cache.guilds.get(tempData.guildId)?.roles.has(role.id)) { + throw new Error(`The role seemed to be created but it was not cached.`); + } + + await editRole(tempData.guildId, role.id, { + name: "#rememberAyntee" + }); + + // Delay the execution by 5 seconds to allow GUILD_ROLE_UPDATE event to be processed + await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.get(role.id)?.name === "#rememberAyntee"); + + assertEquals(cache.guilds.get(tempData.guildId)?.roles.get(role.id)?.name === "#rememberAyntee", true); + }, + ...defaultTestOptions, +}); diff --git a/tests/roles/get_roles.ts b/tests/roles/get_roles.ts new file mode 100644 index 000000000..9fb577bc0 --- /dev/null +++ b/tests/roles/get_roles.ts @@ -0,0 +1,20 @@ +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import {assertEquals} from "../deps.ts"; +import { cache } from "../../src/cache.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import {botId} from "../../src/bot.ts"; +import {getRoles} from "../../src/helpers/roles/get_roles.ts"; + +Deno.test({ + name: "[role] get roles", + async fn() { + cache.guilds.get(tempData.guildId)?.roles.clear(); + + await getRoles(tempData.guildId); + + await delayUntil(10000, () => cache.members.get(botId)?.guilds.get(tempData.guildId).roles.length > 0); + + assertEquals(cache.members.get(botId)?.guilds.get(tempData.guildId).roles.length > 0, true); + }, + ...defaultTestOptions, +}); diff --git a/tests/roles/remove_role.ts b/tests/roles/remove_role.ts new file mode 100644 index 000000000..210378cda --- /dev/null +++ b/tests/roles/remove_role.ts @@ -0,0 +1,75 @@ +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import {assertEquals, assertExists} from "../deps.ts"; +import { cache } from "../../src/cache.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import {addRole} from "../../src/helpers/roles/add_role.ts"; +import {createRole} from "../../src/helpers/roles/create_role.ts"; +import {botId} from "../../src/bot.ts"; +import {removeRole} from "../../src/helpers/roles/remove_role.ts"; + +async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) { + const role = await createRole(tempData.guildId, { + name: "hoti", + }); + + assertExists(role); + + // Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed + await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.has(role.id)); + + if (!cache.guilds.get(tempData.guildId)?.roles.has(role.id)) { + throw new Error(`The role seemed to be created but it was not cached.`); + } + + if (type === "raw") { + await addRole(tempData.guildId, botId, role.id, reason); + } else { + await cache.members.get(botId).addRole(tempData.guildId, role.id, reason); + } + + // Delay the execution by 5 seconds to allow GUILD_MEMBER_UPDATE event to be processed + await delayUntil(10000, () => cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(role.id)); + + if (type === "raw") { + await removeRole(tempData.guildId, botId, role.id, reason); + } else { + await cache.members.get(botId).removeRole(tempData.guildId, role.id, reason); + } + + // Delay the execution by 5 seconds to allow GUILD_MEMBER_UPDATE event to be processed + await delayUntil(10000, () => !cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(role.id)); + + assertEquals(cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(role.id), false); +} + +Deno.test({ + name: "[role] remove a role without a reason", + async fn() { + await ifItFailsBlameWolf("raw") + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[role] remove a role with a reason", + async fn() { + await ifItFailsBlameWolf("raw", "with a reason") + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[role] member.removeRole() without a reason", + async fn() { + await ifItFailsBlameWolf("getter") + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[role] member.removeRole() with a reason", + async fn() { + await ifItFailsBlameWolf("getter", "with a reason") + }, + ...defaultTestOptions, +}); diff --git a/tests/roles/swap_roles.ts b/tests/roles/swap_roles.ts new file mode 100644 index 000000000..9bbbff37d --- /dev/null +++ b/tests/roles/swap_roles.ts @@ -0,0 +1,45 @@ +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import {assertEquals, assertExists} from "../deps.ts"; +import { cache } from "../../src/cache.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import {addRole} from "../../src/helpers/roles/add_role.ts"; +import {createRole} from "../../src/helpers/roles/create_role.ts"; +import {botId} from "../../src/bot.ts"; +import {swapRoles} from "../../src/helpers/roles/swap_roles.ts"; +import {delay} from "../../src/util/utils.ts"; + +Deno.test({ + name: "[role] swap roles", + async fn() { + const role = await createRole(tempData.guildId, { + name: "hoti", + }); + + assertExists(role); + + const secondRole = await createRole(tempData.guildId, { + name: "not hoti", + }); + + assertExists(secondRole); + + // Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed + await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.has(role.id) && cache.guilds.get(tempData.guildId)?.roles.has(secondRole.id)); + + if (!cache.guilds.get(tempData.guildId)?.roles.has(role.id) || !cache.guilds.get(tempData.guildId)?.roles.has(secondRole.id)) { + throw new Error(`The role seemed to be created but it was not cached.`); + } + + const result = await swapRoles(tempData.guildId, [ + { + id: role.id, + }, + { + id: secondRole.id, + }, + ]); + + assertExists(result); + }, + ...defaultTestOptions, +});