Skip to content

Commit

Permalink
fix: disallow duplicate dwc (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeadTriXz authored Sep 16, 2023
1 parent 80e1516 commit a4ad090
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { type APIUser, PermissionFlagsBits, MessageFlags, type APIRole, OverwriteType } from "@discordjs/core";
import {
type APIUser,
type APIRole,
MessageFlags,
OverwriteType,
PermissionFlagsBits
} from "@discordjs/core";
import {
type ApplicationCommandInteraction,
SlashCommand,
Expand Down Expand Up @@ -90,6 +96,14 @@ export default class extends SlashCommand<ModerationModule> {
});
}

const dwc = await this.module.dwcScheduledBans.get(interaction.guildID, options.user.id);
if (dwc !== null) {
return interaction.createMessage({
content: `${config.emotes.error} That user is already flagged.`,
flags: MessageFlags.Ephemeral
});
}

const guild = await this.client.api.guilds.get(interaction.guildID);
const member = interaction.data.resolved.members.get(options.user.id);
if (member !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,17 @@ describe("/dwc", () => {

profilesModule.flagUser = vi.fn();
requestsModule.flagUser = vi.fn();
vi.spyOn(module.dwcScheduledBans, "get").mockResolvedValue(null);
vi.spyOn(module.moderationSettings, "getOrCreate").mockResolvedValue(settings);
vi.spyOn(module.cases, "create").mockResolvedValue(entity);
vi.spyOn(profilesModule.profilesSettings, "getOrCreate").mockResolvedValue(profilesSettings);
vi.spyOn(requestsModule.requestsSettings, "getOrCreate").mockResolvedValue(requestsSettings);
});

afterEach(() => {
vi.restoreAllMocks();
});

describe("execute", () => {
beforeEach(() => {
command.client.api.guilds.addRoleToMember = vi.fn();
Expand Down Expand Up @@ -287,6 +292,19 @@ describe("/dwc", () => {
expect(interaction.acknowledged).toBe(false);
});

it("should show an error message if the user is already flagged", async () => {
vi.spyOn(command.module.dwcScheduledBans, "get").mockResolvedValue(entity);
const createSpy = vi.spyOn(interaction, "createMessage");

await command.execute(interaction, options);

expect(createSpy).toHaveBeenCalledOnce();
expect(createSpy).toHaveBeenCalledWith({
content: expect.stringContaining("That user is already flagged."),
flags: MessageFlags.Ephemeral
});
});

it("should show an error message if the user is trying to flag themselves", async () => {
const createSpy = vi.spyOn(interaction, "createMessage");
options.user = interaction.user;
Expand Down

0 comments on commit a4ad090

Please # to comment.