From ffe182e504ed88e755686f0fc2f2902d315167e2 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 19 Jun 2023 17:40:06 +0100 Subject: [PATCH] Prevent user from timing out a bot --- src/commands/timeout.ts | 5 +++++ tests/commands/timeout.test.ts | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/commands/timeout.ts b/src/commands/timeout.ts index 65e3ba0..52fe1e7 100644 --- a/src/commands/timeout.ts +++ b/src/commands/timeout.ts @@ -49,6 +49,11 @@ export default class Timeout extends Command { return; } + if (targetUser.user.bot) { + await interaction.reply('Cannot timeout bots.'); + return; + } + // General Variables const targetMember = targetUser.member as GuildMember; const reason = reasonInput && reasonInput.value ? reasonInput.value.toString() : null; diff --git a/tests/commands/timeout.test.ts b/tests/commands/timeout.test.ts index c64ceb5..1646734 100644 --- a/tests/commands/timeout.test.ts +++ b/tests/commands/timeout.test.ts @@ -360,6 +360,43 @@ describe('execute', () => { expect(interaction.reply).toBeCalledWith('Fields are required.'); }); + test('GIVEN targetUser is a bot, EXPECT error', async () => { + const interaction = { + reply: jest.fn(), + guild: mock(), + guildId: 'guildId', + options: { + get: jest.fn((value: string): CommandInteractionOption | null => { + switch (value) { + case 'target': + return { + user: { + bot: true, + } as User, + member: {} as GuildMember + } as CommandInteractionOption; + case 'length': + return { + value: '1m', + } as CommandInteractionOption; + case 'reason': + return { + value: 'Test reason', + } as CommandInteractionOption; + default: + return null; + } + }), + } + } as unknown as CommandInteraction; + + const command = new Timeout(); + + await command.execute(interaction); + + expect(interaction.reply).toBeCalledWith('Cannot timeout bots.'); + }); + test('GIVEN targetMember IS NOT manageable by the bot, EXPECT insufficient permissions error', async () => { const command = new Timeout();