diff --git a/structures/Guild.ts b/structures/Guild.ts index 98de437..2f88322 100644 --- a/structures/Guild.ts +++ b/structures/Guild.ts @@ -1,11 +1,22 @@ import type { Model } from "./Base.ts"; import type { Snowflake } from "../util/Snowflake.ts"; import type { Session } from "../session/Session.ts"; -import type { DiscordGuild, DiscordMember, MakeRequired } from "../vendor/external.ts"; +import type { DiscordGuild, DiscordRole } from "../vendor/external.ts"; import { DefaultMessageNotificationLevels, ExplicitContentFilterLevels, VerificationLevels } from "../vendor/external.ts"; -import { iconHashToBigInt, iconBigintToHash as _iconBigintToHash } from "../util/hash.ts"; +import { iconHashToBigInt, iconBigintToHash } from "../util/hash.ts"; import { Member } from "./Member.ts"; import { BaseGuild } from "./BaseGuild.ts"; +import { Role } from "./Role.ts"; +import { Routes } from "../util/mod.ts"; + +export interface CreateRole { + name?: string; + color?: number; + iconHash?: string | bigint; + unicodeEmoji?: string; + hoist?: boolean; + mentionable?: boolean; +} /** * Represents a guild @@ -23,7 +34,8 @@ export class Guild extends BaseGuild implements Model { this.vefificationLevel = data.verification_level; this.defaultMessageNotificationLevel = data.default_message_notifications; this.explicitContentFilterLevel = data.explicit_content_filter; - this.members = data.members?.map((member) => new Member(session, member as MakeRequired)) ?? []; + this.members = data.members?.map((member) => new Member(session, { ...member, user: member.user! })) ?? []; + this.roles = data.roles.map((role) => new Role(session, this, role)); } splashHash?: bigint; @@ -35,4 +47,38 @@ export class Guild extends BaseGuild implements Model { defaultMessageNotificationLevel: DefaultMessageNotificationLevels; explicitContentFilterLevel: ExplicitContentFilterLevels; members: Member[]; + roles: Role[]; + + async createRole(options: CreateRole) { + let icon: string | undefined; + + if (options.iconHash) { + if (typeof options.iconHash === "string") { + icon = options.iconHash; + } + else { + icon = iconBigintToHash(options.iconHash); + } + } + + const role = await this.session.rest.runMethod( + this.session.rest, + "PUT", + Routes.GUILD_ROLES(this.id), + { + name: options.name, + color: options.color, + icon, + unicode_emoji: options.unicodeEmoji, + hoist: options.hoist, + mentionable: options.mentionable, + } + ); + + return new Role(this.session, this, role); + } + + async deleteRole(roleId: Snowflake): Promise { + await this.session.rest.runMethod(this.session.rest, "DELETE", Routes.GUILD_ROLE(this.id, roleId)); + } } diff --git a/structures/Role.ts b/structures/Role.ts new file mode 100644 index 0000000..fd754f7 --- /dev/null +++ b/structures/Role.ts @@ -0,0 +1,62 @@ +import type { Model } from "./Base.ts"; +import type { Session } from "../session/Session.ts"; +import type { DiscordRole } from "../vendor/external.ts"; +import { Snowflake } from "../util/Snowflake.ts"; +import { iconHashToBigInt } from "../util/hash.ts"; +import { Guild } from "./Guild.ts"; + +export class Role implements Model { + constructor(session: Session, guild: Guild, data: DiscordRole) { + this.session = session; + this.id = data.id; + this.guild = guild; + this.hoist = data.hoist; + this.iconHash = data.icon ? iconHashToBigInt(data.icon) : undefined; + this.color = data.color; + this.name = data.name; + this.unicodeEmoji = data.unicode_emoji; + this.mentionable = data.mentionable; + this.managed = data.managed; + } + + session: Session; + id: Snowflake; + guild: Guild; + hoist: boolean; + iconHash?: bigint; + color: number; + name: string; + unicodeEmoji?: string; + mentionable: boolean; + managed: boolean; + + get createdTimestamp() { + return Snowflake.snowflakeToTimestamp(this.id); + } + + get createdAt() { + return new Date(this.createdTimestamp); + } + + get hexColor() { + return `#${this.color.toString(16).padStart(6, '0')}`; + } + + /* + * delete() { + * return.this.guild.deleteRole(this.id); + * } + * edit() { + * return this.guild.editRole(this.id); + * } + * */ + + toString() { + switch (this.id) { + case this.guild.id: + return "@everyone"; + default: + return `<@&${this.id}>`; + } + } +} diff --git a/util/Routes.ts b/util/Routes.ts index a6adec0..b2f6f2c 100644 --- a/util/Routes.ts +++ b/util/Routes.ts @@ -73,3 +73,11 @@ export function GUILD_BANS(guildId: Snowflake, options?: GetBans) { return url; } + +export function GUILD_ROLE(guildId: Snowflake, roleId: Snowflake) { + return `/guilds/${guildId}/roles/${roleId}`; +} + +export function GUILD_ROLES(guildId: Snowflake) { + return `/guilds/${guildId}/roles`; +}