2020-12-02 12:29:52 +00:00
|
|
|
import { MemberRolesManager } from '../managers/memberRoles.ts'
|
2021-04-04 05:42:15 +00:00
|
|
|
import { Client } from '../client/mod.ts'
|
2020-12-02 12:29:52 +00:00
|
|
|
import { GUILD_MEMBER } from '../types/endpoint.ts'
|
|
|
|
import { MemberPayload } from '../types/guild.ts'
|
|
|
|
import { Permissions } from '../utils/permissions.ts'
|
2021-01-25 11:28:46 +00:00
|
|
|
import { SnowflakeBase } from './base.ts'
|
2020-12-02 12:29:52 +00:00
|
|
|
import { Guild } from './guild.ts'
|
2021-03-30 16:43:01 +00:00
|
|
|
import { VoiceChannel } from './guildVoiceChannel.ts'
|
2020-12-02 12:29:52 +00:00
|
|
|
import { Role } from './role.ts'
|
|
|
|
import { User } from './user.ts'
|
|
|
|
|
|
|
|
export interface MemberData {
|
|
|
|
nick?: string | null
|
|
|
|
roles?: Array<Role | string>
|
|
|
|
deaf?: boolean
|
|
|
|
mute?: boolean
|
2021-03-30 16:43:01 +00:00
|
|
|
channel?: string | VoiceChannel | null
|
2020-12-02 12:29:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 11:28:46 +00:00
|
|
|
export class Member extends SnowflakeBase {
|
2020-12-02 12:29:52 +00:00
|
|
|
id: string
|
|
|
|
user: User
|
2021-01-27 19:10:11 +00:00
|
|
|
nick: string | null
|
2020-12-02 12:29:52 +00:00
|
|
|
roles: MemberRolesManager
|
|
|
|
joinedAt: string
|
|
|
|
premiumSince?: string
|
|
|
|
deaf: boolean
|
|
|
|
mute: boolean
|
|
|
|
guild: Guild
|
|
|
|
permissions: Permissions
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
client: Client,
|
|
|
|
data: MemberPayload,
|
|
|
|
user: User,
|
|
|
|
guild: Guild,
|
|
|
|
perms?: Permissions
|
|
|
|
) {
|
|
|
|
super(client)
|
|
|
|
this.id = data.user.id
|
|
|
|
this.user = user
|
|
|
|
this.nick = data.nick
|
|
|
|
this.guild = guild
|
|
|
|
this.roles = new MemberRolesManager(this.client, this.guild.roles, this)
|
|
|
|
this.joinedAt = data.joined_at
|
|
|
|
this.premiumSince = data.premium_since
|
|
|
|
this.deaf = data.deaf
|
|
|
|
this.mute = data.mute
|
|
|
|
if (perms !== undefined) this.permissions = perms
|
|
|
|
else this.permissions = new Permissions(Permissions.DEFAULT)
|
|
|
|
}
|
|
|
|
|
|
|
|
get displayName(): string {
|
2021-01-27 19:10:11 +00:00
|
|
|
return this.nick !== null ? this.nick : this.user.username
|
2020-12-02 12:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return this.user.nickMention
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:28:20 +00:00
|
|
|
readFromData(data: MemberPayload): void {
|
2020-12-02 12:29:52 +00:00
|
|
|
this.nick = data.nick ?? this.nick
|
|
|
|
this.joinedAt = data.joined_at ?? this.joinedAt
|
|
|
|
this.premiumSince = data.premium_since ?? this.premiumSince
|
|
|
|
this.deaf = data.deaf ?? this.deaf
|
|
|
|
this.mute = data.mute ?? this.mute
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Updates the Member data in cache (and this object).
|
2020-12-02 12:29:52 +00:00
|
|
|
*/
|
|
|
|
async fetch(): Promise<Member> {
|
|
|
|
const raw = await this.client.rest.get(this.id)
|
|
|
|
if (typeof raw !== 'object') throw new Error('Member not found')
|
|
|
|
await this.guild.members.set(this.id, raw)
|
|
|
|
this.readFromData(raw)
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Edits the Member
|
2020-12-02 12:29:52 +00:00
|
|
|
* @param data Data to apply
|
|
|
|
*/
|
|
|
|
async edit(data: MemberData): Promise<Member> {
|
|
|
|
const payload = {
|
|
|
|
nick: data.nick,
|
|
|
|
roles: data.roles?.map((e) => (typeof e === 'string' ? e : e.id)),
|
|
|
|
deaf: data.deaf,
|
2021-03-30 16:43:01 +00:00
|
|
|
mute: data.mute,
|
|
|
|
channel_id:
|
2021-03-30 17:00:25 +00:00
|
|
|
data.channel instanceof VoiceChannel ? data.channel.id : data.channel
|
2020-12-02 12:29:52 +00:00
|
|
|
}
|
2021-03-30 16:43:01 +00:00
|
|
|
|
2020-12-02 12:29:52 +00:00
|
|
|
const res = await this.client.rest.patch(
|
|
|
|
GUILD_MEMBER(this.guild.id, this.id),
|
|
|
|
payload,
|
|
|
|
undefined,
|
|
|
|
null,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
if (res.ok === true) {
|
|
|
|
if (data.nick !== undefined)
|
2021-01-27 19:10:11 +00:00
|
|
|
this.nick = data.nick === null ? null : data.nick
|
2020-12-02 12:29:52 +00:00
|
|
|
if (data.deaf !== undefined) this.deaf = data.deaf
|
|
|
|
if (data.mute !== undefined) this.mute = data.mute
|
|
|
|
}
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* New nickname to set. If empty, nick is reset
|
|
|
|
* @param nick New nickname
|
|
|
|
*/
|
|
|
|
async setNickname(nick?: string): Promise<Member> {
|
|
|
|
return await this.edit({
|
|
|
|
nick: nick === undefined ? null : nick
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Resets nickname of the Member
|
2020-12-02 12:29:52 +00:00
|
|
|
*/
|
|
|
|
async resetNickname(): Promise<Member> {
|
|
|
|
return await this.setNickname()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Sets a Member mute in VC
|
2020-12-02 12:29:52 +00:00
|
|
|
* @param mute Value to set
|
|
|
|
*/
|
|
|
|
async setMute(mute?: boolean): Promise<Member> {
|
|
|
|
return await this.edit({
|
2021-03-30 16:43:01 +00:00
|
|
|
mute: mute ?? false
|
2020-12-02 12:29:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Sets a Member deaf in VC
|
2020-12-02 12:29:52 +00:00
|
|
|
* @param deaf Value to set
|
|
|
|
*/
|
|
|
|
async setDeaf(deaf?: boolean): Promise<Member> {
|
|
|
|
return await this.edit({
|
2021-03-30 16:43:01 +00:00
|
|
|
deaf: deaf ?? false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves a Member to another VC
|
|
|
|
* @param channel Channel to move(null or undefined to disconnect)
|
|
|
|
*/
|
|
|
|
async moveVoiceChannel(
|
|
|
|
channel?: string | VoiceChannel | null
|
|
|
|
): Promise<Member> {
|
|
|
|
return await this.edit({
|
|
|
|
channel: channel ?? null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnects a Member from connected VC
|
|
|
|
*/
|
|
|
|
async disconnectVoice(): Promise<Member> {
|
|
|
|
return await this.edit({
|
|
|
|
channel: null
|
2020-12-02 12:29:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Unmutes the Member from VC.
|
2020-12-02 12:29:52 +00:00
|
|
|
*/
|
|
|
|
async unmute(): Promise<Member> {
|
|
|
|
return await this.setMute(false)
|
|
|
|
}
|
|
|
|
|
2021-03-30 16:43:01 +00:00
|
|
|
/**
|
|
|
|
* Undeafs the Member from VC.
|
|
|
|
*/
|
|
|
|
async undeaf(): Promise<Member> {
|
|
|
|
return await this.setDeaf(false)
|
|
|
|
}
|
|
|
|
|
2020-12-02 12:29:52 +00:00
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Kicks the member.
|
2020-12-02 12:29:52 +00:00
|
|
|
*/
|
|
|
|
async kick(): Promise<boolean> {
|
|
|
|
const resp = await this.client.rest.delete(
|
|
|
|
GUILD_MEMBER(this.guild.id, this.id),
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
null,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
if (resp.ok !== true) return false
|
|
|
|
else return true
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Bans the Member.
|
2020-12-02 12:29:52 +00:00
|
|
|
* @param reason Reason for the Ban.
|
2020-12-28 21:44:51 +00:00
|
|
|
* @param deleteOldMessages Delete Old Messages? If yes, how much days.
|
2020-12-02 12:29:52 +00:00
|
|
|
*/
|
|
|
|
async ban(reason?: string, deleteOldMessages?: number): Promise<void> {
|
|
|
|
return this.guild.bans.add(this.id, reason, deleteOldMessages)
|
|
|
|
}
|
|
|
|
}
|