Added types to bypasses object

This commit is contained in:
Inbestigator 2024-03-06 08:41:17 -08:00
parent ee9e44d2d7
commit c090b8b329

View file

@ -1,154 +1,164 @@
import { NavContextMenuPatchCallback, addContextMenuPatch, removeContextMenuPatch } from "@api/ContextMenu"; import { NavContextMenuPatchCallback, addContextMenuPatch, removeContextMenuPatch } from "@api/ContextMenu";
import { definePluginSettings } from "@api/Settings"; import { definePluginSettings } from "@api/Settings";
import { DataStore, Notifications } from "@api/index"; import { DataStore, Notifications } from "@api/index";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types"; import definePlugin, { OptionType } from "@utils/types";
import { ChannelStore, Menu, PresenceStore, PrivateChannelsStore, UserStore } from "@webpack/common"; import { ChannelStore, Menu, PresenceStore, PrivateChannelsStore, UserStore } from "@webpack/common";
import { Channel, Guild, Message, User } from "discord-types/general"; import { Channel, Guild, Message, User } from "discord-types/general";
interface ContextProps { interface ContextProps {
channel: Channel; channel: Channel;
user: User; user: User;
guild: Guild; guild: Guild;
} }
interface IMessageCreate { interface IMessageCreate {
type: "MESSAGE_CREATE"; type: "MESSAGE_CREATE";
optimistic: boolean; optimistic: boolean;
isPushNotification: boolean; isPushNotification: boolean;
channelId: string; channelId: string;
guildId: string; guildId: string;
message: Message; message: Message;
} }
const GuildContext: NavContextMenuPatchCallback = (children, { guild }: ContextProps) => () => { const GuildContext: NavContextMenuPatchCallback = (children, { guild }: ContextProps) => () => {
if (!guild) return; if (!guild) return;
children.splice(-1, 0, ( children.splice(-1, 0, (
<Menu.MenuGroup> <Menu.MenuGroup>
<Menu.MenuItem <Menu.MenuItem
id="dnd-guild-bypass" id="dnd-guild-bypass"
label={`${bypasses["guilds"].includes(guild.id) ? "Remove" : "Add"} DND Bypass`} label={`${bypasses["guilds"].includes(guild.id) ? "Remove" : "Add"} DND Bypass`}
action={async () => { action={async () => {
if (bypasses["guilds"].includes(guild.id)) bypasses["guilds"] = await bypasses["guilds"].filter(id => id !== guild.id); if (bypasses["guilds"].includes(guild.id)) bypasses["guilds"] = await bypasses["guilds"].filter(id => id !== guild.id);
else bypasses["guilds"].push(guild.id); else bypasses["guilds"].push(guild.id);
await DataStore.set("bypassdnd", bypasses); await DataStore.set("bypassdnd", bypasses);
settings.store.guilds = (bypasses["guilds"].join(', ')); settings.store.guilds = (bypasses["guilds"].join(', '));
}} }}
/> />
</Menu.MenuGroup> </Menu.MenuGroup>
)); ));
}; };
const ChannelContext: NavContextMenuPatchCallback = (children, { channel }: ContextProps) => () => { const ChannelContext: NavContextMenuPatchCallback = (children, { channel }: ContextProps) => () => {
if (!channel) return; if (!channel) return;
children.splice(-1, 0, ( children.splice(-1, 0, (
<Menu.MenuGroup> <Menu.MenuGroup>
<Menu.MenuItem <Menu.MenuItem
id="dnd-channel-bypass" id="dnd-channel-bypass"
label={`${bypasses["channels"].includes(channel.id) ? "Remove" : "Add"} DND Bypass`} label={`${bypasses["channels"].includes(channel.id) ? "Remove" : "Add"} DND Bypass`}
action={async () => { action={async () => {
if (bypasses["channels"].includes(channel.id)) bypasses["channels"] = await bypasses["channels"].filter(id => id !== channel.id); if (bypasses["channels"].includes(channel.id)) bypasses["channels"] = await bypasses["channels"].filter(id => id !== channel.id);
else bypasses["channels"].push(channel.id); else bypasses["channels"].push(channel.id);
await DataStore.set("bypassdnd", bypasses); await DataStore.set("bypassdnd", bypasses);
settings.store.channels = (bypasses["channels"].join(', ')); settings.store.channels = (bypasses["channels"].join(', '));
}} }}
/> />
</Menu.MenuGroup> </Menu.MenuGroup>
)); ));
}; };
const UserContext: NavContextMenuPatchCallback = (children, { user }: ContextProps) => () => { const UserContext: NavContextMenuPatchCallback = (children, { user }: ContextProps) => () => {
if (!user) return; if (!user) return;
children.splice(-1, 0, ( children.splice(-1, 0, (
<Menu.MenuGroup> <Menu.MenuGroup>
<Menu.MenuItem <Menu.MenuItem
id="dnd-user-bypass" id="dnd-user-bypass"
label={`${bypasses["users"].includes(user.id) ? "Remove" : "Add"} DND Bypass`} label={`${bypasses["users"].includes(user.id) ? "Remove" : "Add"} DND Bypass`}
action={async () => { action={async () => {
if (bypasses["users"].includes(user.id)) bypasses["users"] = await bypasses["users"].filter(id => id !== user.id); if (bypasses["users"].includes(user.id)) bypasses["users"] = await bypasses["users"].filter(id => id !== user.id);
else bypasses["users"].push(user.id); else bypasses["users"].push(user.id);
await DataStore.set("bypassdnd", bypasses); await DataStore.set("bypassdnd", bypasses);
settings.store.users = (bypasses["users"].join(', ')); settings.store.users = (bypasses["users"].join(', '));
}} }}
/> />
</Menu.MenuGroup> </Menu.MenuGroup>
)); ));
}; };
let bypasses; type Bypasses = {
guilds: string[];
const settings = definePluginSettings({ channels: string[];
guilds: { users: string[];
type: OptionType.STRING, };
description: "Guilds to let bypass (notified when pinged anywhere in guild)",
default: "", let bypasses: Bypasses = {
placeholder: "Separate with commas", guilds: [],
onChange: async function (value) { channels: [],
bypasses["guild"] = value.replace(/\s/g, '').split(',').filter(id => id.trim() !== ''); users: [],
await DataStore.set("bypassdnd", bypasses); };
},
}, const settings = definePluginSettings({
channels: { guilds: {
type: OptionType.STRING, type: OptionType.STRING,
description: "Channels to let bypass (notified when pinged in that channel)", description: "Guilds to let bypass (notified when pinged anywhere in guild)",
default: "", default: "",
placeholder: "Separate with commas", placeholder: "Separate with commas",
onChange: async function (value) { onChange: async function (value) {
bypasses["channels"] = value.replace(/\s/g, '').split(',').filter(id => id.trim() !== ''); bypasses["guilds"] = value.replace(/\s/g, '').split(',').filter(id => id.trim() !== '');
await DataStore.set("bypassdnd", bypasses); await DataStore.set("bypassdnd", bypasses);
}, },
}, },
users: { channels: {
type: OptionType.STRING, type: OptionType.STRING,
description: "Users to let bypass (notified for all messages)", description: "Channels to let bypass (notified when pinged in that channel)",
default: "", default: "",
placeholder: "Separate with commas", placeholder: "Separate with commas",
onChange: async function (value) { onChange: async function (value) {
bypasses["users"] = value.replace(/\s/g, '').split(',').filter(id => id.trim() !== ''); bypasses["channels"] = value.replace(/\s/g, '').split(',').filter(id => id.trim() !== '');
await DataStore.set("bypassdnd", bypasses); await DataStore.set("bypassdnd", bypasses);
}, },
} },
}); users: {
type: OptionType.STRING,
export default definePlugin({ description: "Users to let bypass (notified for all messages)",
name: "BypassDND", default: "",
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.", placeholder: "Separate with commas",
authors: [Devs.Inbestigator], onChange: async function (value) {
flux: { bypasses["users"] = value.replace(/\s/g, '').split(',').filter(id => id.trim() !== '');
async MESSAGE_CREATE({ optimistic, type, message, guildId, channelId }: IMessageCreate) { await DataStore.set("bypassdnd", bypasses);
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; export default definePlugin({
if (await PresenceStore.getStatus(currentUser.id) != 'dnd') return; name: "BypassDND",
if ((bypasses.guilds.includes(guildId) || bypasses.channels.includes(channelId)) && (message.content.includes(`<@${currentUser.id}>`) || message.mentions.some(mention => mention.id === currentUser.id))) { 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.",
await Notifications.showNotification({ authors: [Devs.Inbestigator],
title: `${message.author.globalName ?? message.author.username} sent a message in ${ChannelStore.getChannel(channelId).name}`, flux: {
body: message.content, async MESSAGE_CREATE({ optimistic, type, message, guildId, channelId }: IMessageCreate) {
icon: UserStore.getUser(message.author.id).getAvatarURL(undefined, undefined, false), if (optimistic || type !== "MESSAGE_CREATE") return;
}); if (message.state === "SENDING") return;
return; if (!message.content) return;
} const currentUser = UserStore.getCurrentUser();
if (bypasses.users.includes(message.author.id) && channelId === await PrivateChannelsStore.getOrEnsurePrivateChannel(message.author.id)) { if (message.author.id === currentUser.id) return;
await Notifications.showNotification({ if (await PresenceStore.getStatus(currentUser.id) != 'dnd') return;
title: `${message.author.globalName ?? message.author.username} sent a message in a DM`, if ((bypasses.guilds.includes(guildId) || bypasses.channels.includes(channelId)) && (message.content.includes(`<@${currentUser.id}>`) || message.mentions.some(mention => mention.id === currentUser.id))) {
body: message.content, await Notifications.showNotification({
icon: UserStore.getUser(message.author.id).getAvatarURL(undefined, undefined, false), 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;
settings, }
async start() { if (bypasses.users.includes(message.author.id) && channelId === await PrivateChannelsStore.getOrEnsurePrivateChannel(message.author.id)) {
addContextMenuPatch("guild-context", GuildContext); await Notifications.showNotification({
addContextMenuPatch("channel-context", ChannelContext); title: `${message.author.globalName ?? message.author.username} sent a message in a DM`,
addContextMenuPatch("user-context", UserContext); body: message.content,
bypasses = await DataStore.get("bypassdnd") ?? { guilds: [], channels: [], users: [] }; icon: UserStore.getUser(message.author.id).getAvatarURL(undefined, undefined, false),
await DataStore.set("bypassdnd", bypasses); });
}, }
stop() { }
removeContextMenuPatch("guild-context", GuildContext); },
removeContextMenuPatch("channel-context", ChannelContext); settings,
removeContextMenuPatch("user-context", UserContext); 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);
}
});