From 9077660b3b545a0c8d4a33831f272a8bf47d5cdd Mon Sep 17 00:00:00 2001 From: Kevinking500 Date: Fri, 20 Nov 2026 14:21:08 +0100 Subject: [PATCH] Ping Protection V2.6 --- .../commands/ping-protection.js | 9 +- .../commands/view-moderation-history.js | 3 +- .../commands/view-ping-history.js | 3 +- .../configs/configuration.json | 32 ++++++- .../ping-protection/events/messageCreate.js | 8 +- tests/ping-protection/command.test.js | 53 ++++++++++-- tests/ping-protection/messageCreate.test.js | 84 +++++++++++++++---- .../viewModerationHistory.test.js | 28 ++++++- tests/ping-protection/viewPingHistory.test.js | 28 ++++++- 9 files changed, 214 insertions(+), 34 deletions(-) diff --git a/modules/ping-protection/commands/ping-protection.js b/modules/ping-protection/commands/ping-protection.js index bcb8e3c6..09798a8e 100644 --- a/modules/ping-protection/commands/ping-protection.js +++ b/modules/ping-protection/commands/ping-protection.js @@ -33,7 +33,8 @@ module.exports.subcommands = { }); } const user = interaction.options.getUser('user'); - if (user.id === interaction.client.user?.id) { + const config = interaction.client.configurations['ping-protection']?.['configuration'] || {}; + if (config.botHistoryEasterEgg && user.id === interaction.client.user?.id) { return interaction.reply({ content: localize('ping-protection', 'meme-bot-history'), flags: MessageFlags.Ephemeral @@ -54,7 +55,8 @@ module.exports.subcommands = { }); } const user = interaction.options.getUser('user'); - if (user.id === interaction.client.user?.id) { + const config = interaction.client.configurations['ping-protection']?.['configuration'] || {}; + if (config.botHistoryEasterEgg && user.id === interaction.client.user?.id) { return interaction.reply({ content: localize('ping-protection', 'meme-bot-history'), flags: MessageFlags.Ephemeral @@ -75,7 +77,8 @@ module.exports.subcommands = { }); } const user = interaction.options.getUser('user'); - if (user.id === interaction.client.user?.id) { + const config = interaction.client.configurations['ping-protection']?.['configuration'] || {}; + if (config.botHistoryEasterEgg && user.id === interaction.client.user?.id) { return interaction.reply({ content: localize('ping-protection', 'meme-bot-history'), flags: MessageFlags.Ephemeral diff --git a/modules/ping-protection/commands/view-moderation-history.js b/modules/ping-protection/commands/view-moderation-history.js index 600eb19e..6fbf2df6 100644 --- a/modules/ping-protection/commands/view-moderation-history.js +++ b/modules/ping-protection/commands/view-moderation-history.js @@ -16,7 +16,8 @@ module.exports.config = { * the output (embed + pagination buttons) is identical for the targeted user. */ module.exports.run = async function (interaction) { - if (interaction.targetUser.id === interaction.client.user?.id) { + const config = interaction.client.configurations?.['ping-protection']?.['configuration'] || {}; + if (config.botHistoryEasterEgg && interaction.targetUser.id === interaction.client.user?.id) { return interaction.reply({ content: localize('ping-protection', 'meme-bot-history'), flags: MessageFlags.Ephemeral diff --git a/modules/ping-protection/commands/view-ping-history.js b/modules/ping-protection/commands/view-ping-history.js index d9384ef0..fb100fce 100644 --- a/modules/ping-protection/commands/view-ping-history.js +++ b/modules/ping-protection/commands/view-ping-history.js @@ -16,7 +16,8 @@ module.exports.config = { * (embed + pagination buttons) is identical for the targeted user. */ module.exports.run = async function (interaction) { - if (interaction.targetUser.id === interaction.client.user?.id) { + const config = interaction.client.configurations?.['ping-protection']?.['configuration'] || {}; + if (config.botHistoryEasterEgg && interaction.targetUser.id === interaction.client.user?.id) { return interaction.reply({ content: localize('ping-protection', 'meme-bot-history'), flags: MessageFlags.Ephemeral diff --git a/modules/ping-protection/configs/configuration.json b/modules/ping-protection/configs/configuration.json index cbae2498..fea33641 100644 --- a/modules/ping-protection/configs/configuration.json +++ b/modules/ping-protection/configs/configuration.json @@ -18,6 +18,11 @@ "icon": "fas fa-gears", "displayName": "Ping rules" }, + { + "id": "easterEggs", + "icon": "fa-solid fa-egg", + "displayName": "Easter eggs" + }, { "id": "automod", "icon": "far fa-robot", @@ -107,11 +112,34 @@ "type": "select", "content": [ "Get punished like normal members", - "Ignored", - "Get fun easter eggs when pinging themselves" + "Ignored" ], "default": "Ignored" }, + { + "name": "selfPingEasterEgg", + "category": "easterEggs", + "humanName": "Enable self-ping easter egg", + "description": "Adds a fun twist to protected users. When enabled, protected users pinging themselves see a fun easter egg. Overrides self-ping configuration setting - if enabled, no logging is done.", + "type": "boolean", + "default": false + }, + { + "name": "botPingEasterEgg", + "category": "easterEggs", + "humanName": "Enable bot-ping easter egg", + "description": "When enabled, users pinging the bot will see a fun easter egg.", + "type": "boolean", + "default": false + }, + { + "name": "botHistoryEasterEgg", + "category": "easterEggs", + "humanName": "Enable bot-history easter egg", + "description": "When enabled, checking the ping or moderation history of the bot will show a fun easter egg instead of an empty history.", + "type": "boolean", + "default": false + }, { "name": "enableAutomod", "category": "automod", diff --git a/modules/ping-protection/events/messageCreate.js b/modules/ping-protection/events/messageCreate.js index 35d1fef0..dd3b06a4 100644 --- a/modules/ping-protection/events/messageCreate.js +++ b/modules/ping-protection/events/messageCreate.js @@ -76,6 +76,7 @@ module.exports.run = async function (client, message) { // Easter egg when a user pings the bot const botId = client.user?.id; let shouldSendBotMeme = Boolean( + config.botPingEasterEgg && botId && ( (typeof message.mentions.users.has === 'function' && message.mentions.users.has(botId)) || (typeof message.mentions.users.get === 'function' && Boolean(message.mentions.users.get(botId))) || @@ -112,9 +113,8 @@ module.exports.run = async function (client, message) { if (!target) return; - // Funny easter egg when they ping themselves - if (target.id === message.author.id && config.selfPingConfiguration === 'Ignored') return; - if (target.id === message.author.id && config.selfPingConfiguration === 'Get fun easter eggs when pinging themselves') { + // Self-ping handling: easter egg overrides self-ping configuration and does no logging + if (target.id === message.author.id && config.selfPingEasterEgg) { const secretChance = 0.01; // Secret for a reason.. (1% chance) const standardMemes = [ localize('ping-protection', 'meme-why'), @@ -155,6 +155,8 @@ module.exports.run = async function (client, message) { return; } + if (target.id === message.author.id && config.selfPingConfiguration === 'Ignored') return; + await sendPingWarning(client, message, target, config); const isRole = !target.username; diff --git a/tests/ping-protection/command.test.js b/tests/ping-protection/command.test.js index 6a8d5aa4..a9d37788 100644 --- a/tests/ping-protection/command.test.js +++ b/tests/ping-protection/command.test.js @@ -119,11 +119,12 @@ describe('user subcommands', () => { expect(mockPanel).toHaveBeenCalledWith(interaction.client, user); }); - test('triggers easter egg when checking the bot itself in user.history', async () => { + test('triggers easter egg when checking the bot itself in user.history with botHistoryEasterEgg enabled', async () => { const interaction = makeInteraction({ group: 'user', sub: 'history', - user: {id: 'botId'} + user: {id: 'botId'}, + config: {botHistoryEasterEgg: true} }); interaction.client.user = {id: 'botId'}; await runCommand(interaction); @@ -131,11 +132,24 @@ describe('user subcommands', () => { expect(interaction.reply.mock.calls[0][0].content).toContain('ping-protection.meme-bot-history'); }); - test('triggers easter egg when checking the bot itself in user.actions-history', async () => { + test('generates history when checking the bot itself in user.history with botHistoryEasterEgg disabled', async () => { + const interaction = makeInteraction({ + group: 'user', + sub: 'history', + user: {id: 'botId'}, + config: {botHistoryEasterEgg: false} + }); + interaction.client.user = {id: 'botId'}; + await runCommand(interaction); + expect(mockHistory).toHaveBeenCalledWith(interaction.client, 'botId', 1); + }); + + test('triggers easter egg when checking the bot itself in user.actions-history with botHistoryEasterEgg enabled', async () => { const interaction = makeInteraction({ group: 'user', sub: 'actions-history', - user: {id: 'botId'} + user: {id: 'botId'}, + config: {botHistoryEasterEgg: true} }); interaction.client.user = {id: 'botId'}; await runCommand(interaction); @@ -143,6 +157,18 @@ describe('user subcommands', () => { expect(interaction.reply.mock.calls[0][0].content).toContain('ping-protection.meme-bot-history'); }); + test('generates actions history when checking the bot itself in user.actions-history with botHistoryEasterEgg disabled', async () => { + const interaction = makeInteraction({ + group: 'user', + sub: 'actions-history', + user: {id: 'botId'}, + config: {botHistoryEasterEgg: false} + }); + interaction.client.user = {id: 'botId'}; + await runCommand(interaction); + expect(mockActions).toHaveBeenCalledWith(interaction.client, 'botId', 1); + }); + test('blocks user.panel for users without ManageGuild or Admin', async () => { const interaction = makeInteraction({ group: 'user', @@ -155,18 +181,33 @@ describe('user subcommands', () => { expect(interaction.reply.mock.calls[0][0].content).toContain('no-permission'); }); - test('triggers easter egg when checking the bot itself in user.panel', async () => { + test('triggers easter egg when checking the bot itself in user.panel with botHistoryEasterEgg enabled', async () => { const interaction = makeInteraction({ group: 'user', sub: 'panel', user: {id: 'botId'}, - permissions: ['Administrator'] + permissions: ['Administrator'], + config: {botHistoryEasterEgg: true} }); interaction.client.user = {id: 'botId'}; await runCommand(interaction); expect(mockPanel).not.toHaveBeenCalled(); expect(interaction.reply.mock.calls[0][0].content).toContain('ping-protection.meme-bot-history'); }); + + test('generates panel when checking the bot itself in user.panel with botHistoryEasterEgg disabled', async () => { + const user = {id: 'botId'}; + const interaction = makeInteraction({ + group: 'user', + sub: 'panel', + user, + permissions: ['Administrator'], + config: {botHistoryEasterEgg: false} + }); + interaction.client.user = {id: 'botId'}; + await runCommand(interaction); + expect(mockPanel).toHaveBeenCalledWith(interaction.client, user); + }); }); describe('toggle subcommand', () => { diff --git a/tests/ping-protection/messageCreate.test.js b/tests/ping-protection/messageCreate.test.js index 3746686b..589bfa61 100644 --- a/tests/ping-protection/messageCreate.test.js +++ b/tests/ping-protection/messageCreate.test.js @@ -46,6 +46,8 @@ function makeConfig(over = {}) { protectAllUsersWithProtectedRole: false, allowReplyPings: false, selfPingConfiguration: 'Off', + selfPingEasterEgg: false, + botPingEasterEgg: false, ...over }; } @@ -59,15 +61,15 @@ function makeClient(config) { } function makeMessage({ - authorId = 'pinger', - bot = false, - users = [], - roles = [], - repliedUser = null, - members = [], - content = '', - memberRoles = [] - } = {}) { + authorId = 'pinger', + bot = false, + users = [], + roles = [], + repliedUser = null, + members = [], + content = '', + memberRoles = [] +} = {}) { const memberCollection = makeCollection(members); return { guild: { @@ -106,7 +108,7 @@ beforeEach(() => { describe('guards', () => { test('ignores messages before botReady', async () => { const client = makeClient(makeConfig()); - client.botReadyAt = undefined; + delete client.botReadyAt; await handler.run(client, makeMessage()); expect(mockProcessPing).not.toHaveBeenCalled(); }); @@ -221,10 +223,11 @@ describe('protected ping dispatch', () => { }); describe('self-ping', () => { - test('does nothing when selfPingConfiguration is Ignored', async () => { + test('does nothing when selfPingConfiguration is Ignored and easter egg is disabled', async () => { const client = makeClient(makeConfig({ protectedUsers: ['pinger'], - selfPingConfiguration: 'Ignored' + selfPingConfiguration: 'Ignored', + selfPingEasterEgg: false })); const msg = makeMessage({ authorId: 'pinger', @@ -234,14 +237,52 @@ describe('self-ping', () => { }] }); await handler.run(client, msg); + expect(msg.reply).not.toHaveBeenCalled(); + expect(mockSendWarning).not.toHaveBeenCalled(); + expect(mockProcessPing).not.toHaveBeenCalled(); + }); + + test('punishes when selfPingConfiguration is "Get punished like normal members" and easter egg is disabled', async () => { + const client = makeClient(makeConfig({ + protectedUsers: ['pinger'], + selfPingConfiguration: 'Get punished like normal members', + selfPingEasterEgg: false + })); + const msg = makeMessage({ + authorId: 'pinger', + users: [{ + id: 'pinger', + username: 'Me' + }] + }); + await handler.run(client, msg); + expect(mockSendWarning).toHaveBeenCalled(); + expect(mockProcessPing).toHaveBeenCalled(); + }); + + test('replies with an easter egg when selfPingEasterEgg is true regardless of selfPingConfiguration', async () => { + const client = makeClient(makeConfig({ + protectedUsers: ['pinger'], + selfPingConfiguration: 'Get punished like normal members', + selfPingEasterEgg: true + })); + const msg = makeMessage({ + authorId: 'pinger', + users: [{ + id: 'pinger', + username: 'Me' + }] + }); + await handler.run(client, msg); + expect(msg.reply).toHaveBeenCalled(); expect(mockSendWarning).not.toHaveBeenCalled(); expect(mockProcessPing).not.toHaveBeenCalled(); }); }); describe('bot ping easter egg', () => { - test('replies with an easter egg when a user pings the bot', async () => { - const client = makeClient(makeConfig()); + test('replies with an easter egg when a user pings the bot and botPingEasterEgg is true', async () => { + const client = makeClient(makeConfig({botPingEasterEgg: true})); client.user = {id: 'botId'}; const msg = makeMessage({ authorId: 'regularUser', @@ -263,6 +304,21 @@ describe('bot ping easter egg', () => { expect(mockSendWarning).not.toHaveBeenCalled(); }); + test('does not reply with an easter egg when botPingEasterEgg is false', async () => { + const client = makeClient(makeConfig({botPingEasterEgg: false})); + client.user = {id: 'botId'}; + const msg = makeMessage({ + authorId: 'regularUser', + users: [{ + id: 'botId', + username: 'SecurityBot' + }], + content: 'hello <@botId>' + }); + await handler.run(client, msg); + expect(msg.reply).not.toHaveBeenCalled(); + }); + test('ignores automatic reply without manual ping when allowReplyPings is true', async () => { const client = makeClient(makeConfig({allowReplyPings: true})); client.user = {id: 'botId'}; diff --git a/tests/ping-protection/viewModerationHistory.test.js b/tests/ping-protection/viewModerationHistory.test.js index 06c1d829..d16ad3c2 100644 --- a/tests/ping-protection/viewModerationHistory.test.js +++ b/tests/ping-protection/viewModerationHistory.test.js @@ -41,12 +41,17 @@ describe('View Moderation History context command', () => { }); }); - test('returns easter egg when targeting the bot itself', async () => { + test('returns easter egg when targeting the bot itself with botHistoryEasterEgg enabled', async () => { const reply = jest.fn().mockResolvedValue('ok'); const interaction = { client: { id: 'client', - user: {id: 'botId'} + user: {id: 'botId'}, + configurations: { + 'ping-protection': { + configuration: {botHistoryEasterEgg: true} + } + } }, targetUser: {id: 'botId'}, reply @@ -55,4 +60,23 @@ describe('View Moderation History context command', () => { expect(mockGenerateActionsResponse).not.toHaveBeenCalled(); expect(reply.mock.calls[0][0].content).toContain('ping-protection.meme-bot-history'); }); + + test('generates actions history when targeting the bot itself with botHistoryEasterEgg disabled', async () => { + const reply = jest.fn().mockResolvedValue('ok'); + const interaction = { + client: { + id: 'client', + user: {id: 'botId'}, + configurations: { + 'ping-protection': { + configuration: {botHistoryEasterEgg: false} + } + } + }, + targetUser: {id: 'botId'}, + reply + }; + await command.run(interaction); + expect(mockGenerateActionsResponse).toHaveBeenCalledWith(interaction.client, 'botId', 1); + }); }); \ No newline at end of file diff --git a/tests/ping-protection/viewPingHistory.test.js b/tests/ping-protection/viewPingHistory.test.js index 262c5728..dbec982e 100644 --- a/tests/ping-protection/viewPingHistory.test.js +++ b/tests/ping-protection/viewPingHistory.test.js @@ -41,12 +41,17 @@ describe('View Ping History context command', () => { }); }); - test('returns easter egg when targeting the bot itself', async () => { + test('returns easter egg when targeting the bot itself with botHistoryEasterEgg enabled', async () => { const reply = jest.fn().mockResolvedValue('ok'); const interaction = { client: { id: 'client', - user: {id: 'botId'} + user: {id: 'botId'}, + configurations: { + 'ping-protection': { + configuration: {botHistoryEasterEgg: true} + } + } }, targetUser: {id: 'botId'}, reply @@ -55,4 +60,23 @@ describe('View Ping History context command', () => { expect(mockGenerateHistoryResponse).not.toHaveBeenCalled(); expect(reply.mock.calls[0][0].content).toContain('ping-protection.meme-bot-history'); }); + + test('generates history when targeting the bot itself with botHistoryEasterEgg disabled', async () => { + const reply = jest.fn().mockResolvedValue('ok'); + const interaction = { + client: { + id: 'client', + user: {id: 'botId'}, + configurations: { + 'ping-protection': { + configuration: {botHistoryEasterEgg: false} + } + } + }, + targetUser: {id: 'botId'}, + reply + }; + await command.run(interaction); + expect(mockGenerateHistoryResponse).toHaveBeenCalledWith(interaction.client, 'botId', 1); + }); }); \ No newline at end of file