285 lines
12 KiB
TypeScript
285 lines
12 KiB
TypeScript
/*
|
|
* Vencord, a modification for Discord's desktop app
|
|
* Copyright (c) 2023 Vendicated and contributors
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import ErrorBoundary from "@components/ErrorBoundary";
|
|
import { Devs } from "@utils/constants";
|
|
import { openUserProfile } from "@utils/discord";
|
|
import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalProps, ModalRoot, ModalSize, openModal } from "@utils/modal";
|
|
import definePlugin from "@utils/types";
|
|
import { Button, GuildMemberStore, GuildStore, RestAPI, SelectedGuildStore, showToast, Text, TextInput, Toasts, UserStore, useState } from "@webpack/common";
|
|
|
|
interface MutualFriend {
|
|
id: string;
|
|
username: string;
|
|
discriminator: string;
|
|
avatar: string | null;
|
|
global_name?: string;
|
|
}
|
|
|
|
interface GuildMemberWithMutual {
|
|
id: string;
|
|
username: string;
|
|
discriminator: string;
|
|
avatar: string | null;
|
|
global_name?: string;
|
|
nick?: string;
|
|
}
|
|
|
|
function FriendsCheckModal({ modalProps, onClose }: { modalProps: ModalProps; onClose: () => void; }) {
|
|
const [friendId, setFriendId] = useState("");
|
|
const [loading, setLoading] = useState(false);
|
|
const [results, setResults] = useState<GuildMemberWithMutual[]>([]);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [checkedCount, setCheckedCount] = useState(0);
|
|
const [totalCount, setTotalCount] = useState(0);
|
|
|
|
const currentGuildId = SelectedGuildStore.getGuildId();
|
|
const currentGuild = currentGuildId ? GuildStore.getGuild(currentGuildId) : null;
|
|
|
|
const handleSubmit = async () => {
|
|
if (!friendId.trim()) {
|
|
setError("Please enter a friend's user ID");
|
|
return;
|
|
}
|
|
|
|
if (!currentGuildId) {
|
|
setError("Please select a server first (click on a server in Discord)");
|
|
return;
|
|
}
|
|
|
|
setLoading(true);
|
|
setError(null);
|
|
setResults([]);
|
|
setCheckedCount(0);
|
|
|
|
try {
|
|
// Get all members of the current guild
|
|
const guildMembers = GuildMemberStore.getMembers(currentGuildId);
|
|
const currentUserId = UserStore.getCurrentUser()?.id;
|
|
|
|
if (!guildMembers || guildMembers.length === 0) {
|
|
setError("No members found in this server. Try scrolling through the member list first to load them.");
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
|
|
setTotalCount(guildMembers.length);
|
|
const membersWithMutualFriend: GuildMemberWithMutual[] = [];
|
|
|
|
// Check each member's mutual friends
|
|
for (let i = 0; i < guildMembers.length; i++) {
|
|
const member = guildMembers[i];
|
|
|
|
// Skip yourself
|
|
if (member.userId === currentUserId) {
|
|
setCheckedCount(i + 1);
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
// Fetch mutual friends between this member and the target friend
|
|
const response = await RestAPI.get({
|
|
url: `/users/${member.userId}/relationships`,
|
|
});
|
|
|
|
if (response.body && Array.isArray(response.body)) {
|
|
// Check if the target friend ID is in this member's friends list
|
|
const hasMutualFriend = response.body.some(
|
|
(friend: MutualFriend) => friend.id === friendId.trim()
|
|
);
|
|
|
|
if (hasMutualFriend) {
|
|
// Get user details
|
|
const user = UserStore.getUser(member.userId);
|
|
if (user) {
|
|
membersWithMutualFriend.push({
|
|
id: user.id,
|
|
username: user.username,
|
|
discriminator: user.discriminator || "0",
|
|
avatar: user.avatar,
|
|
global_name: user.globalName,
|
|
nick: member.nick
|
|
});
|
|
}
|
|
}
|
|
}
|
|
} catch (err) {
|
|
// This endpoint might not work for all users, try alternative approach
|
|
// Skip this member if we can't fetch their relationships
|
|
console.log(`Could not fetch relationships for ${member.userId}`);
|
|
}
|
|
|
|
setCheckedCount(i + 1);
|
|
|
|
// Small delay to avoid rate limiting
|
|
if (i % 5 === 0 && i > 0) {
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
|
}
|
|
}
|
|
|
|
setResults(membersWithMutualFriend);
|
|
|
|
showToast(
|
|
`Found ${membersWithMutualFriend.length} members who are friends with that user`,
|
|
Toasts.Type.SUCCESS
|
|
);
|
|
} catch (err: any) {
|
|
console.error("FriendsCheck error:", err);
|
|
setError(err.message || "Failed to check members. Please try again.");
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleFriendIdChange = (value: string) => {
|
|
setFriendId(value);
|
|
setError(null);
|
|
};
|
|
|
|
const handleUserClick = (userId: string) => {
|
|
openUserProfile(userId);
|
|
};
|
|
|
|
return (
|
|
<ModalRoot {...modalProps} size={ModalSize.MEDIUM}>
|
|
<ModalHeader>
|
|
<Text variant="heading-lg/semibold">Friends Check</Text>
|
|
<ModalCloseButton onClick={onClose} />
|
|
</ModalHeader>
|
|
|
|
<ModalContent style={{ padding: "16px" }}>
|
|
<div>
|
|
<Text variant="text-md/semibold">Current Server</Text>
|
|
<Text color="text-muted" style={{ marginBottom: "12px" }}>
|
|
{currentGuild ? currentGuild.name : "No server selected - please click on a server first"}
|
|
</Text>
|
|
|
|
<Text variant="text-md/semibold">Enter Friend's User ID</Text>
|
|
<Text color="text-muted" style={{ marginBottom: "8px" }}>
|
|
Enter the User ID of a friend. The plugin will find members in this server who are also friends with that user.
|
|
</Text>
|
|
<TextInput
|
|
placeholder="Enter friend's user ID (e.g., 123456789012345678)"
|
|
value={friendId}
|
|
onChange={handleFriendIdChange}
|
|
disabled={loading}
|
|
style={{ marginBottom: "16px" }}
|
|
/>
|
|
{error && (
|
|
<Text color="text-danger" style={{ marginBottom: "8px" }}>
|
|
{error}
|
|
</Text>
|
|
)}
|
|
<Button
|
|
onClick={handleSubmit}
|
|
disabled={loading || !friendId.trim() || !currentGuildId}
|
|
color={Button.Colors.BRAND}
|
|
>
|
|
{loading ? `Checking... (${checkedCount}/${totalCount})` : "Check Server Members"}
|
|
</Button>
|
|
</div>
|
|
|
|
{results.length > 0 && (
|
|
<div style={{ marginTop: "20px" }}>
|
|
<Text variant="text-md/semibold">
|
|
Members who are friends with that user ({results.length})
|
|
</Text>
|
|
<div style={{ maxHeight: "300px", overflowY: "auto", marginTop: "8px" }}>
|
|
{results.map(member => (
|
|
<div
|
|
key={member.id}
|
|
onClick={() => handleUserClick(member.id)}
|
|
style={{
|
|
display: "flex",
|
|
alignItems: "center",
|
|
padding: "8px",
|
|
borderRadius: "4px",
|
|
backgroundColor: "var(--background-secondary)",
|
|
marginBottom: "4px",
|
|
cursor: "pointer",
|
|
transition: "background-color 0.2s"
|
|
}}
|
|
onMouseEnter={e => e.currentTarget.style.backgroundColor = "var(--background-secondary-alt)"}
|
|
onMouseLeave={e => e.currentTarget.style.backgroundColor = "var(--background-secondary)"}
|
|
>
|
|
<img
|
|
src={member.avatar
|
|
? `https://cdn.discordapp.com/avatars/${member.id}/${member.avatar}.png?size=32`
|
|
: `https://cdn.discordapp.com/embed/avatars/${parseInt(member.discriminator) % 5}.png`
|
|
}
|
|
alt={member.username}
|
|
style={{
|
|
width: "32px",
|
|
height: "32px",
|
|
borderRadius: "50%",
|
|
marginRight: "12px"
|
|
}}
|
|
/>
|
|
<div>
|
|
<Text variant="text-md/semibold">
|
|
{member.nick || member.global_name || member.username}
|
|
</Text>
|
|
<Text color="text-muted" variant="text-sm/normal">
|
|
{member.username}{member.discriminator !== "0" && `#${member.discriminator}`}
|
|
</Text>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{results.length === 0 && !loading && !error && checkedCount > 0 && (
|
|
<div style={{ marginTop: "20px" }}>
|
|
<Text color="text-muted">
|
|
No members found who are friends with that user.
|
|
</Text>
|
|
</div>
|
|
)}
|
|
</ModalContent>
|
|
|
|
<ModalFooter>
|
|
<Button onClick={onClose} color={Button.Colors.PRIMARY}>
|
|
Close
|
|
</Button>
|
|
</ModalFooter>
|
|
</ModalRoot>
|
|
);
|
|
}
|
|
|
|
function openFriendsCheckModal() {
|
|
openModal((modalProps) => (
|
|
<ErrorBoundary>
|
|
<FriendsCheckModal
|
|
modalProps={modalProps}
|
|
onClose={modalProps.onClose}
|
|
/>
|
|
</ErrorBoundary>
|
|
));
|
|
}
|
|
|
|
export default definePlugin({
|
|
name: "FriendsCheck",
|
|
description: "Check which server members are friends with a specific user",
|
|
authors: [Devs.Ven],
|
|
|
|
toolboxActions: {
|
|
"Open Friends Check": openFriendsCheckModal,
|
|
}
|
|
});
|