-
Notifications
You must be signed in to change notification settings - Fork 26
/
GuildMember.ts
253 lines (217 loc) Β· 7.82 KB
/
GuildMember.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { DiscordBase } from './extra/DiscordBase';
export type GuildMemberData =
| APIGuildMember
| GatewayGuildMemberUpdateDispatchData
| GatewayGuildMemberAddDispatchData
| APIInteractionDataResolvedGuildMember;
import type {
APIGuildMember,
APIInteractionDataResolvedGuildMember,
APIUser,
GatewayGuildMemberAddDispatchData,
GatewayGuildMemberUpdateDispatchData,
RESTGetAPIGuildMembersQuery,
RESTGetAPIGuildMembersSearchQuery,
RESTPatchAPIGuildMemberJSONBody,
RESTPutAPIGuildBanJSONBody,
RESTPutAPIGuildMemberJSONBody,
} from 'discord-api-types/v10';
import type { UsingClient } from '../commands';
import type { MakeRequired, MessageCreateBodyRequest, ObjectToLower, ToClass } from '../common';
import type { ImageOptions, MethodContext } from '../common/types/options';
import type { GuildMemberResolvable } from '../common/types/resolvables';
import { User } from './User';
import { PermissionsBitField } from './extra/Permissions';
export type GatewayGuildMemberAddDispatchDataFixed<Pending extends boolean> = Pending extends true
? Omit<GatewayGuildMemberAddDispatchData, 'user'> & { id: string }
: MakeRequired<GatewayGuildMemberAddDispatchData, 'user'>;
export interface BaseGuildMember extends DiscordBase, ObjectToLower<Omit<APIGuildMember, 'user' | 'roles'>> {}
export class BaseGuildMember extends DiscordBase {
private _roles: string[];
joinedTimestamp?: number;
communicationDisabledUntilTimestamp?: number | null;
constructor(
client: UsingClient,
data: GuildMemberData,
id: string,
/** the choosen guild id */
readonly guildId: string,
) {
const { roles, ...dataN } = data;
super(client, { ...dataN, id });
this._roles = data.roles;
this.patch(data);
}
guild(force = false) {
return this.client.guilds.fetch(this.id, force);
}
fetch(force = false) {
return this.client.members.fetch(this.guildId, this.id, force);
}
ban(body?: RESTPutAPIGuildBanJSONBody, reason?: string) {
return this.client.members.ban(this.guildId, this.id, body, reason);
}
kick(reason?: string) {
return this.client.members.kick(this.guildId, this.id, reason);
}
edit(body: RESTPatchAPIGuildMemberJSONBody, reason?: string) {
return this.client.members.edit(this.guildId, this.id, body, reason);
}
presence() {
return this.client.members.presence(this.id);
}
voice() {
return this.client.members.voice(this.guildId, this.id);
}
toString() {
return `<@${this.id}>`;
}
private patch(data: GuildMemberData) {
if ('joined_at' in data && data.joined_at) {
this.joinedTimestamp = Date.parse(data.joined_at);
}
if ('communication_disabled_until' in data) {
this.communicationDisabledUntilTimestamp = data.communication_disabled_until?.length
? Date.parse(data.communication_disabled_until)
: null;
}
}
get roles() {
return {
keys: Object.freeze(this._roles.concat(this.guildId)) as string[],
list: (force = false) =>
this.client.roles
.list(this.guildId, force)
.then(roles => roles.filter(role => this.roles.keys.includes(role.id))),
add: (id: string) => this.client.members.addRole(this.guildId, this.id, id),
remove: (id: string) => this.client.members.removeRole(this.guildId, this.id, id),
permissions: (force = false) =>
this.roles.list(force).then(roles => new PermissionsBitField(roles.map(x => BigInt(x.permissions.bits)))),
sorted: (force = false) => this.roles.list(force).then(roles => roles.sort((a, b) => b.position - a.position)),
highest: (force = false) => this.roles.sorted(force).then(roles => roles[0]),
};
}
static methods({ client, guildId }: MethodContext<{ guildId: string }>) {
return {
resolve: (resolve: GuildMemberResolvable) => client.members.resolve(guildId, resolve),
search: (query?: RESTGetAPIGuildMembersSearchQuery) => client.members.search(guildId, query),
unban: (id: string, body?: RESTPutAPIGuildBanJSONBody, reason?: string) =>
client.members.unban(guildId, id, body, reason),
ban: (id: string, body?: RESTPutAPIGuildBanJSONBody, reason?: string) =>
client.members.ban(guildId, id, body, reason),
kick: (id: string, reason?: string) => client.members.kick(guildId, id, reason),
edit: (id: string, body: RESTPatchAPIGuildMemberJSONBody, reason?: string) =>
client.members.edit(guildId, id, body, reason),
add: (id: string, body: RESTPutAPIGuildMemberJSONBody) => client.members.add(guildId, id, body),
fetch: (memberId: string, force = false) => client.members.fetch(guildId, memberId, force),
list: (query?: RESTGetAPIGuildMembersQuery, force = false) => client.members.list(guildId, query, force),
};
}
}
export interface GuildMember extends ObjectToLower<Omit<APIGuildMember, 'user' | 'roles'>> {}
/**
* Represents a guild member
* @link https://discord.com/developers/docs/resources/guild#guild-member-object
*/
export class GuildMember extends BaseGuildMember {
user: User;
private __me?: GuildMember;
constructor(
client: UsingClient,
data: GuildMemberData,
user: APIUser | User,
/** the choosen guild id */
readonly guildId: string,
) {
super(client, data, user.id, guildId);
this.user = user instanceof User ? user : new User(client, user);
}
get tag() {
return this.user.tag;
}
get bot() {
return this.user.bot;
}
get name() {
return this.user.name;
}
get username() {
return this.user.username;
}
get globalName() {
return this.user.globalName;
}
/** gets the nickname or the username */
get displayName() {
return this.nick ?? this.globalName ?? this.username;
}
dm(force = false) {
return this.user.dm(force);
}
write(body: MessageCreateBodyRequest) {
return this.user.write(body);
}
avatarURL(options?: ImageOptions) {
return this.user.avatarURL(options);
}
dynamicAvatarURL(options?: ImageOptions) {
if (!this.avatar) {
return this.user.avatarURL(options);
}
return this.rest.cdn.guilds(this.guildId).users(this.id).avatars(this.avatar).get(options);
}
bannerURL(options?: ImageOptions) {
return this.user.bannerURL(options);
}
async fetchPermissions(force = false) {
if ('permissions' in this) return this.permissions as PermissionsBitField;
return this.roles.permissions(force);
}
async manageable(force = false) {
this.__me = await this.client.guilds.fetchSelf(this.guildId, force);
const ownerId = (await this.client.guilds.fetch(this.guildId, force)).ownerId;
if (this.user.id === ownerId) return false;
if (this.user.id === this.client.botId) return false;
if (this.client.botId === ownerId) return true;
return (await this.__me!.roles.highest()).position > (await this.roles.highest(force)).position;
}
async bannable(force = false) {
return (await this.manageable(force)) && (await this.__me!.fetchPermissions(force)).has('BanMembers');
}
async kickable(force = false) {
return (await this.manageable(force)) && (await this.__me!.fetchPermissions(force)).has('KickMembers');
}
async moderatable(force = false) {
return (
!(await this.roles.permissions(force)).has('Administrator') &&
(await this.manageable(force)) &&
(await this.__me!.fetchPermissions(force)).has('KickMembers')
);
}
}
export interface UnavailableMember {
pending: true;
}
export class UnavailableMember extends BaseGuildMember {}
export interface InteractionGuildMember
extends ObjectToLower<Omit<APIInteractionDataResolvedGuildMember, 'roles' | 'deaf' | 'mute' | 'permissions'>> {}
/**
* Represents a guild member
* @link https://discord.com/developers/docs/resources/guild#guild-member-object
*/
export class InteractionGuildMember extends (GuildMember as unknown as ToClass<
Omit<GuildMember, 'deaf' | 'mute'>,
InteractionGuildMember
>) {
permissions: PermissionsBitField;
constructor(
client: UsingClient,
data: APIInteractionDataResolvedGuildMember,
user: APIUser | User,
/** the choosen guild id */
guildId: string,
) {
super(client, data, user, guildId);
this.permissions = new PermissionsBitField(Number(data.permissions));
}
}