/
messages.ts
124 lines (100 loc) · 3.79 KB
/
messages.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
import type { Client } from '../client/mod.ts'
import { Message } from '../structures/message.ts'
import type { TextChannel } from '../structures/textChannel.ts'
import { User } from '../structures/user.ts'
import type { MessagePayload } from '../types/channel.ts'
import { CHANNEL_MESSAGE } from '../types/endpoint.ts'
import { Snowflake } from '../utils/snowflake.ts'
import { BaseManager } from './base.ts'
export class MessagesManager extends BaseManager<MessagePayload, Message> {
channel: TextChannel
constructor(client: Client, channel: TextChannel) {
super(client, `messages:${channel.id}`, Message)
this.channel = channel
}
async get(key: string): Promise<Message | undefined> {
const raw = await this._get(key)
if (raw === undefined) return
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)
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 this.DataType(this.client, raw, channel, author)
await res.mentions.fromPayload(raw)
if (typeof raw.guild_id === 'string')
res.guild = await this.client.guilds.get(raw.guild_id)
if (typeof res.guild === 'object')
res.member = await res.guild.members.get(raw.author.id)
return res
}
async set(key: string, value: MessagePayload): Promise<void> {
await this.client.cache.set(
this.cacheName,
key,
value,
this.client.messageCacheLifetime
)
const keys = (await this.client.cache.keys(this.cacheName)) ?? []
if (keys.length > this.client.messageCacheMax) {
const sorted = keys.sort(
(b, a) => new Snowflake(a).timestamp - new Snowflake(b).timestamp
)
const toRemove = sorted.filter((_, i) => i >= this.client.messageCacheMax)
await this.client.cache.delete(this.cacheName, ...toRemove)
}
}
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
.get(CHANNEL_MESSAGE(this.channel.id, id))
.then(async (data) => {
await this.set(id, data as MessagePayload)
let channel = await this.client.channels.get<TextChannel>(
this.channel.id
)
if (channel === undefined)
channel = await this.client.channels.fetch(this.channel.id)
await this.client.users.set(
data.author.id,
(data as MessagePayload).author
)
const res = (await this.get(data.id)) as Message
await res.mentions.fromPayload(data)
resolve(res)
})
.catch((e) => reject(e))
})
}
}