diff --git a/src/buttons/event-creation/step1-gameSelection.ts b/src/buttons/event-creation/step1-gameSelection.ts index 70dd039..171f292 100644 --- a/src/buttons/event-creation/step1-gameSelection.ts +++ b/src/buttons/event-creation/step1-gameSelection.ts @@ -2,8 +2,9 @@ import { ActionRow, ApplicationCommandFlags, ApplicationCommandTypes, Bot, Butto import { infoColor1, somethingWentWrong } from '../../commandUtils.ts'; import { CommandDetails } from '../../types/commandTypes.ts'; import { Activities } from './activities.ts'; -import { addTokenToMap, deleteTokenEarly, generateActionRow, generateMapId, getNestedActivity, selfDestructMessage, tokenMap } from './utils.ts'; +import { generateActionRow, getNestedActivity } from './utils.ts'; import { idSeparator, LfgEmbedIndexes, lfgStartTimeName, pathIdxEnder, pathIdxSeparator } from '../eventUtils.ts'; +import { addTokenToMap, deleteTokenEarly, generateMapId, selfDestructMessage, tokenMap } from '../tokenCleanup.ts'; import utils from '../../utils.ts'; import { customId as createCustomActivityBtnId } from './step1a-openCustomModal.ts'; import { customId as finalizeEventBtnId } from './step2-finalize.ts'; diff --git a/src/buttons/event-creation/step1a-openCustomModal.ts b/src/buttons/event-creation/step1a-openCustomModal.ts index 6413122..b04eb75 100644 --- a/src/buttons/event-creation/step1a-openCustomModal.ts +++ b/src/buttons/event-creation/step1a-openCustomModal.ts @@ -1,5 +1,5 @@ import { Bot, Interaction, InteractionResponseTypes, MessageComponentTypes, TextStyles } from '../../../deps.ts'; -import { deleteTokenEarly } from './utils.ts'; +import { deleteTokenEarly } from '../tokenCleanup.ts'; import { idSeparator, pathIdxSeparator } from '../eventUtils.ts'; import { customId as verifyCustomActivityId } from './step1b-verifyCustomActivity.ts'; import utils from '../../utils.ts'; diff --git a/src/buttons/event-creation/step1b-verifyCustomActivity.ts b/src/buttons/event-creation/step1b-verifyCustomActivity.ts index 9b55775..08adc59 100644 --- a/src/buttons/event-creation/step1b-verifyCustomActivity.ts +++ b/src/buttons/event-creation/step1b-verifyCustomActivity.ts @@ -1,8 +1,8 @@ import config from '../../../config.ts'; import { ApplicationCommandFlags, Bot, ButtonStyles, Interaction, InteractionResponseTypes, MessageComponentTypes } from '../../../deps.ts'; import { failColor, infoColor1, safelyDismissMsg, somethingWentWrong } from '../../commandUtils.ts'; -import { addTokenToMap, selfDestructMessage } from './utils.ts'; import { idSeparator, pathIdxEnder, pathIdxSeparator } from '../eventUtils.ts'; +import { addTokenToMap, selfDestructMessage } from '../tokenCleanup.ts'; import { activityMaxPlayersId, activitySubtitleId, activityTitleId } from './step1a-openCustomModal.ts'; import { customId as gameSelectionId } from './step1-gameSelection.ts'; import { customId as openCustomModalId } from './step1a-openCustomModal.ts'; diff --git a/src/buttons/event-creation/step2-finalize.ts b/src/buttons/event-creation/step2-finalize.ts index d1eadb3..dcadef5 100644 --- a/src/buttons/event-creation/step2-finalize.ts +++ b/src/buttons/event-creation/step2-finalize.ts @@ -1,8 +1,9 @@ import { Bot, Interaction } from '../../../deps.ts'; import { somethingWentWrong } from '../../commandUtils.ts'; import { eventDateId, eventDescriptionId, eventTimeId, eventTimeZoneId } from './step1-gameSelection.ts'; -import { addTokenToMap, createLFGPost, getFinalActivity } from './utils.ts'; +import { createLFGPost, getFinalActivity } from './utils.ts'; import { idSeparator, pathIdxSeparator } from '../eventUtils.ts'; +import { addTokenToMap } from '../tokenCleanup.ts'; import { Activities, Activity } from './activities.ts'; import { getDateFromRawInput } from './dateTimeUtils.ts'; import utils from '../../utils.ts'; diff --git a/src/buttons/event-creation/step3-createEvent.ts b/src/buttons/event-creation/step3-createEvent.ts index 8e8c73a..1bfe610 100644 --- a/src/buttons/event-creation/step3-createEvent.ts +++ b/src/buttons/event-creation/step3-createEvent.ts @@ -1,6 +1,7 @@ import { Bot, Interaction, InteractionResponseTypes, MessageComponentTypes } from '../../../deps.ts'; -import { deleteTokenEarly, generateLFGButtons } from './utils.ts'; +import { generateLFGButtons } from './utils.ts'; import { idSeparator, LfgEmbedIndexes } from '../eventUtils.ts'; +import { deleteTokenEarly } from '../tokenCleanup.ts'; import { somethingWentWrong } from '../../commandUtils.ts'; import { dbClient, queries } from '../../db.ts'; import utils from '../../utils.ts'; diff --git a/src/buttons/event-creation/utils.ts b/src/buttons/event-creation/utils.ts index 23b3661..f6fada7 100644 --- a/src/buttons/event-creation/utils.ts +++ b/src/buttons/event-creation/utils.ts @@ -1,17 +1,5 @@ -import { - ActionRow, - ApplicationCommandFlags, - Bot, - ButtonComponent, - ButtonStyles, - Interaction, - InteractionResponse, - InteractionResponseTypes, - MessageComponentTypes, - SelectOption, -} from '../../../deps.ts'; +import { ActionRow, ApplicationCommandFlags, ButtonComponent, ButtonStyles, InteractionResponse, InteractionResponseTypes, MessageComponentTypes, SelectOption } from '../../../deps.ts'; import config from '../../../config.ts'; -import utils from '../../utils.ts'; import { Activity } from './activities.ts'; import { alternateEventBtnStr, @@ -26,6 +14,7 @@ import { pathIdxSeparator, requestToJoinEventBtnStr, } from '../eventUtils.ts'; +import { selfDestructMessage } from '../tokenCleanup.ts'; import { successColor } from '../../commandUtils.ts'; import { LFGMember } from '../../types/commandTypes.ts'; import { customId as gameSelCustomId } from './step1-gameSelection.ts'; @@ -35,16 +24,6 @@ import { customId as leaveEventCustomId } from '../live-event/leaveEvent.ts'; import { customId as alternateEventCustomId } from '../live-event/alternateEvent.ts'; import { customId as deleteEventCustomId } from '../live-event/deleteEvent.ts'; -// Discord Interaction Tokens last 15 minutes, we will self kill after 14.5 minutes -const tokenTimeoutS = (15 * 60) - 30; -export const tokenTimeoutMS = tokenTimeoutS * 1000; -export const selfDestructMessage = (currentTime: number) => - `**Please note:** This message will self destruct due to limits imposed by the Discord API.`; - -export const tokenMap: Map = new Map(); export const getNestedActivity = (idxPath: Array, activities: Array): Array => { const nextIdx = idxPath[0]; @@ -74,33 +53,6 @@ export const generateActionRow = (baseValue: string, activities: Array }], }); -export const generateMapId = (guildId: bigint, channelId: bigint, userId: bigint) => `${guildId}-${channelId}-${userId}`; - -export const addTokenToMap = (bot: Bot, interaction: Interaction, guildId: bigint, channelId: bigint, userId: bigint) => - tokenMap.set(generateMapId(guildId, channelId, userId), { - token: interaction.token, - timeoutId: setTimeout( - (guildId, channelId, userId) => { - deleteTokenEarly(bot, interaction, guildId, channelId, userId); - }, - tokenTimeoutMS, - guildId, - channelId, - userId, - ), - }); - -export const deleteTokenEarly = async (bot: Bot, interaction: Interaction, guildId: bigint, channelId: bigint, userId: bigint) => { - const tokenMapEntry = tokenMap.get(generateMapId(guildId, channelId, userId)); - if (tokenMapEntry && tokenMapEntry.token) { - await bot.helpers.deleteOriginalInteractionResponse(tokenMap.get(generateMapId(guildId, channelId, userId))?.token || '').catch((e: Error) => - utils.commonLoggers.interactionSendError('utils.ts:deleteTokenEarly', interaction, e) - ); - clearTimeout(tokenMapEntry.timeoutId); - tokenMap.delete(generateMapId(guildId, channelId, userId)); - } -}; - const createEventBtnName = 'Create Event'; const createWhitelistedBtnName = 'Create Whitelisted Event'; const editEventDetailsBtnName = 'Edit Event Details'; diff --git a/src/buttons/live-event/deleteEvent.ts b/src/buttons/live-event/deleteEvent.ts index 488bafb..2b3dcc1 100644 --- a/src/buttons/live-event/deleteEvent.ts +++ b/src/buttons/live-event/deleteEvent.ts @@ -19,6 +19,7 @@ const execute = async (bot: Bot, interaction: Interaction) => { logChannelId: 0n, }; + // Make sure this is being done by the owner or a Group Up Manager if (interaction.member.user.id === ownerId || (lfgChannelSetting.managed && interaction.member.roles.includes(lfgChannelSetting.managerRoleId))) { const actionByManager = interaction.member.user.id !== ownerId; // Open Delete Confirmation diff --git a/src/buttons/tokenCleanup.ts b/src/buttons/tokenCleanup.ts new file mode 100644 index 0000000..a1395e0 --- /dev/null +++ b/src/buttons/tokenCleanup.ts @@ -0,0 +1,40 @@ +import { Bot, Interaction } from '../../deps.ts'; +import utils from '../utils.ts'; + +// Discord Interaction Tokens last 15 minutes, we will self kill after 14.5 minutes +const tokenTimeoutS = (15 * 60) - 30; +const tokenTimeoutMS = tokenTimeoutS * 1000; +export const selfDestructMessage = (currentTime: number) => + `**Please note:** This message will self destruct due to limits imposed by the Discord API.`; + +export const tokenMap: Map = new Map(); + +export const generateMapId = (guildId: bigint, channelId: bigint, userId: bigint) => `${guildId}-${channelId}-${userId}`; + +export const addTokenToMap = (bot: Bot, interaction: Interaction, guildId: bigint, channelId: bigint, userId: bigint) => + tokenMap.set(generateMapId(guildId, channelId, userId), { + token: interaction.token, + timeoutId: setTimeout( + (guildId, channelId, userId) => { + deleteTokenEarly(bot, interaction, guildId, channelId, userId); + }, + tokenTimeoutMS, + guildId, + channelId, + userId, + ), + }); + +export const deleteTokenEarly = async (bot: Bot, interaction: Interaction, guildId: bigint, channelId: bigint, userId: bigint) => { + const tokenMapEntry = tokenMap.get(generateMapId(guildId, channelId, userId)); + if (tokenMapEntry && tokenMapEntry.token) { + await bot.helpers.deleteOriginalInteractionResponse(tokenMap.get(generateMapId(guildId, channelId, userId))?.token || '').catch((e: Error) => + utils.commonLoggers.interactionSendError('tokenCleanup.ts:deleteTokenEarly', interaction, e) + ); + clearTimeout(tokenMapEntry.timeoutId); + tokenMap.delete(generateMapId(guildId, channelId, userId)); + } +};