forked from jaimeadf/BetterDiscordPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
261 lines (227 loc) · 8.38 KB
/
index.jsx
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import React from 'react';
import {
WebpackModules,
Settings,
PluginUtilities,
Toasts,
Patcher,
DiscordSelectors,
Utilities,
ReactTools
} from '@zlibrary/api';
import Plugin from '@zlibrary/plugin';
import Reactors from './components/Reactors';
import style from './style.scss';
const Reactions = WebpackModules.find(m => m?.default?.displayName === 'Reactions').default;
const { SettingPanel, SettingGroup, Textbox, Slider, Switch } = Settings;
export default class WhoReacted extends Plugin {
constructor() {
super();
this.defaultSettings = {
maxUsersShown: 6,
avatarSize: 20,
reactionThreshold: 10,
userThreshold: 100,
useHighestUserCount: true,
showSelf: true,
showBots: true
};
}
async onStart() {
PluginUtilities.addStyle(this.getName(), style);
await this.patchReaction();
}
onStop() {
PluginUtilities.removeStyle(this.getName());
Patcher.unpatchAll();
}
buildSettingsPanel() {
return new SettingPanel(
() => {
this.saveSettings();
this.forceUpdateAllReactions();
},
this.buildDisplaySettingsGroup(),
this.buildThresholdSettingsGroup(),
this.buildFilterSettingsGroup()
);
}
buildDisplaySettingsGroup() {
return new SettingGroup('Display settings')
.append(
new Textbox(
'Max users shown',
'The maximum number of users shown for each reaction emoji.',
this.settings.maxUsersShown,
value => {
if (isNaN(value) || value < 1 || value > 99) {
return Toasts.error('Value must be a number between 1 and 99!');
}
this.settings.maxUsersShown = parseInt(value);
}
)
)
.append(
new Slider(
'Avatar size',
'Sets the size of the user avatars.',
8,
32,
this.settings.avatarSize,
value => (this.settings.avatarSize = value),
{
defaultValue: this.defaultSettings.avatarSize,
markers: [8, 12, 16, 20, 24, 32],
stickToMarkers: true,
units: 'px'
}
)
);
}
buildThresholdSettingsGroup() {
function renderMarker(value) {
if (value === 0) {
return 'Off';
}
if (value >= 1000) {
return `${value / 1000}k`;
}
return value;
}
return new SettingGroup('Thresholds')
.append(
new Slider(
'Reaction threshold',
'Hides the reactors when the number of separate reactions is exceeded on a message.',
0,
20,
this.settings.reactionThreshold,
value => (this.settings.reactionThreshold = value),
{
defaultValue: this.defaultSettings.reactionThreshold,
markers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
stickToMarkers: true,
renderMarker
}
)
)
.append(
new Slider(
'User threshold',
'Hides the reactors when their count is exceeded on a message.',
0,
10000,
this.settings.userThreshold,
value => (this.settings.userThreshold = value),
{
defaultValue: this.defaultSettings.userThreshold,
markers: [0, 10, 20, 50, 100, 500, 1000, 2000, 3000, 4000, 5000, 10000],
stickToMarkers: true,
equidistant: true,
renderMarker
}
)
)
.append(
new Switch(
'Use highest user count',
'Uses the reaction with most reactors of a message for user threshold.',
this.settings.useHighestUserCount,
value => (this.settings.useHighestUserCount = value)
)
);
}
buildFilterSettingsGroup() {
return new SettingGroup('Filters')
.append(
new Switch(
'Show self',
'Shows yourself within the reactors.',
this.settings.showSelf,
value => (this.settings.showSelf = value)
)
)
.append(
new Switch(
'Show bots',
'Shows bots within the reactors.',
this.settings.showBots,
value => (this.settings.showBots = value)
)
);
}
getSettingsPanel() {
return this.buildSettingsPanel().getElement();
}
async patchReaction() {
const Reaction = await this.findReaction();
Patcher.after(Reaction.prototype, 'render', (thisObject, args, returnValue) => {
const { message, emoji, count } = thisObject.props;
if (!this.canShowReactors(message)) return;
const renderTooltip = returnValue.props.children;
returnValue.props.children = props => {
const tooltip = renderTooltip(props);
const popout = tooltip.props.children.props.children;
const renderReactionInner = popout.props.children;
popout.props.children = props => {
const reactionInner = renderReactionInner(props);
reactionInner.props.children.props.children.push(
<Reactors
message={message}
emoji={emoji}
count={count}
max={this.settings.maxUsersShown}
showSelf={this.settings.showSelf}
showBots={this.settings.showBots}
size={this.settings.avatarSize}
/>
);
return reactionInner;
};
return tooltip;
};
});
this.forceUpdateAllReactions();
}
canShowReactors({ reactions }) {
const { reactionThreshold, userThreshold, useHighestUserCount } = this.settings;
if (reactionThreshold !== 0 && reactions.length > reactionThreshold) {
return false;
}
if (userThreshold !== 0) {
const userCount = useHighestUserCount
? Math.max(...reactions.map(reaction => reaction.count))
: reactions.reduce((total, reaction) => total + reaction.count, 0);
if (userCount > userThreshold) {
return false;
}
}
return true;
}
findReaction() {
return new Promise(resolve => {
const node = document.querySelector(DiscordSelectors.Reactions.reaction);
if (node) {
return resolve(this.findReactionReactInstance(node).type);
}
const unpatch = Patcher.after(Reactions.prototype, 'render', (thisObject, args, returnValue) => {
if (!returnValue) return;
const reaction = returnValue.props.children[0][0];
if (reaction) {
unpatch();
resolve(reaction.type);
}
});
});
}
forceUpdateAllReactions() {
for (const node of document.querySelectorAll(DiscordSelectors.Reactions.reaction)) {
this.findReactionReactInstance(node).stateNode.forceUpdate();
}
}
findReactionReactInstance(node) {
return Utilities.findInTree(ReactTools.getReactInstance(node), r => r?.type?.displayName === 'Reaction', {
walkable: ['return']
});
}
}