Fix managers' array function
This commit is contained in:
parent
920cf133bc
commit
a04f6ab973
8 changed files with 126 additions and 6 deletions
|
@ -42,7 +42,7 @@ export class BaseManager<T, T2> {
|
|||
}
|
||||
|
||||
/** Gets an Array of values from Cache */
|
||||
async array(): Promise<undefined | T2[]> {
|
||||
async array(): Promise<T2[]> {
|
||||
let arr = await (this.client.cache.array(this.cacheName) as T[])
|
||||
if (arr === undefined) arr = []
|
||||
return arr.map((e) => new this.DataType(this.client, e)) as any
|
||||
|
|
|
@ -25,7 +25,7 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
|
|||
return res as any
|
||||
}
|
||||
|
||||
async array(): Promise<undefined | Channel[]> {
|
||||
async array(): Promise<Channel[]> {
|
||||
const arr = await (this.client.cache.array(
|
||||
this.cacheName
|
||||
) as ChannelPayload[])
|
||||
|
|
|
@ -29,7 +29,7 @@ export class GuildVoiceStatesManager extends BaseManager<
|
|||
return new VoiceState(this.client, raw, {
|
||||
user: ((await this.client.users.get(raw.user_id)) as unknown) as User,
|
||||
channel:
|
||||
raw.channel_id == null
|
||||
raw.channel_id === null
|
||||
? null
|
||||
: (((await this.client.channels.get<VoiceChannel>(
|
||||
raw.channel_id
|
||||
|
@ -40,6 +40,37 @@ export class GuildVoiceStatesManager extends BaseManager<
|
|||
})
|
||||
}
|
||||
|
||||
async array(): Promise<VoiceState[]> {
|
||||
let arr = await (this.client.cache.array(
|
||||
this.cacheName
|
||||
) as VoiceStatePayload[])
|
||||
if (arr === undefined) arr = []
|
||||
|
||||
return await Promise.all(
|
||||
arr.map(async (raw) => {
|
||||
const guild =
|
||||
raw.guild_id === undefined
|
||||
? undefined
|
||||
: await this.client.guilds.get(raw.guild_id)
|
||||
|
||||
return new VoiceState(this.client, raw, {
|
||||
user: ((await this.client.users.get(raw.user_id)) as unknown) as User,
|
||||
channel:
|
||||
raw.channel_id === null
|
||||
? null
|
||||
: (((await this.client.channels.get<VoiceChannel>(
|
||||
raw.channel_id
|
||||
)) as unknown) as VoiceChannel),
|
||||
guild,
|
||||
member:
|
||||
guild === undefined
|
||||
? undefined
|
||||
: await guild.members.get(raw.user_id)
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
async fromPayload(d: VoiceStatePayload[]): Promise<void> {
|
||||
for (const data of d) {
|
||||
await this.set(data.user_id, data)
|
||||
|
|
|
@ -37,6 +37,27 @@ export class MembersManager extends BaseManager<MemberPayload, Member> {
|
|||
return res
|
||||
}
|
||||
|
||||
async array(): Promise<Member[]> {
|
||||
let arr = await (this.client.cache.array(this.cacheName) as MemberPayload[])
|
||||
if (arr === undefined) arr = []
|
||||
|
||||
return await Promise.all(
|
||||
arr.map(async (raw) => {
|
||||
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))
|
||||
}
|
||||
return new Member(this.client, raw, user, this.guild, permissions)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
async fetch(id: string): Promise<Member> {
|
||||
return await new Promise((resolve, reject) => {
|
||||
this.client.rest
|
||||
|
|
|
@ -37,6 +37,20 @@ export class MessageReactionsManager extends BaseManager<
|
|||
)
|
||||
}
|
||||
|
||||
async array(): Promise<MessageReaction[]> {
|
||||
let arr = await (this.client.cache.array(this.cacheName) as Reaction[])
|
||||
if (arr === undefined) arr = []
|
||||
|
||||
return await Promise.all(
|
||||
arr.map(async (raw) => {
|
||||
let emoji = await this.client.emojis.get(raw.emoji.id)
|
||||
if (emoji === undefined) emoji = new Emoji(this.client, raw.emoji)
|
||||
|
||||
return new MessageReaction(this.client, raw, this.message, emoji)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
async flush(): Promise<any> {
|
||||
await this.client.cache.deleteCache(`reaction_users:${this.message.id}`)
|
||||
return this.client.cache.deleteCache(this.cacheName)
|
||||
|
|
|
@ -44,6 +44,42 @@ export class MessagesManager extends BaseManager<MessagePayload, Message> {
|
|||
)
|
||||
}
|
||||
|
||||
async array(): Promise<Message[]> {
|
||||
let arr = await (this.client.cache.array(
|
||||
this.cacheName
|
||||
) as MessagePayload[])
|
||||
if (arr === undefined) arr = []
|
||||
|
||||
const result: Message[] = []
|
||||
await Promise.all(
|
||||
arr.map(async (raw) => {
|
||||
if (raw.author === undefined) return
|
||||
|
||||
let channel = await this.client.channels.get(raw.channel_id)
|
||||
if (channel === undefined)
|
||||
channel = await this.client.channels.fetch(raw.channel_id)
|
||||
if (channel === undefined) return
|
||||
|
||||
let author = ((await this.client.users.get(
|
||||
raw.author.id
|
||||
)) as unknown) as User
|
||||
|
||||
if (author === undefined) author = new User(this.client, raw.author)
|
||||
|
||||
const res = new Message(
|
||||
this.client,
|
||||
raw,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
||||
channel as TextChannel,
|
||||
author
|
||||
)
|
||||
await res.mentions.fromPayload(raw)
|
||||
result.push(res)
|
||||
})
|
||||
)
|
||||
return result
|
||||
}
|
||||
|
||||
async fetch(id: string): Promise<Message> {
|
||||
return await new Promise((resolve, reject) => {
|
||||
this.client.rest
|
||||
|
|
|
@ -27,6 +27,25 @@ export class GuildPresencesManager extends BaseManager<
|
|||
return presence
|
||||
}
|
||||
|
||||
async array(): Promise<Presence[]> {
|
||||
let arr = await (this.client.cache.array(
|
||||
this.cacheName
|
||||
) as PresenceUpdatePayload[])
|
||||
if (arr === undefined) arr = []
|
||||
|
||||
const result: Presence[] = []
|
||||
await Promise.all(
|
||||
arr.map(async (raw) => {
|
||||
let user = await this.client.users.get(raw.user.id)
|
||||
if (user === undefined) user = new User(this.client, raw.user)
|
||||
const guild = await this.client.guilds.get(raw.guild_id)
|
||||
if (guild === undefined) return
|
||||
result.push(new Presence(this.client, raw, user, guild))
|
||||
})
|
||||
)
|
||||
return result
|
||||
}
|
||||
|
||||
async fromPayload(
|
||||
data: PresenceUpdatePayload[]
|
||||
): Promise<GuildPresencesManager> {
|
||||
|
|
|
@ -59,10 +59,9 @@ client.on('messageCreate', async (msg: Message) => {
|
|||
if (msg.content === '!ping') {
|
||||
msg.reply(`Pong! Ping: ${client.ping}ms`)
|
||||
} else if (msg.content === '!members') {
|
||||
const col = await msg.guild?.members.collection()
|
||||
const col = await msg.guild?.members.array()
|
||||
const data = col
|
||||
?.array()
|
||||
.map((c: Member, i: number) => {
|
||||
?.map((c: Member, i: number) => {
|
||||
return `${i + 1}. ${c.user.tag}`
|
||||
})
|
||||
.join('\n') as string
|
||||
|
|
Loading…
Reference in a new issue