Skip to content

Commit

Permalink
Merge pull request #43 from pakkographic/dev
Browse files Browse the repository at this point in the history
Add member methods to Client, Guild & Member.
  • Loading branch information
pakkographic authored Nov 23, 2022
2 parents 997d8b2 + 2022c66 commit ec3d90a
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 2 deletions.
99 changes: 97 additions & 2 deletions lib/routes/Guilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ import {
PUTGuildMemberXPBody,
PUTGuildMemberXPResponse,
PUTGuildWebhookResponse,
PATCHChannelResponse
PATCHChannelResponse,
POSTGuildBanResponse,
GETGuildMembersResponse,
APIGuildMember,
GETGuildBanResponse,
GETGuildBansResponse
} from "../Constants";
import { CreateChannelOptions, EditChannelOptions } from "../types/channel";
import { EditWebhookOptions } from "../types/webhooks";
import { EditMemberOptions } from "../types/guilds";
import { BannedMember } from "../structures/BannedMember";

export class Guilds {
#manager: RESTManager;
Expand Down Expand Up @@ -71,7 +78,7 @@ export class Guilds {

/** This method is used to get a specific guild member.
* @param guildID The ID of the Guild.
* @param memberID The ID of the Guild Member you'd like to get.
* @param memberID The ID of the Guild Member to get.
*/
async getMember(guildID: string, memberID: string): Promise<Member>{
return this.#manager.authRequest<GETGuildMemberResponse>({
Expand All @@ -80,6 +87,37 @@ export class Guilds {
}).then(data => new Member(data.member, this.#manager.client, guildID));
}

/** This method is used to get a list of guild member.
* @param guildID ID of the guild to get members.
*/
async getMembers(guildID: string): Promise<Array<Member>> {
return this.#manager.authRequest<GETGuildMembersResponse>({
method: "GET",
path: endpoints.GUILD_MEMBERS(guildID)
}).then(data => data.members.map(d => new Member(d as APIGuildMember, this.#manager.client, guildID)));
}

/** Get a ban.
* @param guildID ID of the guild.
* @param memberID ID of the banned member.
*/
async getBan(guildID: string, memberID: string): Promise<BannedMember> {
return this.#manager.authRequest<GETGuildBanResponse>({
method: "GET",
path: endpoints.GUILD_BAN(guildID, memberID)
}).then(data => new BannedMember(guildID, data.serverMemberBan, this.#manager.client));
}

/** This method is used to get a list of guild ban.
* @param guildID ID of the guild.
*/
async getBans(guildID: string): Promise<Array<BannedMember>> {
return this.#manager.authRequest<GETGuildBansResponse>({
method: "GET",
path: endpoints.GUILD_BANS(guildID)
}).then(data => data.serverMemberBans.map(d => new BannedMember(guildID, d, this.#manager.client)));
}

/** Add a member to a group
* @param groupID ID of a guild group.
* @param memberID ID of a member.
Expand Down Expand Up @@ -126,6 +164,63 @@ export class Guilds {
});
}

/** Edit a member.
* @param guildID ID of the guild the member is in.
* @param memberID ID of the the member to edit.
* @param options Edit options.
*/
async editMember(guildID: string, memberID: string, options: EditMemberOptions): Promise<void> {
if (options.nickname) {
return this.#manager.authRequest<void>({
method: "PUT",
path: endpoints.MEMBER_NICKNAME(guildID, memberID),
json: {
nickname: options.nickname
}
});
} else if (!options.nickname) {
return this.#manager.authRequest<void>({
method: "DELETE",
path: endpoints.MEMBER_NICKNAME(guildID, memberID)
});
}
}

/** Remove a member from a guild.
* @param guildID The ID of the guild the member is in.
* @param memberID The ID of the member to kick.
*/
async removeMember(guildID: string, memberID: string): Promise<void> {
return this.#manager.authRequest<void>({
method: "DELETE",
path: endpoints.GUILD_MEMBER(guildID, memberID)
});
}

/** Ban a guild member.
* @param guildID ID of the guild the member is in.
* @param memberID ID of the member to ban.
* @param reason The reason of the ban.
*/
async createBan(guildID: string, memberID: string, reason?: string): Promise<BannedMember> {
return this.#manager.authRequest<POSTGuildBanResponse>({
method: "POST",
path: endpoints.GUILD_BAN(guildID, memberID),
json: { reason }
}).then(data => new BannedMember(guildID, data.serverMemberBan, this.#manager.client));
}

/** Unban a guild member.
* @param guildID ID of the guild the member was in.
* @param memberID ID of the member to unban.
*/
async removeBan(guildID: string, memberID: string): Promise<void> {
return this.#manager.authRequest<void>({
method: "DELETE",
path: endpoints.GUILD_BAN(guildID, memberID)
});
}

