From 3ca46f747b9f2146aba75eb92e36e496ec452237 Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Tue, 6 Apr 2021 10:21:27 +0200 Subject: [PATCH] fix(structures/role): use new types & add MemberStruct interface (#758) * Update role.ts * remove mod thing --- src/structures/role.ts | 110 ++++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 35 deletions(-) diff --git a/src/structures/role.ts b/src/structures/role.ts index 3d3a5fae9..14f7f813d 100644 --- a/src/structures/role.ts +++ b/src/structures/role.ts @@ -1,11 +1,17 @@ import { cache } from "../cache.ts"; import { deleteRole } from "../helpers/roles/delete_role.ts"; import { editRole } from "../helpers/roles/edit_role.ts"; -import { createNewProp } from "../util/utils.ts"; +import { CreateGuildRole } from "../types/guilds/create_guild_role.ts"; +import { DiscordGuildRoleCreate } from "../types/guilds/guild_role_create.ts"; +import { Errors } from "../types/misc/errors.ts"; +import { Role } from "../types/permissions/role.ts"; +import { Collection } from "../util/collection.ts"; +import { highestRole } from "../util/permissions.ts"; +import { createNewProp, snakeKeysToCamelCase } from "../util/utils.ts"; -const baseRole: Partial = { +const baseRole: Partial = { get guild() { - return cache.guilds.find((g) => g.roles.has(this.id!)); + return cache.guilds.get(this.guildId!); }, get hexColor() { return this.color!.toString(16); @@ -20,31 +26,13 @@ const baseRole: Partial = { }, // METHODS - delete(guildId?: string) { - // If not guild id was provided try and find one - if (!guildId) guildId = guildId || this.guild?.id; - // If a guild id is still not available error out - if (!guildId) { - throw new Error( - "role.delete() did not find a valid guild in cache. Please provide the guildId like role.delete(guildId)", - ); - } - - return deleteRole(guildId, this.id!).catch(console.error); + delete() { + return deleteRole(this.guildId!, this.id!).catch(console.error); }, - edit(options: CreateRoleOptions, guildId?: string) { - // If not guild id was provided try and find one - if (!guildId) guildId = guildId || this.guild?.id; - // If a guild id is still not available error out - if (!guildId) { - throw new Error( - "role.edit() did not find a valid guild in cache. Please provide the guildId like role.edit({}, guildId)", - ); - } - - return editRole(guildId, this.id!, options); + edit(options) { + return editRole(this.guildId!, this.id!, options); }, - higherThanRoleId(roleId: string, position?: number) { + higherThanRole(roleId: string, position?: number) { // If no position try and find one from cache if (!position) position = this.guild?.roles.get(roleId)?.position; // If still none error out. @@ -61,22 +49,74 @@ const baseRole: Partial = { return this.position! > position; }, + async higherThanMember(memberId: string) { + const guild = this.guild; + if (!guild) throw new Error(Errors.GUILD_NOT_FOUND); + + if (this.guild.ownerId === memberId) return false; + + const memberHighestRole = await highestRole(guild, memberId); + return this.higherThanRole!( + memberHighestRole.id, + memberHighestRole.position, + ); + }, }; // deno-lint-ignore require-await -export async function createRoleStruct({ tags = {}, ...rest }: RoleData) { - const restProps: Record> = {}; +export async function createRoleStruct(data: DiscordGuildRoleCreate) { + const { + tags = {}, + ...rest + } = snakeKeysToCamelCase({ guildId: data.guild_id, ...data.role }) as Role & { + guildId: string; + }; + + const props: Record> = {}; for (const key of Object.keys(rest)) { // @ts-ignore index signature - restProps[key] = createNewProp(rest[key]); + props[key] = createNewProp(rest[key]); } - const role = Object.create(baseRole, { - ...restProps, - botId: createNewProp(tags.bot_id), - isNitroBoostRole: createNewProp("premium_subscriber" in tags), - integrationId: createNewProp(tags.integration_id), + const role: RoleStruct = Object.create(baseRole, { + ...props, + botId: createNewProp(tags.botId), + isNitroBoostRole: createNewProp("premiumSubscriber" in tags), + integrationId: createNewProp(tags.integrationId), }); - return role as Role; + return role; +} + +export interface RoleStruct extends Omit { + /** The bot id that is associated with this role. */ + botId?: string; + /** If this role is the nitro boost role. */ + isNitroBoostRole: boolean; + /** The integration id that is associated with this role */ + integrationId: string; + /** The roles guildId */ + guildId: string; + + // GETTERS + + /** The guild where this role is. If undefined, the guild is not cached */ + guild?: GuildStruct; + /** The hex color for this role. */ + hexColor: string; + /** The cached members that have this role */ + members: Collection; + /** The @ mention of the role in a string. */ + mention: string; + + // METHODS + + /** Delete the role */ + delete(): ReturnType; + /** Edits the role */ + edit(options: CreateGuildRole): ReturnType; + /** Checks if this role is higher than another role. */ + higherThanRole(roleId: string, position?: number): boolean; + /** Checks if the role has a higher position than the given member */ + higherThanMember(memberId: string): Promise; }