From df0a29fc16b27ba510364580480c767ebba0031c Mon Sep 17 00:00:00 2001 From: AntonMacG Date: Mon, 13 Nov 2023 09:38:19 +0000 Subject: [PATCH] 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) {