Move tokenMap to own file since its used everywhere

tokenCleanup is now a separate module of code to make it better organized.
This commit is contained in:
Ean Milligan (Bastion) 2023-04-25 03:08:15 -04:00
parent 3475f84623
commit ce0a3cca1d
8 changed files with 51 additions and 55 deletions

View File

@ -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';

View File

@ -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';

View File

@ -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';

View File

@ -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';

View File

@ -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';

View File

@ -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 <t:${Math.floor((currentTime + tokenTimeoutMS) / 1000)}:R> due to limits imposed by the Discord API.`;
export const tokenMap: Map<string, {
token: string;
timeoutId: number;
}> = new Map();
export const getNestedActivity = (idxPath: Array<number>, activities: Array<Activity>): Array<Activity> => {
const nextIdx = idxPath[0];
@ -74,33 +53,6 @@ export const generateActionRow = (baseValue: string, activities: Array<Activity>
}],
});
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';

View File

@ -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

View File

@ -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 <t:${Math.floor((currentTime + tokenTimeoutMS) / 1000)}:R> due to limits imposed by the Discord API.`;
export const tokenMap: Map<string, {
token: string;
timeoutId: number;
}> = 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));
}
};