2022-11-28 13:29:46 -05:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2022 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 { Flex } from "@components/Flex";
|
|
|
|
import { Devs } from "@utils/constants";
|
|
|
|
import { getCurrentChannel } from "@utils/discord";
|
|
|
|
import { useForceUpdater } from "@utils/misc";
|
|
|
|
import definePlugin from "@utils/types";
|
2022-11-28 17:01:04 -05:00
|
|
|
import { FluxDispatcher, Tooltip } from "@webpack/common";
|
2022-11-28 13:29:46 -05:00
|
|
|
|
|
|
|
const counts = {} as Record<string, [number, number]>;
|
|
|
|
let forceUpdate: () => void;
|
|
|
|
|
|
|
|
function MemberCount() {
|
|
|
|
const guildId = getCurrentChannel().guild_id;
|
|
|
|
const c = counts[guildId];
|
|
|
|
|
|
|
|
forceUpdate = useForceUpdater();
|
|
|
|
|
|
|
|
if (!c) return null;
|
|
|
|
|
2022-12-03 17:11:08 -05:00
|
|
|
let total = c[0].toLocaleString();
|
2022-11-28 13:29:46 -05:00
|
|
|
if (total === "0" && c[1] > 0) {
|
|
|
|
total = "Loading...";
|
|
|
|
}
|
|
|
|
|
2022-12-03 17:11:08 -05:00
|
|
|
const online = c[1].toLocaleString();
|
|
|
|
|
2022-11-28 13:29:46 -05:00
|
|
|
return (
|
|
|
|
<Flex id="vc-membercount" style={{
|
|
|
|
marginTop: "1em",
|
|
|
|
marginBottom: "-.5em",
|
|
|
|
paddingInline: "1em",
|
|
|
|
justifyContent: "center",
|
|
|
|
alignContent: "center",
|
|
|
|
gap: 0
|
|
|
|
}}>
|
2022-12-03 17:11:08 -05:00
|
|
|
<Tooltip text={`${online} Online`} position="bottom">
|
2022-11-28 17:01:04 -05:00
|
|
|
{props => (
|
|
|
|
<div {...props}>
|
|
|
|
<span
|
|
|
|
style={{
|
2023-02-16 16:40:19 -05:00
|
|
|
backgroundColor: "var(--green-360)",
|
2022-11-28 17:01:04 -05:00
|
|
|
width: "12px",
|
|
|
|
height: "12px",
|
|
|
|
borderRadius: "50%",
|
|
|
|
display: "inline-block",
|
|
|
|
marginRight: "0.5em"
|
|
|
|
}}
|
|
|
|
/>
|
2023-02-16 16:40:19 -05:00
|
|
|
<span style={{ color: "var(--green-360)" }}>{online}</span>
|
2022-11-28 17:01:04 -05:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Tooltip>
|
2022-12-03 17:11:08 -05:00
|
|
|
<Tooltip text={`${total} Total Members`} position="bottom">
|
2022-11-28 17:01:04 -05:00
|
|
|
{props => (
|
|
|
|
<div {...props}>
|
|
|
|
<span
|
|
|
|
style={{
|
|
|
|
width: "6px",
|
|
|
|
height: "6px",
|
|
|
|
borderRadius: "50%",
|
2023-02-16 16:40:19 -05:00
|
|
|
border: "3px solid var(--primary-400)",
|
2022-11-28 17:01:04 -05:00
|
|
|
display: "inline-block",
|
|
|
|
marginRight: "0.5em",
|
|
|
|
marginLeft: "1em"
|
|
|
|
}}
|
|
|
|
/>
|
2023-02-16 16:40:19 -05:00
|
|
|
<span style={{ color: "var(--primary-400)" }}>{total}</span>
|
2022-11-28 17:01:04 -05:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Tooltip>
|
2022-11-28 13:29:46 -05:00
|
|
|
</Flex>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default definePlugin({
|
|
|
|
name: "MemberCount",
|
|
|
|
description: "Shows the amount of online & total members in the server member list",
|
2022-12-03 17:42:18 -05:00
|
|
|
authors: [Devs.Ven, Devs.Commandtechno],
|
2022-11-28 13:29:46 -05:00
|
|
|
|
|
|
|
patches: [{
|
|
|
|
find: ".isSidebarVisible,",
|
|
|
|
replacement: {
|
|
|
|
match: /(var (.)=.\.className.+?children):\[(.\.useMemo[^}]+"aria-multiselectable")/,
|
2023-02-10 16:41:49 -05:00
|
|
|
replace: "$1:[$2.startsWith('members')?$self.render():null,$3"
|
2022-11-28 13:29:46 -05:00
|
|
|
}
|
|
|
|
}],
|
|
|
|
|
2022-11-28 17:01:04 -05:00
|
|
|
onGuildMemberListUpdate({ guildId, groups, memberCount, id }) {
|
|
|
|
// eeeeeh - sometimes it has really wrong counts??? like 10 times less than actual
|
|
|
|
// but if we only listen to everyone updates, sometimes we never get the count?
|
|
|
|
// this seems to work but isn't optional
|
|
|
|
if (id !== "everyone" && counts[guildId]) return;
|
|
|
|
|
2022-11-28 13:29:46 -05:00
|
|
|
let count = 0;
|
|
|
|
for (const group of groups) {
|
|
|
|
if (group.id !== "offline")
|
|
|
|
count += group.count;
|
|
|
|
}
|
|
|
|
counts[guildId] = [memberCount, count];
|
|
|
|
forceUpdate?.();
|
|
|
|
},
|
|
|
|
|
|
|
|
start() {
|
|
|
|
FluxDispatcher.subscribe("GUILD_MEMBER_LIST_UPDATE", this.onGuildMemberListUpdate);
|
|
|
|
},
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
FluxDispatcher.unsubscribe("GUILD_MEMBER_LIST_UPDATE", this.onGuildMemberListUpdate);
|
|
|
|
},
|
|
|
|
|
|
|
|
render: () => (
|
|
|
|
<ErrorBoundary noop>
|
|
|
|
<MemberCount />
|
|
|
|
</ErrorBoundary>
|
|
|
|
)
|
|
|
|
});
|