Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions modules/ping-protection/commands/ping-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion modules/ping-protection/commands/view-moderation-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion modules/ping-protection/commands/view-ping-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 30 additions & 2 deletions modules/ping-protection/configs/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions modules/ping-protection/events/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))) ||
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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;
Expand Down
53 changes: 47 additions & 6 deletions tests/ping-protection/command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,56 @@ 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);
expect(mockHistory).not.toHaveBeenCalled();
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);
expect(mockActions).not.toHaveBeenCalled();
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',
Expand All @@ -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', () => {
Expand Down
84 changes: 70 additions & 14 deletions tests/ping-protection/messageCreate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ function makeConfig(over = {}) {
protectAllUsersWithProtectedRole: false,
allowReplyPings: false,
selfPingConfiguration: 'Off',
selfPingEasterEgg: false,
botPingEasterEgg: false,
...over
};
}
Expand All @@ -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: {
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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'};
Expand Down
Loading
Loading