From e5ffb349f72c8bb05c5c94b50035fdf67be8cfb1 Mon Sep 17 00:00:00 2001 From: AntonMacG Date: Mon, 13 Nov 2023 07:21:20 +0000 Subject: [PATCH] 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) +}