Skip to content
Merged
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
8 changes: 7 additions & 1 deletion src/discord/events/interaction/commands/submit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApplicationCommandOptionTypes } from 'discord.js/typings/enums';
import { ChatInputCommandDefinition, PopulatedCommandInteraction } from '../interaction';
import { getCtfByGuildContext } from '../../../util/ResourceManager';
import { debounceChallengeChannelUpdates } from '../../../util/UpdateDebouncer';

export default {
name: 'submit',
Expand All @@ -18,7 +19,12 @@ export default {
const ctf = await getCtfByGuildContext(interaction.guild);
if (!ctf) throw new Error('this guild does not belong to a ctf');

const flag = ctf.getFlag(interaction.options.getString('flag', true));
const flag = await ctf.getFlag(interaction.options.getString('flag', true));
if (!flag) throw new Error('Invalid flag. Checking for trailing spaces and typos.');
const challenge = await flag.getChallenge();

// update the challenge channels, but in a debounced fashion
debounceChallengeChannelUpdates(challenge, interaction.client);

return 'to be implemented';
},
Expand Down
1 change: 0 additions & 1 deletion src/discord/events/interaction/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Client,
CommandInteraction,
Interaction,
UserContextMenuInteraction,
} from 'discord.js';
import { category, challenge, ctf } from './commands';
import { embedify, logger } from '../../../log';
Expand Down
24 changes: 24 additions & 0 deletions src/discord/util/UpdateDebouncer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Challenge } from '../../database/models/Challenge';
import { refreshChallenge } from "../hooks/ChallengeHooks";
import { Client } from "discord.js";

// a hashmap from challenge ids to their timeout ID
const mapping = new Map<number, NodeJS.Timeout>();

// this function will trigger an update to challenge channels... but no more than
// once every two minutes
export function debounceChallengeChannelUpdates(challenge: Challenge, client: Client<true>) {
const timeout = mapping.get(challenge.id);

// if there's currently a timeout for this challenge, just ignore for now
if (timeout) return;

// otherwise, kick off the update to happen in two minutes
mapping.set(
challenge.id,
setTimeout(() => {
mapping.delete(challenge.id);
void refreshChallenge(challenge, client);
}, 1000 * 120),
);
}