2020-11-08 07:57:24 +00:00
|
|
|
import { User } from '../structures/user.ts'
|
|
|
|
import { Client } from '../models/client.ts'
|
|
|
|
import { Guild } from '../structures/guild.ts'
|
|
|
|
import { Member } from '../structures/member.ts'
|
|
|
|
import { GUILD_MEMBER } from '../types/endpoint.ts'
|
|
|
|
import { MemberPayload } from '../types/guild.ts'
|
|
|
|
import { BaseManager } from './base.ts'
|
2020-11-15 07:32:46 +00:00
|
|
|
import { Permissions } from "../utils/permissions.ts"
|
2020-11-02 12:08:38 +00:00
|
|
|
|
|
|
|
export class MembersManager extends BaseManager<MemberPayload, Member> {
|
|
|
|
guild: Guild
|
|
|
|
|
|
|
|
constructor(client: Client, guild: Guild) {
|
2020-11-03 09:21:29 +00:00
|
|
|
super(client, `members:${guild.id}`, Member)
|
2020-11-02 12:08:38 +00:00
|
|
|
this.guild = guild
|
|
|
|
}
|
|
|
|
|
2020-11-15 07:32:46 +00:00
|
|
|
async get(key: string): Promise<Member | undefined> {
|
2020-11-07 13:05:37 +00:00
|
|
|
const raw = await this._get(key)
|
|
|
|
if (raw === undefined) return
|
|
|
|
const user = new User(this.client, raw.user)
|
2020-11-15 07:32:46 +00:00
|
|
|
const roles = await this.guild.roles.array()
|
|
|
|
let permissions = new Permissions(Permissions.DEFAULT)
|
|
|
|
if (roles !== undefined) {
|
|
|
|
const mRoles = roles.filter(r => raw.roles.includes(r.id) as boolean || r.id === this.guild.id)
|
|
|
|
permissions = new Permissions(mRoles.map(r => r.permissions))
|
|
|
|
}
|
|
|
|
const res = new this.DataType(this.client, raw, user, this.guild, permissions)
|
2020-11-07 13:05:37 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:21:29 +00:00
|
|
|
async fetch(id: string): Promise<Member> {
|
|
|
|
return await new Promise((resolve, reject) => {
|
2020-11-07 13:05:37 +00:00
|
|
|
this.client.rest.get(GUILD_MEMBER(this.guild.id, id)).then(async data => {
|
|
|
|
await this.set(id, data as MemberPayload)
|
|
|
|
const user: User = new User(this.client, data.user)
|
2020-11-15 07:32:46 +00:00
|
|
|
const roles = await this.guild.roles.array()
|
|
|
|
let permissions = new Permissions(Permissions.DEFAULT)
|
|
|
|
if (roles !== undefined) {
|
|
|
|
const mRoles = roles.filter(r => data.roles.includes(r.id) as boolean || r.id === this.guild.id)
|
|
|
|
permissions = new Permissions(mRoles.map(r => r.permissions))
|
|
|
|
}
|
|
|
|
const res = new Member(this.client, data as MemberPayload, user, this.guild, permissions)
|
2020-11-07 13:05:37 +00:00
|
|
|
resolve(res)
|
2020-11-03 09:21:29 +00:00
|
|
|
}).catch(e => reject(e))
|
2020-11-02 12:08:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:21:29 +00:00
|
|
|
async fromPayload(members: MemberPayload[]): Promise<void> {
|
2020-11-03 15:19:20 +00:00
|
|
|
for (const member of members) {
|
2020-11-02 12:08:38 +00:00
|
|
|
await this.set(member.user.id, member)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|