diff --git a/src/managers/guilds.ts b/src/managers/guilds.ts index 30a8747..36166f6 100644 --- a/src/managers/guilds.ts +++ b/src/managers/guilds.ts @@ -3,36 +3,22 @@ import { Guild } from '../structures/guild.ts' import { Role } from '../structures/role.ts' import { GUILD, GUILDS, GUILD_PREVIEW } from '../types/endpoint.ts' import { - GuildChannels, GuildPayload, MemberPayload, GuildCreateRolePayload, GuildCreatePayload, - Verification, - GuildCreateChannelOptions, GuildCreateChannelPayload, GuildPreview, GuildPreviewPayload, GuildModifyOptions, - GuildModifyPayload + GuildModifyPayload, + GuildCreateOptions } from '../types/guild.ts' import { BaseManager } from './base.ts' import { MembersManager } from './members.ts' import { fetchAuto } from '../../deps.ts' import { Emoji } from '../structures/emoji.ts' -export interface GuildCreateOptions { - name: string - region?: string - icon?: string - verificationLevel?: Verification - roles?: Array - channels?: Array - afkChannelID?: string - afkTimeout?: number - systemChannelID?: string -} - export class GuildManager extends BaseManager { constructor(client: Client) { super(client, 'guilds', Guild) @@ -66,6 +52,7 @@ export class GuildManager extends BaseManager { * @param options Options for creating a guild */ async create(options: GuildCreateOptions): Promise { + // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions if (options.icon !== undefined && !options.icon.startsWith('data:')) { options.icon = await fetchAuto(options.icon) } @@ -145,7 +132,7 @@ export class GuildManager extends BaseManager { } /** - * Edits a guild. + * Edits a guild. Returns edited guild. * @param guild Guild or guild id * @param options Guild edit options * @param asRaw true for get raw data, false for get guild(defaults to false) @@ -225,7 +212,7 @@ export class GuildManager extends BaseManager { } /** - * Deletes a guild. + * Deletes a guild. Returns deleted guild. * @param guild Guild or guild id */ async delete(guild: Guild | string): Promise { diff --git a/src/managers/roles.ts b/src/managers/roles.ts index 0ad93ef..f0a1fda 100644 --- a/src/managers/roles.ts +++ b/src/managers/roles.ts @@ -3,7 +3,7 @@ import { Client } from '../models/client.ts' import { Guild } from '../structures/guild.ts' import { Role } from '../structures/role.ts' import { GUILD_ROLE, GUILD_ROLES } from '../types/endpoint.ts' -import { RolePayload } from '../types/role.ts' +import { RoleModifyPayload, RolePayload } from '../types/role.ts' import { BaseManager } from './base.ts' export interface CreateGuildRoleOptions { @@ -35,6 +35,12 @@ export class RolesManager extends BaseManager { }) } + async get(key: string): Promise { + const raw = await this._get(key) + if (raw === undefined) return + return new Role(this.client, raw, this.guild) + } + async fromPayload(roles: RolePayload[]): Promise { for (const role of roles) { await this.set(role.id, role) @@ -80,4 +86,17 @@ export class RolesManager extends BaseManager { ) return true } + + async edit(role: Role | string, options: RoleModifyPayload): Promise { + if (role instanceof Role) { + role = role.id + } + const resp = await this.client.rest.patch( + GUILD_ROLE(this.guild.id, role), + options + ) + + const result = new Role(this.client, resp, this.guild) + return result + } } diff --git a/src/structures/role.ts b/src/structures/role.ts index ed2e821..f5e9129 100644 --- a/src/structures/role.ts +++ b/src/structures/role.ts @@ -1,10 +1,12 @@ import { Client } from '../models/client.ts' import { Base } from './base.ts' -import { RolePayload } from '../types/role.ts' +import { RoleModifyPayload, RolePayload } from '../types/role.ts' import { Permissions } from '../utils/permissions.ts' +import { Guild } from './guild.ts' export class Role extends Base { id: string + guild: Guild name: string color: number hoist: boolean @@ -14,9 +16,10 @@ export class Role extends Base { mentionable: boolean tags?: RoleTags - constructor(client: Client, data: RolePayload) { + constructor(client: Client, data: RolePayload, guild: Guild) { super(client, data) this.id = data.id + this.guild = guild this.name = data.name this.color = data.color this.hoist = data.hoist @@ -46,6 +49,10 @@ export class Role extends Base { this.managed = data.managed ?? this.managed this.mentionable = data.mentionable ?? this.mentionable } + + async edit(options: RoleModifyPayload): Promise { + return this.guild.roles.edit(this, options) + } } export interface RoleTags { diff --git a/src/types/guild.ts b/src/types/guild.ts index 77446f1..90989c7 100644 --- a/src/types/guild.ts +++ b/src/types/guild.ts @@ -1,6 +1,7 @@ import { Emoji } from '../structures/emoji.ts' import { CategoryChannel } from '../structures/guildCategoryChannel.ts' import { VoiceChannel } from '../structures/guildVoiceChannel.ts' +import { Role } from '../structures/role.ts' import { GuildTextChannel } from '../structures/textChannel.ts' import { ApplicationPayload } from './application.ts' import { @@ -206,6 +207,18 @@ export interface GuildCreateChannelOptions { parentID?: string } +export interface GuildCreateOptions { + name: string + region?: string + icon?: string + verificationLevel?: Verification + roles?: Array + channels?: Array + afkChannelID?: string + afkTimeout?: number + systemChannelID?: string +} + export interface GuildPreviewPayload { id: string name: string diff --git a/src/types/role.ts b/src/types/role.ts index 5ddfee8..72cea94 100644 --- a/src/types/role.ts +++ b/src/types/role.ts @@ -18,3 +18,11 @@ export interface RoleTagsPayload { /** The id of the integration this role belongs to */ integration_id?: string } + +export interface RoleModifyPayload { + name?: string | null + permissions?: string | null + color?: number | null + hoist?: boolean | null + mentionable?: boolean | null +}