mirror of
https://github.com/Vendicated/Vencord.git
synced 2025-02-24 07:25:10 +00:00
Refactored storing system
This commit is contained in:
parent
7478a275b3
commit
4fcb34f1b5
1 changed files with 63 additions and 70 deletions
|
@ -9,42 +9,53 @@ import { Notifications } from "@api/index";
|
||||||
import { definePluginSettings } from "@api/Settings";
|
import { definePluginSettings } from "@api/Settings";
|
||||||
import { Devs } from "@utils/constants";
|
import { Devs } from "@utils/constants";
|
||||||
import { getCurrentChannel } from "@utils/discord";
|
import { getCurrentChannel } from "@utils/discord";
|
||||||
|
import { localStorage } from "@utils/localStorage";
|
||||||
import { Logger } from "@utils/Logger";
|
import { Logger } from "@utils/Logger";
|
||||||
import definePlugin, { OptionType } from "@utils/types";
|
import definePlugin, { OptionType } from "@utils/types";
|
||||||
import { ChannelStore, Menu, MessageStore, NavigationRouter, PresenceStore, PrivateChannelsStore, UserStore, WindowStore } from "@webpack/common";
|
import { ChannelStore, Menu, MessageStore, NavigationRouter, PresenceStore, PrivateChannelsStore, UserStore, WindowStore } from "@webpack/common";
|
||||||
import type { Message } from "discord-types/general";
|
import type { Message } from "discord-types/general";
|
||||||
|
|
||||||
interface IMessageCreate {
|
interface MessageCreateProps {
|
||||||
channelId: string;
|
channelId: string;
|
||||||
guildId: string;
|
guildId: string;
|
||||||
message: Message;
|
message: Message;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Icon(enabled?: boolean): JSX.Element {
|
type Sources = "guild" | "user" | "channel";
|
||||||
return <svg
|
|
||||||
width="18"
|
function Icon(enabled: boolean) {
|
||||||
height="18"
|
return (
|
||||||
>
|
<svg width="18" height="18">
|
||||||
<circle cx="9" cy="9" r="8" fill={!enabled ? "var(--status-danger)" : "currentColor"} />
|
<circle cx="9" cy="9" r="8" fill={enabled ? "currentColor" : "var(--status-danger)"} />
|
||||||
<circle cx="9" cy="9" r="3.75" fill={!enabled ? "white" : "black"} />
|
<circle cx="9" cy="9" r="3.75" fill={enabled ? "black" : "white"} />
|
||||||
</svg>;
|
</svg>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function processIds(value: string): string {
|
const getBypassed = (source: Sources) =>
|
||||||
return value.replace(/\s/g, "").split(",").filter(id => id.trim() !== "").join(", ");
|
(JSON.parse(localStorage.getItem("vc-bypass-dnd") ?? '{"guild": [], "user": [], "channel": []}') as Record<Sources, string[]>)[source];
|
||||||
|
|
||||||
|
const getAllBypassed = () => ({
|
||||||
|
guild: getBypassed("guild"),
|
||||||
|
user: getBypassed("user"),
|
||||||
|
channel: getBypassed("channel"),
|
||||||
|
});
|
||||||
|
|
||||||
|
function setLists(source: Sources, value: string[]) {
|
||||||
|
localStorage.setItem("vc-bypass-dnd", JSON.stringify({ ...getAllBypassed(), [source]: value }));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function showNotification(message: Message, guildId: string | undefined): Promise<void> {
|
async function showNotification(message: Message, guildId: string | undefined) {
|
||||||
try {
|
try {
|
||||||
const channel = ChannelStore.getChannel(message.channel_id);
|
const channel = ChannelStore.getChannel(message.channel_id);
|
||||||
const channelRegex = /<#(\d{19})>/g;
|
const channelRegex = /<#(\d+)>/g;
|
||||||
const userRegex = /<@(\d{18})>/g;
|
const userRegex = /<@(\d+)>/g;
|
||||||
|
|
||||||
message.content = message.content.replace(channelRegex, (match: any, channelId: string) => {
|
message.content = message.content.replace(channelRegex, (_, channelId) => {
|
||||||
return `#${ChannelStore.getChannel(channelId)?.name}`;
|
return `#${ChannelStore.getChannel(channelId)?.name}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
message.content = message.content.replace(userRegex, (match: any, userId: string) => {
|
message.content = message.content.replace(userRegex, (_, userId) => {
|
||||||
return `@${(UserStore.getUser(userId) as any).globalName}`;
|
return `@${(UserStore.getUser(userId) as any).globalName}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -52,7 +63,7 @@ async function showNotification(message: Message, guildId: string | undefined):
|
||||||
title: `${(message.author as any).globalName} ${guildId ? `(#${channel?.name}, ${ChannelStore.getChannel(channel?.parent_id)?.name})` : ""}`,
|
title: `${(message.author as any).globalName} ${guildId ? `(#${channel?.name}, ${ChannelStore.getChannel(channel?.parent_id)?.name})` : ""}`,
|
||||||
body: message.content,
|
body: message.content,
|
||||||
icon: UserStore.getUser(message.author.id).getAvatarURL(undefined, undefined, false),
|
icon: UserStore.getUser(message.author.id).getAvatarURL(undefined, undefined, false),
|
||||||
onClick: function (): void {
|
onClick: () => {
|
||||||
NavigationRouter.transitionTo(`/channels/${guildId ?? "@me"}/${message.channel_id}/${message.id}`);
|
NavigationRouter.transitionTo(`/channels/${guildId ?? "@me"}/${message.channel_id}/${message.id}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -65,82 +76,64 @@ async function showNotification(message: Message, guildId: string | undefined):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function ContextCallback(name: "guild" | "user" | "channel"): NavContextMenuPatchCallback {
|
const ContextCallback = (name: Sources): NavContextMenuPatchCallback => (children, props: Record<Sources, { id: string; }>) => {
|
||||||
return (children, props) => {
|
const data = props[name];
|
||||||
const type = props[name];
|
if (!data) return;
|
||||||
if (!type) return;
|
const isEnabled = getBypassed(name).includes(data.id);
|
||||||
const enabled = settings.store[`${name}s`].split(", ").includes(type.id);
|
|
||||||
if (name === "user" && type.id === UserStore.getCurrentUser().id) return;
|
if (name === "user" && data.id === UserStore.getCurrentUser().id) {
|
||||||
children.splice(-1, 0, (
|
return;
|
||||||
<Menu.MenuGroup>
|
}
|
||||||
<Menu.MenuItem
|
|
||||||
id={`dnd-${name}-bypass`}
|
children.splice(-1, 0, (
|
||||||
label={`${enabled ? "Remove" : "Add"} DND Bypass`}
|
<Menu.MenuGroup>
|
||||||
icon={() => Icon(enabled)}
|
<Menu.MenuItem
|
||||||
action={() => {
|
id={`dnd-${name}-bypass`}
|
||||||
let bypasses: string[] = settings.store[`${name}s`].split(", ");
|
label={`${isEnabled ? "Remove" : "Add"} DND Bypass`}
|
||||||
if (enabled) bypasses = bypasses.filter(id => id !== type.id);
|
icon={() => Icon(isEnabled)}
|
||||||
else bypasses.push(type.id);
|
action={() => {
|
||||||
settings.store[`${name}s`] = bypasses.filter(id => id.trim() !== "").join(", ");
|
const bypasses = getBypassed(name);
|
||||||
}}
|
setLists(name, isEnabled ? bypasses.filter(id => id !== data.id) : [...bypasses, data.id]);
|
||||||
/>
|
}}
|
||||||
</Menu.MenuGroup>
|
/>
|
||||||
));
|
</Menu.MenuGroup>
|
||||||
};
|
));
|
||||||
}
|
};
|
||||||
|
|
||||||
const settings = definePluginSettings({
|
const settings = definePluginSettings({
|
||||||
guilds: {
|
|
||||||
type: OptionType.STRING,
|
|
||||||
description: "Guilds to let bypass (notified when pinged anywhere in guild)",
|
|
||||||
default: "",
|
|
||||||
placeholder: "Separate with commas",
|
|
||||||
onChange: value => settings.store.guilds = processIds(value)
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
type: OptionType.STRING,
|
|
||||||
description: "Channels to let bypass (notified when pinged in that channel)",
|
|
||||||
default: "",
|
|
||||||
placeholder: "Separate with commas",
|
|
||||||
onChange: value => settings.store.channels = processIds(value)
|
|
||||||
},
|
|
||||||
users: {
|
|
||||||
type: OptionType.STRING,
|
|
||||||
description: "Users to let bypass (notified for all messages sent in DMs)",
|
|
||||||
default: "",
|
|
||||||
placeholder: "Separate with commas",
|
|
||||||
onChange: value => settings.store.users = processIds(value)
|
|
||||||
},
|
|
||||||
allowOutsideOfDms: {
|
allowOutsideOfDms: {
|
||||||
type: OptionType.BOOLEAN,
|
type: OptionType.BOOLEAN,
|
||||||
description: "Allow selected users to bypass DND outside of DMs too (acts like a channel/guild bypass, but it's for all messages sent by the selected users)"
|
description: "Allow selected users to bypass do not disturb outside of DMs (get notified of all messages you're mentioned in from selected users)",
|
||||||
},
|
},
|
||||||
notificationSound: {
|
notificationSound: {
|
||||||
type: OptionType.BOOLEAN,
|
type: OptionType.BOOLEAN,
|
||||||
description: "Whether the notification sound should be played",
|
description: "Whether the notification sound should be played",
|
||||||
default: true,
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export default definePlugin({
|
export default definePlugin({
|
||||||
name: "BypassDND",
|
name: "BypassDND",
|
||||||
description: "Still get notifications from specific sources when in do not disturb mode. Right-click on users/channels/guilds to set them to bypass do not disturb mode.",
|
description: "Get notifications from specific sources even in do not disturb mode. Right-click on users/channels/guilds to set bypass do not disturb mode.",
|
||||||
authors: [Devs.Inbestigator],
|
authors: [Devs.Inbestigator],
|
||||||
flux: {
|
flux: {
|
||||||
async MESSAGE_CREATE({ message, guildId, channelId }: IMessageCreate): Promise<void> {
|
async MESSAGE_CREATE({ message, guildId, channelId }: MessageCreateProps) {
|
||||||
try {
|
try {
|
||||||
const currentUser = UserStore.getCurrentUser();
|
const currentUser = UserStore.getCurrentUser();
|
||||||
const userStatus = await PresenceStore.getStatus(currentUser.id);
|
const userStatus = await PresenceStore.getStatus(currentUser.id);
|
||||||
const currentChannelId = getCurrentChannel()?.id ?? "0";
|
const currentChannelId = getCurrentChannel()?.id ?? "0";
|
||||||
if (message.state === "SENDING" || message.content === "" || message.author.id === currentUser.id || (channelId === currentChannelId && WindowStore.isFocused()) || userStatus !== "dnd") {
|
const isLookingAtChannel = channelId === currentChannelId && WindowStore.isFocused();
|
||||||
|
|
||||||
|
if (message.author.id === currentUser.id || isLookingAtChannel || userStatus !== "dnd") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const mentioned = MessageStore.getMessage(channelId, message.id)?.mentioned;
|
|
||||||
if ((settings.store.guilds.split(", ").includes(guildId) || settings.store.channels.split(", ").includes(channelId)) && mentioned) {
|
const isMentioned = MessageStore.getMessage(channelId, message.id)?.mentioned;
|
||||||
|
|
||||||
|
if ((getBypassed("guild").includes(guildId) || getBypassed("channel").includes(channelId)) && isMentioned) {
|
||||||
await showNotification(message, guildId);
|
await showNotification(message, guildId);
|
||||||
} else if (settings.store.users.split(", ").includes(message.author.id)) {
|
} else if (getBypassed("user").includes(message.author.id)) {
|
||||||
const userChannelId = await PrivateChannelsStore.getOrEnsurePrivateChannel(message.author.id);
|
const userChannelId = await PrivateChannelsStore.getOrEnsurePrivateChannel(message.author.id);
|
||||||
if (channelId === userChannelId || (mentioned && settings.store.allowOutsideOfDms === true)) {
|
if (channelId === userChannelId || (isMentioned && settings.store.allowOutsideOfDms === true)) {
|
||||||
await showNotification(message, guildId);
|
await showNotification(message, guildId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue