-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathReaction.ts
More file actions
83 lines (73 loc) · 2.25 KB
/
Reaction.ts
File metadata and controls
83 lines (73 loc) · 2.25 KB
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
import type { APIReaction } from 'discord-api-types/v10';
import { Structure } from '../Structure.js';
import { kBurstColors, kData } from '../utils/symbols.js';
import type { Partialize } from '../utils/types.js';
/**
* Represents a reaction on a message on Discord.
*
* @typeParam Omitted - Specify the properties that will not be stored in the raw data field as a union, implement via `DataTemplate`
* @remarks has substructures `Emoji`, `ReactionCountDetails` which need to be instantiated and stored by an extending class using it
*/
export class Reaction<Omitted extends keyof APIReaction | '' = ''> extends Structure<APIReaction, Omitted> {
/**
* The template used for removing data from the raw data stored for each Reaction.
*
* @remarks This template has defaults, if you want to remove additional data and keep the defaults,
* use `Object.defineProperties`. To override the defaults, set this value directly.
*/
public static override DataTemplate: Partial<APIReaction> = {
set burst_colors(_: string[]) {},
};
protected [kBurstColors]: number[] | null = null;
/**
* @param data - The raw data received from the API for the reaction
*/
public constructor(data: Partialize<APIReaction, Omitted>) {
super(data);
this.optimizeData(data);
}
/**
* {@inheritDoc Structure.optimizeData}
*
* @internal
*/
protected override optimizeData(data: Partial<APIReaction>) {
if (data.burst_colors) {
this[kBurstColors] = data.burst_colors.map((color) => Number.parseInt(color, 16));
}
}
/**
* The amount how often this emoji has been used to react (including super reacts)
*/
public get count() {
return this[kData].count;
}
/**
* Whether the current user has reacted using this emoji
*/
public get me() {
return this[kData].me;
}
/**
* Whether the current user has super-reacted using this emoji
*/
public get meBurst() {
return this[kData].me_burst;
}
/**
* The colors used for super reaction
*/
public get burstColors() {
return this[kBurstColors];
}
/**
* {@inheritDoc Structure.toJSON}
*/
public override toJSON() {
const clone = super.toJSON();
if (this[kBurstColors]) {
clone.burst_colors = this[kBurstColors].map((color) => `#${color.toString(16).padStart(6, '0')}`);
}
return clone;
}
}