From bc2eff8e9f44597395584b6561cecd8299cbf1fb Mon Sep 17 00:00:00 2001 From: Inbestigator <119569726+Inbestigator@users.noreply.github.com> Date: Wed, 6 Mar 2024 08:03:23 -0800 Subject: [PATCH] Update and rename s to index.tsx --- src/plugins/bypassDND/index.tsx | 154 ++++++++++++++++++++++++++++++++ src/plugins/bypassDND/s | 0 2 files changed, 154 insertions(+) create mode 100644 src/plugins/bypassDND/index.tsx delete mode 100644 src/plugins/bypassDND/s diff --git a/src/plugins/bypassDND/index.tsx b/src/plugins/bypassDND/index.tsx new file mode 100644 index 000000000..93e7500e1 --- /dev/null +++ b/src/plugins/bypassDND/index.tsx @@ -0,0 +1,154 @@ +import { NavContextMenuPatchCallback, addContextMenuPatch, removeContextMenuPatch } from "@api/ContextMenu"; +import { definePluginSettings } from "@api/Settings"; +import { DataStore, Notifications } from "@api/index"; +import { Devs } from "@utils/constants"; +import definePlugin, { OptionType } from "@utils/types"; +import { ChannelStore, Menu, PresenceStore, PrivateChannelsStore, UserStore } from "@webpack/common"; +import { Channel, Guild, Message, User } from "discord-types/general"; +interface ContextProps { + channel: Channel; + user: User; + guild: Guild; +} + +interface IMessageCreate { + type: "MESSAGE_CREATE"; + optimistic: boolean; + isPushNotification: boolean; + channelId: string; + guildId: string; + message: Message; +} + +const GuildContext: NavContextMenuPatchCallback = (children, { guild }: ContextProps) => () => { + if (!guild) return; + children.splice(-1, 0, ( + + { + if (bypasses["guilds"].includes(guild.id)) bypasses["guilds"] = await bypasses["guilds"].filter(id => id !== guild.id); + else bypasses["guilds"].push(guild.id); + await DataStore.set("bypassdnd", bypasses); + settings.store.guilds = (bypasses["guilds"].join(', ')); + }} + /> + + )); +}; + +const ChannelContext: NavContextMenuPatchCallback = (children, { channel }: ContextProps) => () => { + if (!channel) return; + children.splice(-1, 0, ( + + { + if (bypasses["channels"].includes(channel.id)) bypasses["channels"] = await bypasses["channels"].filter(id => id !== channel.id); + else bypasses["channels"].push(channel.id); + await DataStore.set("bypassdnd", bypasses); + settings.store.channels = (bypasses["channels"].join(', ')); + }} + /> + + )); +}; + +const UserContext: NavContextMenuPatchCallback = (children, { user }: ContextProps) => () => { + if (!user) return; + children.splice(-1, 0, ( + + { + if (bypasses["users"].includes(user.id)) bypasses["users"] = await bypasses["users"].filter(id => id !== user.id); + else bypasses["users"].push(user.id); + await DataStore.set("bypassdnd", bypasses); + settings.store.users = (bypasses["users"].join(', ')); + }} + /> + + )); +}; + +let bypasses; + +const settings = definePluginSettings({ + guilds: { + type: OptionType.STRING, + description: "Guilds to let bypass (notified when pinged anywhere in guild)", + default: "", + placeholder: "Separate with commas", + onChange: async function (value) { + bypasses["guild"] = value.replace(/\s/g, '').split(',').filter(id => id.trim() !== ''); + await DataStore.set("bypassdnd", bypasses); + }, + }, + channels: { + type: OptionType.STRING, + description: "Channels to let bypass (notified when pinged in that channel)", + default: "", + placeholder: "Separate with commas", + onChange: async function (value) { + bypasses["channels"] = value.replace(/\s/g, '').split(',').filter(id => id.trim() !== ''); + await DataStore.set("bypassdnd", bypasses); + }, + }, + users: { + type: OptionType.STRING, + description: "Users to let bypass (notified for all messages)", + default: "", + placeholder: "Separate with commas", + onChange: async function (value) { + bypasses["users"] = value.replace(/\s/g, '').split(',').filter(id => id.trim() !== ''); + await DataStore.set("bypassdnd", bypasses); + }, + } +}); + +export default definePlugin({ + 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.", + authors: [Devs.Inbestigator], + flux: { + async MESSAGE_CREATE({ optimistic, type, message, guildId, channelId }: IMessageCreate) { + if (optimistic || type !== "MESSAGE_CREATE") return; + if (message.state === "SENDING") return; + if (!message.content) return; + const currentUser = UserStore.getCurrentUser(); + if (message.author.id === currentUser.id) return; + if (await PresenceStore.getStatus(currentUser.id) != 'dnd') return; + if ((bypasses.guilds.includes(guildId) || bypasses.channels.includes(channelId)) && (message.content.includes(`<@${currentUser.id}>`) || message.mentions.some(mention => mention.id === currentUser.id))) { + await Notifications.showNotification({ + title: `${message.author.globalName ?? message.author.username} sent a message in ${ChannelStore.getChannel(channelId).name}`, + body: message.content, + icon: UserStore.getUser(message.author.id).getAvatarURL(undefined, undefined, false), + }); + return; + } + if (bypasses.users.includes(message.author.id) && channelId === await PrivateChannelsStore.getOrEnsurePrivateChannel(message.author.id)) { + await Notifications.showNotification({ + title: `${message.author.globalName ?? message.author.username} sent a message in a DM`, + body: message.content, + icon: UserStore.getUser(message.author.id).getAvatarURL(undefined, undefined, false), + }); + } + } + }, + settings, + async start() { + addContextMenuPatch("guild-context", GuildContext); + addContextMenuPatch("channel-context", ChannelContext); + addContextMenuPatch("user-context", UserContext); + bypasses = await DataStore.get("bypassdnd") ?? { guilds: [], channels: [], users: [] }; + await DataStore.set("bypassdnd", bypasses); + }, + stop() { + removeContextMenuPatch("guild-context", GuildContext); + removeContextMenuPatch("channel-context", ChannelContext); + removeContextMenuPatch("user-context", UserContext); + } +}); diff --git a/src/plugins/bypassDND/s b/src/plugins/bypassDND/s deleted file mode 100644 index e69de29bb..000000000