-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
messageReactions.ts
125 lines (109 loc) · 3.76 KB
/
messageReactions.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
125
import type { Client } from '../client/mod.ts'
import { Emoji } from '../structures/emoji.ts'
import type { Message } from '../structures/message.ts'
import { MessageReaction } from '../structures/messageReaction.ts'
import type { User } from '../structures/user.ts'
import type { Reaction } from '../types/channel.ts'
import {
MESSAGE_REACTION,
MESSAGE_REACTIONS,
MESSAGE_REACTION_USER
} from '../types/endpoint.ts'
import { BaseManager } from './base.ts'
export class MessageReactionsManager extends BaseManager<
Reaction,
MessageReaction
> {
message: Message
constructor(client: Client, message: Message) {
super(client, `reactions:${message.id}`, MessageReaction)
this.message = message
}
async updateRefs(): Promise<void> {
const newVal = await this.message.channel.messages.get(this.message.id)
if (newVal !== undefined) {
this.message = newVal
}
await this.message.updateRefs()
}
async get(id: string): Promise<MessageReaction | undefined> {
const raw = await this._get(id)
if (raw === undefined) return
const emojiID = raw.emoji.id !== null ? raw.emoji.id : raw.emoji.name
let emoji = await this.client.emojis.get(emojiID as string)
if (emoji === undefined) emoji = new Emoji(this.client, raw.emoji)
await this.updateRefs()
const reaction = new MessageReaction(this.client, raw, this.message, emoji)
return reaction
}
async set(key: string, value: Reaction): Promise<void> {
await this.client.cache.set(
this.cacheName,
key,
value,
this.client.reactionCacheLifetime
)
}
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) => {
const emojiID = raw.emoji.id !== null ? raw.emoji.id : raw.emoji.name
let emoji = await this.client.emojis.get(emojiID as string)
if (emoji === undefined) emoji = new Emoji(this.client, raw.emoji)
return new MessageReaction(this.client, raw, this.message, emoji)
})
)
}
async flush(): Promise<void> {
await this.client.cache.deleteCache(`reaction_users:${this.message.id}`)
await this.client.cache.deleteCache(this.cacheName)
}
/** Remove all Reactions from the Message */
async removeAll(): Promise<void> {
await this.client.rest.delete(
MESSAGE_REACTIONS(this.message.channel.id, this.message.id)
)
}
/** Remove a specific Emoji from Reactions */
async removeEmoji(emoji: Emoji | string): Promise<MessageReactionsManager> {
emoji =
emoji instanceof String && emoji[0] === '<' ? emoji.substring(1) : emoji
emoji =
emoji instanceof String && emoji[emoji.length - 1] === '>'
? emoji.substring(0, emoji.length - 2)
: emoji
const val = encodeURIComponent(
(typeof emoji === 'object' ? emoji.id ?? emoji.name : emoji) as string
)
await this.client.rest.delete(
MESSAGE_REACTION(this.message.channel.id, this.message.id, val)
)
return this
}
/** Remove a specific Emoji from Reactions */
async removeUser(
emoji: Emoji | string,
user: User | string
): Promise<MessageReactionsManager> {
emoji =
emoji instanceof String && emoji[0] === '<' ? emoji.substring(1) : emoji
emoji =
emoji instanceof String && emoji[emoji.length - 1] === '>'
? emoji.substring(0, emoji.length - 2)
: emoji
const val = encodeURIComponent(
(typeof emoji === 'object' ? emoji.id ?? emoji.name : emoji) as string
)
await this.client.rest.delete(
MESSAGE_REACTION_USER(
this.message.channel.id,
this.message.id,
val,
typeof user === 'string' ? user : user.id
)
)
return this
}
}