feat: all role-related endpoints

This commit is contained in:
Yuzu 2022-07-01 16:50:16 -05:00
parent b99218bdc3
commit 5b0d124380
4 changed files with 59 additions and 0 deletions

View File

@ -81,6 +81,11 @@ export interface BeginGuildPrune {
includeRoles?: Snowflake[]; includeRoles?: Snowflake[];
} }
export interface ModifyRolePositions {
id: Snowflake;
position?: number | null;
}
/** /**
* Represents a guild * Represents a guild
* @link https://discord.com/developers/docs/resources/guild#guild-object * @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); return new Role(this.session, role, this.id);
} }
async addRole(memberId: Snowflake, roleId: Snowflake, { reason }: { reason?: string } = {}) {
await this.session.rest.runMethod<undefined>(
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<undefined>(
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<DiscordRole[]>(
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<void> { async deleteInvite(inviteCode: string): Promise<void> {
await this.session.rest.runMethod<undefined>( await this.session.rest.runMethod<undefined>(
this.session.rest, this.session.rest,

View File

@ -82,6 +82,14 @@ export class Member implements Model {
return member; 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 */ /** gets the user's avatar */
avatarUrl(options: { format?: ImageFormat; size?: ImageSize } = { size: 128 }) { avatarUrl(options: { format?: ImageFormat; size?: ImageSize } = { size: 128 }) {
let url: string; let url: string;

View File

@ -56,6 +56,14 @@ export class Role implements Model {
return role; 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() { toString() {
switch (this.id) { switch (this.id) {
case this.guildId: case this.guildId:

View File

@ -220,3 +220,7 @@ export function CHANNEL_MESSAGE_REACTION(
export function CHANNEL_MESSAGE_CROSSPOST(channelId: Snowflake, messageId: Snowflake) { export function CHANNEL_MESSAGE_CROSSPOST(channelId: Snowflake, messageId: Snowflake) {
return `/channels/${channelId}/messages/${messageId}/crosspost`; return `/channels/${channelId}/messages/${messageId}/crosspost`;
} }
export function GUILD_MEMBER_ROLE(guildId: Snowflake, memberId: Snowflake, roleId: Snowflake) {
return `/guilds/${guildId}/members/${memberId}/roles/${roleId}`;
}