From 172b754ab96a11d19523e27f00813d5b5f6472f8 Mon Sep 17 00:00:00 2001 From: Inbestigator Date: Fri, 12 Apr 2024 22:04:02 -0700 Subject: [PATCH] Added notification sound (thanks to Drakz for the snippet) --- src/plugins/bypassDND/index.tsx | 53 +++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/plugins/bypassDND/index.tsx b/src/plugins/bypassDND/index.tsx index 8d6bd5248..eb2446558 100644 --- a/src/plugins/bypassDND/index.tsx +++ b/src/plugins/bypassDND/index.tsx @@ -12,11 +12,7 @@ import { getCurrentChannel } from "@utils/discord"; import { Logger } from "@utils/Logger"; import definePlugin, { OptionType } from "@utils/types"; import { ChannelStore, Menu, MessageStore, NavigationRouter, PresenceStore, PrivateChannelsStore, UserStore, WindowStore } from "@webpack/common"; -import type { Message, User as DiscordUser } from "discord-types/general"; - -interface User extends DiscordUser { - globalName: string; -} +import type { Message } from "discord-types/general"; interface IMessageCreate { channelId: string; @@ -39,26 +35,34 @@ function processIds(value: string): string { } async function showNotification(message: Message, guildId: string | undefined): Promise { - const channel = ChannelStore.getChannel(message.channel_id); - const channelRegex = /<#(\d{19})>/g; - const userRegex = /<@(\d{18})>/g; + try { + const channel = ChannelStore.getChannel(message.channel_id); + const channelRegex = /<#(\d{19})>/g; + const userRegex = /<@(\d{18})>/g; - message.content = message.content.replace(channelRegex, (match, channelId: string) => { - return `#${ChannelStore.getChannel(channelId)?.name}`; - }); + message.content = message.content.replace(channelRegex, (match: any, channelId: string) => { + return `#${ChannelStore.getChannel(channelId)?.name}`; + }); - message.content = message.content.replace(userRegex, (match, userId: string) => { - return `@${(UserStore.getUser(userId) as User).globalName}`; - }); + message.content = message.content.replace(userRegex, (match: any, userId: string) => { + return `@${(UserStore.getUser(userId) as any).globalName}`; + }); - await Notifications.showNotification({ - title: `${(message.author as User).globalName} ${guildId ? `(#${channel?.name}, ${ChannelStore.getChannel(channel?.parent_id)?.name})` : ""}`, - body: message.content, - icon: UserStore.getUser(message.author.id).getAvatarURL(undefined, undefined, false), - onClick: function (): void { - NavigationRouter.transitionTo(`/channels/${guildId ?? "@me"}/${message.channel_id}/${message.id}`); + await Notifications.showNotification({ + title: `${(message.author as any).globalName} ${guildId ? `(#${channel?.name}, ${ChannelStore.getChannel(channel?.parent_id)?.name})` : ""}`, + body: message.content, + icon: UserStore.getUser(message.author.id).getAvatarURL(undefined, undefined, false), + onClick: function (): void { + NavigationRouter.transitionTo(`/channels/${guildId ?? "@me"}/${message.channel_id}/${message.id}`); + } + }); + + if (settings.store.notificationSound) { + new Audio("https://discord.com/assets/9422aef94aa931248105.mp3").play(); } - }); + } catch (error) { + new Logger("BypassDND").error("Failed to notify user: ", error); + } } function ContextCallback(name: "guild" | "user" | "channel"): NavContextMenuPatchCallback { @@ -110,6 +114,11 @@ const settings = definePluginSettings({ allowOutsideOfDms: { 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)" + }, + notificationSound: { + type: OptionType.BOOLEAN, + description: "Whether the notification sound should be played", + default: true, } }); @@ -136,7 +145,7 @@ export default definePlugin({ } } } catch (error) { - new Logger("BypassDND").error("Failed to handle message", error); + new Logger("BypassDND").error("Failed to handle message: ", error); } } },