From e5ffb349f72c8bb05c5c94b50035fdf67be8cfb1 Mon Sep 17 00:00:00 2001 From: AntonMacG Date: Mon, 13 Nov 2023 07:21:20 +0000 Subject: [PATCH 1/8] Added a whitelist (always ping) --- src/plugins/replyPingControl/index.ts | 46 +++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/plugins/replyPingControl/index.ts b/src/plugins/replyPingControl/index.ts index 4cabcc2d2..0fb938f54 100644 --- a/src/plugins/replyPingControl/index.ts +++ b/src/plugins/replyPingControl/index.ts @@ -7,7 +7,7 @@ import { definePluginSettings } from "@api/Settings"; import { Devs } from "@utils/constants"; import definePlugin, { OptionType } from "@utils/types"; -import { MessageStore, UserStore } from "@webpack/common"; +import { MessageStore, showToast, UserStore } from "@webpack/common"; import { MessageJSON } from "discord-types/general"; export const settings = definePluginSettings({ @@ -15,12 +15,18 @@ export const settings = definePluginSettings({ type: OptionType.BOOLEAN, description: "Always get pinged when someone replies to your messages", default: false, + }, + replyPingWhitelist: { + type: OptionType.STRING, + description: "Comma-separated list of User IDs to always receive reply pings from", + default: "", + disabled: () => settings.store.alwaysPingOnReply, } }); export default definePlugin({ name: "ReplyPingControl", - description: "Control whether to always or never get pinged on message replies", + description: "Control whether to always or never get pinged on message replies, with a whitelist feature", authors: [Devs.ant0n, Devs.MrDiamond], settings, @@ -36,13 +42,20 @@ export default definePlugin({ const user = UserStore.getCurrentUser(); if (message.author.id === user.id) return; - if (this.getRepliedMessage(message)?.author.id !== user.id) + + const repliedMessage = this.getRepliedMessage(message); + if (!repliedMessage || repliedMessage.author.id !== user.id) return; - if (!settings.store.alwaysPingOnReply) + const whitelist = parseWhitelist(settings.store.replyPingWhitelist); + const isWhitelisted = whitelist.includes(message.author.id); + + if (isWhitelisted || settings.store.alwaysPingOnReply) { + if (!message.mentions.some(mention => mention.id === user.id)) + message.mentions.push(user as any); + } else { message.mentions = message.mentions.filter(mention => mention.id !== user.id); - else if (!message.mentions.some(mention => mention.id === user.id)) - message.mentions.push(user as any); + } }, getRepliedMessage(message: MessageJSON) { @@ -50,3 +63,24 @@ export default definePlugin({ return ref && MessageStore.getMessage(ref.channel_id, ref.message_id); }, }); + +function validateWhitelist(value: string) { + const whitelist = parseWhitelist(value); + if (whitelist.some(id => !isValidUserId(id))) { + showToast("Invalid User ID: One or more User IDs in the whitelist are invalid. Please check your input."); + return false; + } + showToast("Whitelist Updated: Reply ping whitelist has been successfully updated."); + return true; +} + + +function parseWhitelist(value: string) { + return value.split(",") + .map(id => id.trim()) + .filter(id => id !== ""); +} + +function isValidUserId(id: string) { + return /^\d+$/.test(id); // Validates if the string is a number (basic validation for Discord User IDs) +} From 191bb3cce4617bfa237ef2457a0623090cd38495 Mon Sep 17 00:00:00 2001 From: ant0n Date: Mon, 13 Nov 2023 07:43:38 +0000 Subject: [PATCH 2/8] Removed pipebomb... --- src/plugins/replyPingControl/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/replyPingControl/index.ts b/src/plugins/replyPingControl/index.ts index 0fb938f54..0edd535f3 100644 --- a/src/plugins/replyPingControl/index.ts +++ b/src/plugins/replyPingControl/index.ts @@ -82,5 +82,5 @@ function parseWhitelist(value: string) { } function isValidUserId(id: string) { - return /^\d+$/.test(id); // Validates if the string is a number (basic validation for Discord User IDs) + return /^\d+$/.test(id); } From c65f3dbbbe95c89851a70a8aa9ada33d4b91742e Mon Sep 17 00:00:00 2001 From: AntonMacG Date: Mon, 13 Nov 2023 09:27:21 +0000 Subject: [PATCH 3/8] Updated README.md to include Whitelist --- src/plugins/replyPingControl/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/plugins/replyPingControl/README.md b/src/plugins/replyPingControl/README.md index 1b28ef630..87181da15 100644 --- a/src/plugins/replyPingControl/README.md +++ b/src/plugins/replyPingControl/README.md @@ -6,5 +6,7 @@ Overrides notification settings for received reply pings to either always notify - **Enabled**: You will always be notified when someone replies to your messages, regardless of if the sender has reply pings enabled. - **Disabled (Default)**: You will not receive notification pings for replies, regardless of if the sender has reply pings enabled. +- **Whitelist**: You will be mentioned if any of the userIDs added to this reply to you, regardless of their ping setting or your ping setting. Formatted as comma seperated IDs, e.g. `145224646868860928,1017176847865352332`. + ## License This project is licensed under the GPL-3.0-or-later License - see file for details. From df0a29fc16b27ba510364580480c767ebba0031c Mon Sep 17 00:00:00 2001 From: AntonMacG Date: Mon, 13 Nov 2023 09:38:19 +0000 Subject: [PATCH 4/8] Added a caching for the whitelist --- src/plugins/replyPingControl/index.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/plugins/replyPingControl/index.ts b/src/plugins/replyPingControl/index.ts index 0edd535f3..2ce74bad6 100644 --- a/src/plugins/replyPingControl/index.ts +++ b/src/plugins/replyPingControl/index.ts @@ -10,6 +10,8 @@ import definePlugin, { OptionType } from "@utils/types"; import { MessageStore, showToast, UserStore } from "@webpack/common"; import { MessageJSON } from "discord-types/general"; +let cachedWhitelist: string[] = []; + export const settings = definePluginSettings({ alwaysPingOnReply: { type: OptionType.BOOLEAN, @@ -21,6 +23,14 @@ export const settings = definePluginSettings({ description: "Comma-separated list of User IDs to always receive reply pings from", default: "", disabled: () => settings.store.alwaysPingOnReply, + onChange: newValue => { + cachedWhitelist = parseWhitelist(newValue); + if (!validateWhitelist(newValue)) { + showToast("Invalid User ID: One or more User IDs in the whitelist are invalid. Please check your input."); + } else { + showToast("Whitelist Updated: Reply ping whitelist has been successfully updated."); + } + } } }); @@ -47,8 +57,7 @@ export default definePlugin({ if (!repliedMessage || repliedMessage.author.id !== user.id) return; - const whitelist = parseWhitelist(settings.store.replyPingWhitelist); - const isWhitelisted = whitelist.includes(message.author.id); + const isWhitelisted = cachedWhitelist.includes(message.author.id); if (isWhitelisted || settings.store.alwaysPingOnReply) { if (!message.mentions.some(mention => mention.id === user.id)) @@ -66,19 +75,13 @@ export default definePlugin({ function validateWhitelist(value: string) { const whitelist = parseWhitelist(value); - if (whitelist.some(id => !isValidUserId(id))) { - showToast("Invalid User ID: One or more User IDs in the whitelist are invalid. Please check your input."); - return false; - } - showToast("Whitelist Updated: Reply ping whitelist has been successfully updated."); - return true; + return !whitelist.some(id => !isValidUserId(id)); } - function parseWhitelist(value: string) { return value.split(",") .map(id => id.trim()) - .filter(id => id !== ""); + .filter(id => id !== "" && isValidUserId(id)); } function isValidUserId(id: string) { From 0e494fc22bafd56b1f786985cdcfa00fe634abc6 Mon Sep 17 00:00:00 2001 From: AntonMacG Date: Mon, 13 Nov 2023 09:49:50 +0000 Subject: [PATCH 5/8] Updated validateWhitelist --- src/plugins/replyPingControl/index.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/plugins/replyPingControl/index.ts b/src/plugins/replyPingControl/index.ts index 2ce74bad6..3c9cdd15f 100644 --- a/src/plugins/replyPingControl/index.ts +++ b/src/plugins/replyPingControl/index.ts @@ -75,7 +75,12 @@ export default definePlugin({ function validateWhitelist(value: string) { const whitelist = parseWhitelist(value); - return !whitelist.some(id => !isValidUserId(id)); + if (whitelist.length === 0 && value.trim() !== "") { + showToast("Invalid User ID: One or more User IDs in the whitelist are invalid. Please check your input."); + return false; + } + showToast("Whitelist Updated: Reply ping whitelist has been successfully updated."); + return true; } function parseWhitelist(value: string) { From 400e9705e5ec1dfb47277b72eb8c1487c2c7ee9c Mon Sep 17 00:00:00 2001 From: AntonMacG Date: Mon, 13 Nov 2023 10:12:31 +0000 Subject: [PATCH 6/8] :husk: --- src/plugins/replyPingControl/index.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/plugins/replyPingControl/index.ts b/src/plugins/replyPingControl/index.ts index 3c9cdd15f..a6aaf7f90 100644 --- a/src/plugins/replyPingControl/index.ts +++ b/src/plugins/replyPingControl/index.ts @@ -24,10 +24,12 @@ export const settings = definePluginSettings({ default: "", disabled: () => settings.store.alwaysPingOnReply, onChange: newValue => { - cachedWhitelist = parseWhitelist(newValue); - if (!validateWhitelist(newValue)) { + const newWhitelist = parseWhitelist(newValue); + + if (newWhitelist.length === 0 && newValue.trim() !== "") { showToast("Invalid User ID: One or more User IDs in the whitelist are invalid. Please check your input."); } else { + cachedWhitelist = newWhitelist; showToast("Whitelist Updated: Reply ping whitelist has been successfully updated."); } } @@ -73,16 +75,6 @@ export default definePlugin({ }, }); -function validateWhitelist(value: string) { - const whitelist = parseWhitelist(value); - if (whitelist.length === 0 && value.trim() !== "") { - showToast("Invalid User ID: One or more User IDs in the whitelist are invalid. Please check your input."); - return false; - } - showToast("Whitelist Updated: Reply ping whitelist has been successfully updated."); - return true; -} - function parseWhitelist(value: string) { return value.split(",") .map(id => id.trim()) From 4ac6d97ec8eb9fefd5fe23fa0ac48701b439062b Mon Sep 17 00:00:00 2001 From: AntonMacG Date: Mon, 13 Nov 2023 10:17:33 +0000 Subject: [PATCH 7/8] Fixed validation fail if at least one ID was valid --- src/plugins/replyPingControl/index.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/plugins/replyPingControl/index.ts b/src/plugins/replyPingControl/index.ts index a6aaf7f90..a2a66e7fa 100644 --- a/src/plugins/replyPingControl/index.ts +++ b/src/plugins/replyPingControl/index.ts @@ -24,12 +24,13 @@ export const settings = definePluginSettings({ default: "", disabled: () => settings.store.alwaysPingOnReply, onChange: newValue => { - const newWhitelist = parseWhitelist(newValue); + const originalIDs = newValue.split(",").map(id => id.trim()).filter(id => id !== ""); + const validatedIDs = originalIDs.filter(isValidUserId); - if (newWhitelist.length === 0 && newValue.trim() !== "") { + if (originalIDs.length !== validatedIDs.length) { showToast("Invalid User ID: One or more User IDs in the whitelist are invalid. Please check your input."); } else { - cachedWhitelist = newWhitelist; + cachedWhitelist = validatedIDs; showToast("Whitelist Updated: Reply ping whitelist has been successfully updated."); } } @@ -78,7 +79,7 @@ export default definePlugin({ function parseWhitelist(value: string) { return value.split(",") .map(id => id.trim()) - .filter(id => id !== "" && isValidUserId(id)); + .filter(id => id !== ""); } function isValidUserId(id: string) { From b20501dcfecc26c9b22eec61f2843b7eac0a309c Mon Sep 17 00:00:00 2001 From: AntonMacG Date: Mon, 13 Nov 2023 10:20:58 +0000 Subject: [PATCH 8/8] No longer creats two arrays onChange --- src/plugins/replyPingControl/index.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/plugins/replyPingControl/index.ts b/src/plugins/replyPingControl/index.ts index a2a66e7fa..6eeafc3db 100644 --- a/src/plugins/replyPingControl/index.ts +++ b/src/plugins/replyPingControl/index.ts @@ -24,13 +24,16 @@ export const settings = definePluginSettings({ default: "", disabled: () => settings.store.alwaysPingOnReply, onChange: newValue => { - const originalIDs = newValue.split(",").map(id => id.trim()).filter(id => id !== ""); - const validatedIDs = originalIDs.filter(isValidUserId); + const originalIDs = newValue.split(",") + .map(id => id.trim()) + .filter(id => id !== ""); - if (originalIDs.length !== validatedIDs.length) { + const isInvalid = originalIDs.some(id => !isValidUserId(id)); + + if (isInvalid) { showToast("Invalid User ID: One or more User IDs in the whitelist are invalid. Please check your input."); } else { - cachedWhitelist = validatedIDs; + cachedWhitelist = originalIDs; showToast("Whitelist Updated: Reply ping whitelist has been successfully updated."); } }