From ffe182e504ed88e755686f0fc2f2902d315167e2 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 19 Jun 2023 17:40:06 +0100 Subject: [PATCH 1/3] 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(); -- 2.43.4 From b0407db9bbc7fee197a883cd862bca57e5db3a4b Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 19 Jun 2023 17:49:16 +0100 Subject: [PATCH 2/3] Update time length to ignore commas --- src/helpers/StringTools.ts | 4 ++++ src/helpers/TimeLengthInput.ts | 4 +++- src/vylbot.ts | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/helpers/StringTools.ts b/src/helpers/StringTools.ts index 5119f94..0254dc5 100644 --- a/src/helpers/StringTools.ts +++ b/src/helpers/StringTools.ts @@ -35,4 +35,8 @@ export default class StringTools { return result; } + + public static ReplaceAll(str: string, find: string, replace: string) { + return str.replace(new RegExp(find, 'g'), replace); + } } \ No newline at end of file diff --git a/src/helpers/TimeLengthInput.ts b/src/helpers/TimeLengthInput.ts index 7c167d2..95befa6 100644 --- a/src/helpers/TimeLengthInput.ts +++ b/src/helpers/TimeLengthInput.ts @@ -1,8 +1,10 @@ +import StringTools from "./StringTools"; + export default class TimeLengthInput { public readonly value: string; constructor(input: string) { - this.value = input; + this.value = StringTools.ReplaceAll(input, ',', ''); } public GetDays(): number { diff --git a/src/vylbot.ts b/src/vylbot.ts index c1d2997..8cfd848 100644 --- a/src/vylbot.ts +++ b/src/vylbot.ts @@ -2,6 +2,7 @@ import { CoreClient } from "./client/client"; import * as dotenv from "dotenv"; import registry from "./registry"; import { IntentsBitField } from "discord.js"; +import StringTools from "./helpers/StringTools"; dotenv.config(); -- 2.43.4 From b82e74bc687ae0d747fe55aaea3c5f6826d4c97e Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 26 Jun 2023 18:00:00 +0100 Subject: [PATCH 3/3] Remove unused import statement --- src/vylbot.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/vylbot.ts b/src/vylbot.ts index 8cfd848..668c8e5 100644 --- a/src/vylbot.ts +++ b/src/vylbot.ts @@ -2,7 +2,6 @@ import { CoreClient } from "./client/client"; import * as dotenv from "dotenv"; import registry from "./registry"; import { IntentsBitField } from "discord.js"; -import StringTools from "./helpers/StringTools"; dotenv.config(); @@ -36,4 +35,4 @@ const client = new CoreClient([ registry.RegisterCommands(); registry.RegisterEvents(); -client.start(); \ No newline at end of file +client.start(); -- 2.43.4