harmony/src/managers/members.ts

54 lines
2.1 KiB
TypeScript
Raw Normal View History

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'
import { Permissions } from "../utils/permissions.ts"
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)
this.guild = guild
}
async get(key: string): Promise<Member | undefined> {
const raw = await this._get(key)
if (raw === undefined) return
const user = new User(this.client, raw.user)
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)
return res
}
2020-11-03 09:21:29 +00:00
async fetch(id: string): Promise<Member> {
return await new Promise((resolve, reject) => {
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)
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)
resolve(res)
2020-11-03 09:21:29 +00:00
}).catch(e => reject(e))
})
}
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) {
await this.set(member.user.id, member)
}
}
}