-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
387 lines (355 loc) · 12.9 KB
/
index.tsx
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { NavContextMenuPatchCallback } from "@api/ContextMenu";
import { definePluginSettings, useSettings } from "@api/Settings";
import ErrorBoundary from "@components/ErrorBoundary";
import { Devs } from "@utils/constants";
import { LazyComponent } from "@utils/lazyReact";
import { classes } from "@utils/misc";
import definePlugin, { OptionType } from "@utils/types";
import { filters, find, findByPropsLazy, findStoreLazy } from "@webpack";
import {
ChannelStore,
Menu,
PermissionsBits,
PermissionStore,
React,
SelectedChannelStore,
Toasts,
UserStore
} from "@webpack/common";
import type { Channel, User } from "discord-types/general";
import type { PropsWithChildren, SVGProps } from "react";
const HeaderBarIcon = LazyComponent(() => {
const filter = filters.byCode(".HEADER_BAR_BADGE");
return find(m => m.Icon && filter(m.Icon)).Icon;
});
interface BaseIconProps extends IconProps {
viewBox: string;
}
interface IconProps extends SVGProps<SVGSVGElement> {
className?: string;
height?: string | number;
width?: string | number;
}
function Icon({
height = 24,
width = 24,
className,
children,
viewBox,
...svgProps
}: PropsWithChildren<BaseIconProps>) {
return (
<svg
className={classes(className, "vc-icon")}
role="img"
width={width}
height={height}
viewBox={viewBox}
{...svgProps}
>
{children}
</svg>
);
}
function FollowIcon(props: IconProps) {
return (
<Icon
{...props}
className={classes(props.className, "vc-follow-icon")}
viewBox="0 -960 960 960"
>
<path
fill="currentColor"
d="m480-120-58-52q-101-91-167-157T150-447.5Q111-500 95.5-544T80-634q0-94 63-157t157-63q52 0 99 22t81 62q34-40 81-62t99-22q94 0 157 63t63 157q0 46-15.5 90T810-447.5Q771-395 705-329T538-172l-58 52Zm0-108q96-86 158-147.5t98-107q36-45.5 50-81t14-70.5q0-60-40-100t-100-40q-47 0-87 26.5T518-680h-76q-15-41-55-67.5T300-774q-60 0-100 40t-40 100q0 35 14 70.5t50 81q36 45.5 98 107T480-228Zm0-273Z"
/>
</Icon>
);
}
function UnfollowIcon(props: IconProps) {
return (
<Icon
{...props}
className={classes(props.className, "vc-unfollow-icon")}
viewBox="0 -960 960 960"
>
<path
fill="currentColor"
d="m480-120-58-52q-101-91-167-157T150-447.5Q111-500 95.5-544T80-634q0-94 63-157t157-63q52 0 99 22t81 62q34-40 81-62t99-22q94 0 157 63t63 157q0 46-15.5 90T810-447.5Q771-395 705-329T538-172l-58 52Z"
/>
</Icon>
);
}
interface VoiceState {
userId: string;
channelId?: string;
oldChannelId?: string;
deaf: boolean;
mute: boolean;
selfDeaf: boolean;
selfMute: boolean;
selfStream: boolean;
selfVideo: boolean;
sessionId: string;
suppress: boolean;
requestToSpeakTimestamp: string | null;
}
export const settings = definePluginSettings({
executeOnFollow: {
type: OptionType.BOOLEAN,
description: "Make sure to be in the same VC when following a user",
restartNeeded: false,
default: true
},
onlyManualTrigger: {
type: OptionType.BOOLEAN,
description: "Only trigger on indicator click",
restartNeeded: false,
default: false
},
followLeave: {
type: OptionType.BOOLEAN,
description: "Also leave when the followed user leaves",
restartNeeded: false,
default: false
},
autoMoveBack: {
type: OptionType.BOOLEAN,
description: "Automatically move back to the VC of the followed user when you got moved",
restartNeeded: false,
default: false
},
followUserId: {
type: OptionType.STRING,
description: "Followed User ID",
restartNeeded: false,
hidden: true, // Managed via context menu and indicator
default: "",
},
channelFull: {
type: OptionType.BOOLEAN,
description: "Attempt to move you to the channel when is not full anymore",
restartNeeded: false,
default: true,
}
});
const ChannelActions: {
disconnect: () => void;
selectVoiceChannel: (channelId: string) => void;
} = findByPropsLazy("disconnect", "selectVoiceChannel");
const VoiceStateStore: VoiceStateStore = findStoreLazy("VoiceStateStore");
const CONNECT = 1n << 20n;
interface VoiceStateStore {
getAllVoiceStates(): VoiceStateEntry;
getVoiceStatesForChannel(channelId: string): VoiceStateMember;
}
interface VoiceStateEntry {
[guildIdOrMe: string]: VoiceStateMember;
}
interface VoiceStateMember {
[userId: string]: VoiceState;
}
function getChannelId(userId: string) {
if (!userId) {
return null;
}
try {
const states = VoiceStateStore.getAllVoiceStates();
for (const users of Object.values(states)) {
if (users[userId]) {
return users[userId].channelId ?? null;
}
}
} catch (e) { }
return null;
}
function triggerFollow(userChannelId: string | null = getChannelId(settings.store.followUserId)) {
if (settings.store.followUserId) {
const myChanId = SelectedChannelStore.getVoiceChannelId();
if (userChannelId) {
// join when not already in the same channel
if (userChannelId !== myChanId) {
const channel = ChannelStore.getChannel(userChannelId);
const voiceStates = VoiceStateStore.getVoiceStatesForChannel(userChannelId);
const memberCount = voiceStates ? Object.keys(voiceStates).length : null;
if (channel.type === 1 || PermissionStore.can(CONNECT, channel)) {
if (channel.userLimit !== 0 && memberCount !== null && memberCount >= channel.userLimit && !PermissionStore.can(PermissionsBits.MOVE_MEMBERS, channel)) {
Toasts.show({
message: "Channel is full",
id: Toasts.genId(),
type: Toasts.Type.FAILURE
});
return;
}
ChannelActions.selectVoiceChannel(userChannelId);
Toasts.show({
message: "Followed user into a new voice channel",
id: Toasts.genId(),
type: Toasts.Type.SUCCESS
});
} else {
Toasts.show({
message: "Insufficient permissions to enter in the voice channel",
id: Toasts.genId(),
type: Toasts.Type.FAILURE
});
}
} else {
Toasts.show({
message: "You are already in the same channel",
id: Toasts.genId(),
type: Toasts.Type.FAILURE
});
}
} else if (myChanId) {
// if not in a voice channel and the setting is on disconnect
if (settings.store.followLeave) {
ChannelActions.disconnect();
Toasts.show({
message: "Followed user left, disconnected",
id: Toasts.genId(),
type: Toasts.Type.SUCCESS
});
} else {
Toasts.show({
message: "Followed user left, but not following disconnect",
id: Toasts.genId(),
type: Toasts.Type.FAILURE
});
}
} else {
Toasts.show({
message: "Followed user is not in a voice channel",
id: Toasts.genId(),
type: Toasts.Type.FAILURE
});
}
}
}
function toggleFollow(userId: string) {
if (settings.store.followUserId === userId) {
settings.store.followUserId = "";
} else {
settings.store.followUserId = userId;
if (settings.store.executeOnFollow) {
triggerFollow();
}
}
}
interface UserContextProps {
channel: Channel;
guildId?: string;
user: User;
}
const UserContext: NavContextMenuPatchCallback = (children, { user }: UserContextProps) => {
if (!user || user.id === UserStore.getCurrentUser().id) return;
const isFollowed = settings.store.followUserId === user.id;
const label = isFollowed ? "Unfollow User" : "Follow User";
const icon = isFollowed ? UnfollowIcon : FollowIcon;
children.splice(-1, 0, (
<Menu.MenuGroup>
<Menu.MenuItem
id="follow-user"
label={label}
action={() => toggleFollow(user.id)}
icon={icon}
/>
</Menu.MenuGroup>
));
};
export default definePlugin({
name: "FollowUser",
description: "Adds a follow option in the user context menu to always be in the same VC as them",
authors: [Devs.D3SOX],
settings,
patches: [
{
find: "toolbar:function",
replacement: {
match: /(function \i\(\i\){)(.{1,200}toolbar.{1,100}mobileToolbar)/,
replace: "$1$self.addIconToToolBar(arguments[0]);$2"
}
},
],
contextMenus: {
"user-context": UserContext
},
flux: {
VOICE_STATE_UPDATES({ voiceStates }: { voiceStates: VoiceState[]; }) {
if (settings.store.onlyManualTrigger || !settings.store.followUserId) {
return;
}
for (const { userId, channelId, oldChannelId } of voiceStates) {
if (channelId !== oldChannelId) {
const isMe = userId === UserStore.getCurrentUser().id;
// move back if the setting is on and you were moved
if (settings.store.autoMoveBack && isMe && channelId && oldChannelId) {
triggerFollow();
continue;
}
// if you're not in the channel of the followed user and it is no longer full, join
if (settings.store.channelFull && !isMe && !channelId && oldChannelId && oldChannelId !== SelectedChannelStore.getVoiceChannelId()) {
const channel = ChannelStore.getChannel(oldChannelId);
const channelVoiceStates = VoiceStateStore.getVoiceStatesForChannel(oldChannelId);
const memberCount = channelVoiceStates ? Object.keys(channelVoiceStates).length : null;
if (channel.userLimit !== 0 && memberCount !== null && memberCount === (channel.userLimit - 1) && !PermissionStore.can(PermissionsBits.MOVE_MEMBERS, channel)) {
const users = Object.values(channelVoiceStates).map(x => x.userId);
if (users.includes(settings.store.followUserId)) {
triggerFollow(oldChannelId);
continue;
}
}
}
const isFollowed = settings.store.followUserId === userId;
if (!isFollowed) {
continue;
}
if (channelId) {
// move or join new channel -> also join
triggerFollow(channelId);
} else if (oldChannelId) {
// leave -> disconnect
triggerFollow(null);
}
}
}
},
},
FollowIndicator() {
const { plugins: { FollowUser: { followUserId } } } = useSettings(["plugins.FollowUser.followUserId"]);
if (followUserId) {
return (
<HeaderBarIcon
tooltip={`Following ${UserStore.getUser(followUserId).username} (click to trigger manually, right-click to unfollow)`}
icon={UnfollowIcon}
onClick={() => {
triggerFollow();
}}
onContextMenu={() => {
settings.store.followUserId = "";
}}
/>
);
}
return null;
},
addIconToToolBar(e: { toolbar: React.ReactNode[] | React.ReactNode; }) {
if (Array.isArray(e.toolbar)) {
return e.toolbar.push(
<ErrorBoundary noop={true} key="follow-indicator">
<this.FollowIndicator/>
</ErrorBoundary>
);
}
e.toolbar = [
<ErrorBoundary noop={true} key="follow-indicator">
<this.FollowIndicator />
</ErrorBoundary>,
e.toolbar,
];
},
});