/* * 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 . */ 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([]); const [error, setError] = useState(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 ( Friends Check
Current Server {currentGuild ? currentGuild.name : "No server selected - please click on a server first"} Enter Friend's User ID Enter the User ID of a friend. The plugin will find members in this server who are also friends with that user. {error && ( {error} )}
{results.length > 0 && (
Members who are friends with that user ({results.length})
{results.map(member => (
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)"} > {member.username}
{member.nick || member.global_name || member.username} {member.username}{member.discriminator !== "0" && `#${member.discriminator}`}
))}
)} {results.length === 0 && !loading && !error && checkedCount > 0 && (
No members found who are friends with that user.
)}
); } function openFriendsCheckModal() { openModal((modalProps) => ( )); } export default definePlugin({ name: "FriendsCheck", description: "Check which server members are friends with a specific user", authors: [Devs.Ven], toolboxActions: { "Open Friends Check": openFriendsCheckModal, } });