/** Create a webhook
* @param guildID ID of a guild.
* @param channelID ID of a channel.
Expand Down
58 changes: 58 additions & 0 deletions lib/structures/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ListItem } from "./ListItem";
import { UserClient } from "./UserClient";
import { ForumThreadComment } from "./ForumThreadComment";
import { User } from "./User";
import { BannedMember } from "./BannedMember";
import { WSManager } from "../gateway/WSManager";
import { GatewayHandler } from "../gateway/GatewayHandler";
import { RESTManager } from "../rest/RESTManager";
Expand All @@ -37,6 +38,7 @@ import { CreateForumThreadOptions, EditForumThreadOptions, GetForumThreadsFilter
import { CreateForumCommentOptions, EditForumCommentOptions } from "../types/forumThreadComment";
import { CreateDocOptions, EditDocOptions, GetDocsFilter } from "../types/doc";
import { CreateCalendarEventOptions, EditCalendarEventOptions, EditCalendarRSVPOptions, GetCalendarEventsFilter } from "../types/calendarEvent";
import { EditMemberOptions } from "../types/guilds";

/** Represents the bot's client. */
export class Client extends TypedEmitter<ClientEvents> {
Expand Down Expand Up @@ -122,6 +124,28 @@ export class Client extends TypedEmitter<ClientEvents> {
return rMember;
}

/** This method is used to get a list of guild member.
* @param guildID ID of the guild to get members.
*/
async getMembers(guildID: string): Promise<Array<Member>> {
return this.rest.guilds.getMembers(guildID);
}

/** Get a ban.
* @param guildID ID of the guild.
* @param memberID ID of the banned member.
*/
async getBan(guildID: string, memberID: string): Promise<BannedMember> {
return this.rest.guilds.getBan(guildID, memberID);
}

/** This method is used to get a list of guild ban.
* @param guildID ID of the guild.
*/
async getBans(guildID: string): Promise<Array<BannedMember>> {
return this.rest.guilds.getBans(guildID);
}

/** This method is used to get a specific Guild.
*
* Note: Guild = Server
Expand Down Expand Up @@ -584,6 +608,40 @@ export class Client extends TypedEmitter<ClientEvents> {
return this.rest.guilds.memberRemoveRole(guildID, memberID, roleID);
}

/** Edit a member.
* @param guildID ID of the guild the member is in.
* @param memberID ID of the the member to edit.
* @param options Edit options.
*/
async editMember(guildID: string, memberID: string, options: EditMemberOptions): Promise<void> {
return this.rest.guilds.editMember(guildID, memberID, options);
}

/** Remove a member from a guild.
* @param guildID The ID of the guild the member is in.
* @param memberID The ID of the member to kick.
*/
async removeMember(guildID: string, memberID: string): Promise<void> {
return this.rest.guilds.removeMember(guildID, memberID);
}

/** Ban a guild member.
* @param guildID ID of the guild the member is in.
* @param memberID ID of the member to ban.
* @param reason The reason of the ban.
*/
async createBan(guildID: string, memberID: string, reason?: string): Promise<BannedMember> {
return this.rest.guilds.createBan(guildID, memberID, reason);
}

/** Unban a guild member.
* @param guildID ID of the guild the member was in.
* @param memberID ID of the member to unban.
*/
async removeBan(guildID: string, memberID: string): Promise<void> {
return this.rest.guilds.removeBan(guildID, memberID);
}

/** Create a webhook
* @param guildID ID of a guild.
* @param channelID ID of a channel.
Expand Down
16 changes: 16 additions & 0 deletions lib/structures/Guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Base } from "./Base";
import { Channel } from "./Channel";
import { Member } from "./Member";
import { User } from "./User";
import { BannedMember } from "./BannedMember";
import { APIGuild } from "../Constants";

/** Represents a Guild, also called server. */
Expand Down Expand Up @@ -67,4 +68,19 @@ export class Guild extends Base {
async getChannel(channelID: string): Promise<Channel>{
return this.client.rest.channels.getChannel(channelID);
}

/** Ban a member.
* @param memberID ID of the member to ban.
* @param reason The reason of the ban.
*/
async createBan(memberID: string, reason?: string): Promise<BannedMember> {
return this.client.rest.guilds.createBan(this.id as string, memberID, reason);
}

/** Unban a member.
* @param memberID ID of the member to unban.
*/
async removeBan(memberID: string): Promise<void> {
return this.client.rest.guilds.removeBan(this.id as string, memberID);
}
}
26 changes: 26 additions & 0 deletions lib/structures/Member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import { Client } from "./Client";
import { User } from "./User";
import { Guild } from "./Guild";
import { BannedMember } from "./BannedMember";
import { GetSocialLink } from "../types/types";
import { APIGuildMember } from "../Constants";
import { EditMemberOptions } from "../types/guilds";

/** Represents a guild user. */
export class Member extends User {
Expand Down Expand Up @@ -45,6 +47,30 @@ export class Member extends User {
return new User(this._data.user, this.client);
}

/** Edit this member.
* @param options Edit options.
*/
async edit(options: EditMemberOptions): Promise<void> {
return this.client.rest.guilds.editMember(this.guildID, this.id as string, options);
}

/** Kick this member. */
async kick(): Promise<void> {
return this.client.rest.guilds.removeMember(this.guildID, this.id as string);
}

/** Ban this member.
* @param reason The reason of the ban.
*/
async ban(reason?: string): Promise<BannedMember> {
return this.client.rest.guilds.createBan(this.guildID, this.id as string, reason);
}

/** Unban this member. */
async unban(): Promise<void> {
return this.client.rest.guilds.removeBan(this.guildID, this.id as string);
}

/** Get a specified social link from the member, if member is connected to them through Guilded.
* @param socialMediaName Name of a social media linked to this member.
*/
Expand Down
4 changes: 4 additions & 0 deletions lib/types/guilds.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface EditMemberOptions {
/** The nickname of the member. */
nickname: string | null;
}

0 comments on commit ec3d90a

Please sign in to comment.