clean up button implementation some, start on custom act modal

This commit is contained in:
Ean Milligan (Bastion) 2023-02-02 02:26:28 -05:00
parent dad0b8ca76
commit d5f0b08e08
5 changed files with 84 additions and 28 deletions

5
src/buttons/_index.ts Normal file
View File

@ -0,0 +1,5 @@
import { Button } from '../types/commandTypes.ts';
import { createEventButton } from './event-creation/step1-gameSelection.ts';
import { createCustomEventButton } from './event-creation/step1a-openCustomModal.ts';
export const buttons: Array<Button> = [createEventButton, createCustomEventButton];

View File

@ -2,8 +2,9 @@ import { ActionRow, ApplicationCommandFlags, ApplicationCommandTypes, Bot, Butto
import { infoColor1, somethingWentWrong } from '../../commandUtils.ts'; import { infoColor1, somethingWentWrong } from '../../commandUtils.ts';
import { CommandDetails } from '../../types/commandTypes.ts'; import { CommandDetails } from '../../types/commandTypes.ts';
import { Activities } from './activities.ts'; import { Activities } from './activities.ts';
import { generateActionRow, generateMapId, getNestedActivity, pathIdxEnder, pathIdxSeparator } from './utils.ts'; import { deleteTokenEarly, generateActionRow, generateMapId, getNestedActivity, pathIdxEnder, pathIdxSeparator, tokenMap } from './utils.ts';
import utils from '../../utils.ts'; import utils from '../../utils.ts';
import { customId as createCustomActivityBtnId } from './step1a-openCustomModal.ts';
export const customId = 'gameSel'; export const customId = 'gameSel';
const slashCommandName = 'create-event'; const slashCommandName = 'create-event';
@ -16,17 +17,12 @@ const details: CommandDetails = {
type: ApplicationCommandTypes.ChatInput, type: ApplicationCommandTypes.ChatInput,
}; };
const tokenMap: Map<string, {
token: string;
timeoutId: number;
}> = new Map();
const customEventRow: ActionRow = { const customEventRow: ActionRow = {
type: MessageComponentTypes.ActionRow, type: MessageComponentTypes.ActionRow,
components: [{ components: [{
type: MessageComponentTypes.Button, type: MessageComponentTypes.Button,
label: 'Create Custom Event', label: 'Create Custom Event',
customId, customId: createCustomActivityBtnId,
style: ButtonStyles.Primary, style: ButtonStyles.Primary,
}], }],
}; };
@ -39,10 +35,7 @@ const execute = async (bot: Bot, interaction: Interaction) => {
if (interaction.data.values && interaction.data.values[0] && interaction.data.values[0].endsWith(pathIdxEnder)) { if (interaction.data.values && interaction.data.values[0] && interaction.data.values[0].endsWith(pathIdxEnder)) {
// User selected activity, give them the details modal and delete the selectMenus // User selected activity, give them the details modal and delete the selectMenus
bot.helpers.deleteOriginalInteractionResponse(tokenMap.get(generateMapId(interaction.guildId, interaction.channelId, interaction.member.id))?.token || '').catch((e: Error) => await deleteTokenEarly(bot, interaction, interaction.guildId, interaction.channelId, interaction.member.id);
utils.commonLoggers.interactionSendError('step1-gameSelection.ts:nextStep', interaction, e)
);
tokenMap.delete(generateMapId(interaction.guildId, interaction.channelId, interaction.member.id));
bot.helpers.sendInteractionResponse(interaction.id, interaction.token, { bot.helpers.sendInteractionResponse(interaction.id, interaction.token, {
type: InteractionResponseTypes.Modal, type: InteractionResponseTypes.Modal,
data: { data: {
@ -79,6 +72,7 @@ const execute = async (bot: Bot, interaction: Interaction) => {
customId: 'eventDescription', customId: 'eventDescription',
label: 'Description:', label: 'Description:',
style: TextStyles.Paragraph, style: TextStyles.Paragraph,
required: false,
}], }],
}], }],
}, },
@ -113,22 +107,14 @@ const execute = async (bot: Bot, interaction: Interaction) => {
}).catch((e: Error) => utils.commonLoggers.interactionSendError('step1-gameSelection.ts:edit', interaction, e)); }).catch((e: Error) => utils.commonLoggers.interactionSendError('step1-gameSelection.ts:edit', interaction, e));
} else { } else {
// Delete old token entry if it exists // Delete old token entry if it exists
if (tokenMap.has(generateMapId(interaction.guildId, interaction.channelId, interaction.member.id))) { await deleteTokenEarly(bot, interaction, interaction.guildId, interaction.channelId, interaction.member.id);
bot.helpers.deleteOriginalInteractionResponse(tokenMap.get(generateMapId(interaction.guildId, interaction.channelId, interaction.member.id))?.token || '').catch((e: Error) =>
utils.commonLoggers.interactionSendError('step1-gameSelection.ts:cleanup', interaction, e)
);
tokenMap.delete(generateMapId(interaction.guildId, interaction.channelId, interaction.member.id));
}
// Store token for later use // Store token for later use
tokenMap.set(generateMapId(interaction.guildId, interaction.channelId, interaction.member.id), { tokenMap.set(generateMapId(interaction.guildId, interaction.channelId, interaction.member.id), {
token: interaction.token, token: interaction.token,
timeoutId: setTimeout( timeoutId: setTimeout(
(guildId, channelId, memberId) => { (guildId, channelId, userId) => {
bot.helpers.deleteOriginalInteractionResponse(tokenMap.get(generateMapId(guildId, channelId, memberId))?.token || '').catch((e: Error) => deleteTokenEarly(bot, interaction, guildId, channelId, userId);
utils.commonLoggers.interactionSendError('step1-gameSelection.ts:delete', interaction, e)
);
tokenMap.delete(generateMapId(guildId, channelId, memberId));
}, },
tokenTimeoutMS, tokenTimeoutMS,
interaction.guildId, interaction.guildId,

View File

@ -0,0 +1,52 @@
import { Bot, Interaction, InteractionResponseTypes, MessageComponentTypes, TextStyles } from '../../../deps.ts';
import { deleteTokenEarly } from './utils.ts';
import utils from '../../utils.ts';
export const customId = 'createCustomActivity';
export const activityTitleId = 'activityTitle';
export const activitySubtitleId = 'activitySubtitle';
export const activityMaxPlayersId = 'activityMaxPlayers';
const execute = async (bot: Bot, interaction: Interaction) => {
if (interaction.member && interaction.guildId && interaction.channelId) {
await deleteTokenEarly(bot, interaction, interaction.guildId, interaction.channelId, interaction.member.id);
bot.helpers.sendInteractionResponse(interaction.id, interaction.token, {
type: InteractionResponseTypes.Modal,
data: {
title: 'Create Custom Activity',
customId: 'temp', //TODO: fix
components: [{
type: MessageComponentTypes.ActionRow,
components: [{
type: MessageComponentTypes.InputText,
customId: activityTitleId,
label: 'Activity Title:',
style: TextStyles.Short,
}],
}, {
type: MessageComponentTypes.ActionRow,
components: [{
type: MessageComponentTypes.InputText,
customId: activitySubtitleId,
label: 'Activity Subtitle:',
style: TextStyles.Short,
}],
}, {
type: MessageComponentTypes.ActionRow,
components: [{
type: MessageComponentTypes.InputText,
customId: activityMaxPlayersId,
label: 'Max Players:',
style: TextStyles.Short,
}],
}],
},
}).catch((e: Error) => utils.commonLoggers.interactionSendError('step1a-openCustomModal.ts:modal', interaction, e));
}
};
export const createCustomEventButton = {
customId,
execute,
};

View File

@ -1,9 +1,15 @@
import { Activity } from './activities.ts'; import { Activity } from './activities.ts';
import { ActionRow, MessageComponentTypes, SelectOption } from '../../../deps.ts'; import { ActionRow, Bot, Interaction, MessageComponentTypes, SelectOption } from '../../../deps.ts';
import utils from '../../utils.ts';
export const pathIdxSeparator = '|'; export const pathIdxSeparator = '|';
export const pathIdxEnder = '&'; export const pathIdxEnder = '&';
export const tokenMap: Map<string, {
token: string;
timeoutId: number;
}> = new Map();
export const getNestedActivity = (idxPath: Array<number>, activities: Array<Activity>): Array<Activity> => { export const getNestedActivity = (idxPath: Array<number>, activities: Array<Activity>): Array<Activity> => {
const nextIdx = idxPath[0]; const nextIdx = idxPath[0];
if (idxPath.length && activities[nextIdx] && activities[nextIdx].options) { if (idxPath.length && activities[nextIdx] && activities[nextIdx].options) {
@ -31,3 +37,14 @@ export const generateActionRow = (baseValue: string, activities: Array<Activity>
}); });
export const generateMapId = (guildId: bigint, channelId: bigint, userId: bigint) => `${guildId}-${channelId}-${userId}`; export const generateMapId = (guildId: bigint, channelId: bigint, userId: bigint) => `${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));
}
};

View File

@ -1,11 +1,7 @@
import { Bot, BotWithCache, Interaction } from '../../deps.ts'; import { Bot, BotWithCache, Interaction } from '../../deps.ts';
import { buttons } from '../buttons/_index.ts';
import { commands } from '../commands/_index.ts'; import { commands } from '../commands/_index.ts';
import { Button } from '../types/commandTypes.ts';
import { createEventButton } from '../buttons/event-creation/step1-gameSelection.ts';
const buttons: Array<Button> = [createEventButton];
const commandNames: Array<string> = commands.map((command) => command.details.name); const commandNames: Array<string> = commands.map((command) => command.details.name);
const buttonNames: Array<string> = buttons.map((button) => button.customId); const buttonNames: Array<string> = buttons.map((button) => button.customId);