diff --git a/structures/Guild.ts b/structures/Guild.ts index 1fde9c6..1013814 100644 --- a/structures/Guild.ts +++ b/structures/Guild.ts @@ -81,6 +81,11 @@ export interface BeginGuildPrune { includeRoles?: Snowflake[]; } +export interface ModifyRolePositions { + id: Snowflake; + position?: number | null; +} + /** * Represents a guild * @link https://discord.com/developers/docs/resources/guild#guild-object @@ -212,6 +217,40 @@ export class Guild extends BaseGuild implements Model { return new Role(this.session, role, this.id); } + + + async addRole(memberId: Snowflake, roleId: Snowflake, { reason }: { reason?: string } = {}) { + await this.session.rest.runMethod( + this.session.rest, + "PUT", + Routes.GUILD_MEMBER_ROLE(this.id, memberId, roleId), + { reason }, + ); + } + + async removeRole(memberId: Snowflake, roleId: Snowflake, { reason }: { reason?: string } = {}) { + await this.session.rest.runMethod( + this.session.rest, + "DELETE", + Routes.GUILD_MEMBER_ROLE(this.id, memberId, roleId), + { reason }, + ); + } + + /** + * Returns the roles moved + * */ + async moveRoles(options: ModifyRolePositions[]) { + const roles = await this.session.rest.runMethod( + this.session.rest, + "PATCH", + Routes.GUILD_ROLES(this.id), + options, + ); + + return roles.map((role) => new Role(this.session, role, this.id)); + } + async deleteInvite(inviteCode: string): Promise { await this.session.rest.runMethod( this.session.rest, diff --git a/structures/Member.ts b/structures/Member.ts index 352331a..6468ce3 100644 --- a/structures/Member.ts +++ b/structures/Member.ts @@ -82,6 +82,14 @@ export class Member implements Model { return member; } + async addRole(roleId: Snowflake, options: { reason?: string } = {}) { + await Guild.prototype.addRole.call({ id: this.guildId, session: this.session }, this.user.id, roleId, options); + } + + async removeRole(roleId: Snowflake, options: { reason?: string } = {}) { + await Guild.prototype.removeRole.call({ id: this.guildId, session: this.session }, this.user.id, roleId, options); + } + /** gets the user's avatar */ avatarUrl(options: { format?: ImageFormat; size?: ImageSize } = { size: 128 }) { let url: string; diff --git a/structures/Role.ts b/structures/Role.ts index be1f878..41a4c97 100644 --- a/structures/Role.ts +++ b/structures/Role.ts @@ -56,6 +56,14 @@ export class Role implements Model { return role; } + async add(memberId: Snowflake, options: { reason?: string } = {}) { + await Guild.prototype.addRole.call({ id: this.guildId, session: this.session }, memberId, this.id, options); + } + + async remove(memberId: Snowflake, options: { reason?: string } = {}) { + await Guild.prototype.removeRole.call({ id: this.guildId, session: this.session }, memberId, this.id, options); + } + toString() { switch (this.id) { case this.guildId: diff --git a/util/Routes.ts b/util/Routes.ts index 492d95b..db2654e 100644 --- a/util/Routes.ts +++ b/util/Routes.ts @@ -220,3 +220,7 @@ export function CHANNEL_MESSAGE_REACTION( export function CHANNEL_MESSAGE_CROSSPOST(channelId: Snowflake, messageId: Snowflake) { return `/channels/${channelId}/messages/${messageId}/crosspost`; } + +export function GUILD_MEMBER_ROLE(guildId: Snowflake, memberId: Snowflake, roleId: Snowflake) { + return `/guilds/${guildId}/members/${memberId}/roles/${roleId}`; +}