This commit is contained in:
Ean Milligan (Bastion) 2023-01-31 03:34:53 -05:00
parent d712b116b6
commit dad0b8ca76
3 changed files with 42 additions and 29 deletions

View File

@ -1,8 +1,8 @@
import { ApplicationCommandFlags, ApplicationCommandTypes, Bot, Interaction, InteractionResponseTypes, MessageComponentTypes, ActionRow, ButtonStyles, TextStyles } from '../../../deps.ts'; import { ActionRow, ApplicationCommandFlags, ApplicationCommandTypes, Bot, ButtonStyles, Interaction, InteractionResponseTypes, MessageComponentTypes, TextStyles } from '../../../deps.ts';
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, getNestedActivity, generateMapId, pathIdxSeparator, pathIdxEnder } from './utils.ts'; import { generateActionRow, generateMapId, getNestedActivity, pathIdxEnder, pathIdxSeparator } from './utils.ts';
import utils from '../../utils.ts'; import utils from '../../utils.ts';
export const customId = 'gameSel'; export const customId = 'gameSel';
@ -17,8 +17,8 @@ const details: CommandDetails = {
}; };
const tokenMap: Map<string, { const tokenMap: Map<string, {
token: string, token: string;
timeoutId: number, timeoutId: number;
}> = new Map(); }> = new Map();
const customEventRow: ActionRow = { const customEventRow: ActionRow = {
@ -29,17 +29,19 @@ const customEventRow: ActionRow = {
customId, customId,
style: ButtonStyles.Primary, style: ButtonStyles.Primary,
}], }],
} };
const execute = async (bot: Bot, interaction: Interaction) => { const execute = async (bot: Bot, interaction: Interaction) => {
if (interaction.data && (interaction.data.name === slashCommandName || interaction.data.customId) && interaction.member && interaction.guildId && interaction.channelId) { if (interaction.data && (interaction.data.name === slashCommandName || interaction.data.customId) && interaction.member && interaction.guildId && interaction.channelId) {
// Parse indexPath from the select value // Parse indexPath from the select value
const rawIdxPath: Array<string> = interaction.data.values ? interaction.data.values[0].split(pathIdxSeparator) : ['']; const rawIdxPath: Array<string> = interaction.data.values ? interaction.data.values[0].split(pathIdxSeparator) : [''];
const idxPath: Array<number> = rawIdxPath.map(rawIdx => rawIdx ? parseInt(rawIdx) : -1); const idxPath: Array<number> = rawIdxPath.map((rawIdx) => rawIdx ? parseInt(rawIdx) : -1);
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) => utils.commonLoggers.interactionSendError('step1-gameSelection.ts:cleanup', interaction, e)); bot.helpers.deleteOriginalInteractionResponse(tokenMap.get(generateMapId(interaction.guildId, interaction.channelId, interaction.member.id))?.token || '').catch((e: Error) =>
utils.commonLoggers.interactionSendError('step1-gameSelection.ts:nextStep', interaction, e)
);
tokenMap.delete(generateMapId(interaction.guildId, interaction.channelId, interaction.member.id)); 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,
@ -53,34 +55,34 @@ const execute = async (bot: Bot, interaction: Interaction) => {
customId: 'eventTime', customId: 'eventTime',
label: 'Start Time:', label: 'Start Time:',
style: TextStyles.Short, style: TextStyles.Short,
}] }],
},{ }, {
type: MessageComponentTypes.ActionRow, type: MessageComponentTypes.ActionRow,
components: [{ components: [{
type: MessageComponentTypes.InputText, type: MessageComponentTypes.InputText,
customId: 'eventTimeZone', customId: 'eventTimeZone',
label: 'Time Zone:', label: 'Time Zone:',
style: TextStyles.Short, style: TextStyles.Short,
}] }],
},{ }, {
type: MessageComponentTypes.ActionRow, type: MessageComponentTypes.ActionRow,
components: [{ components: [{
type: MessageComponentTypes.InputText, type: MessageComponentTypes.InputText,
customId: 'eventDate', customId: 'eventDate',
label: 'Start Date:', label: 'Start Date:',
style: TextStyles.Short, style: TextStyles.Short,
}] }],
},{ }, {
type: MessageComponentTypes.ActionRow, type: MessageComponentTypes.ActionRow,
components: [{ components: [{
type: MessageComponentTypes.InputText, type: MessageComponentTypes.InputText,
customId: 'eventDescription', customId: 'eventDescription',
label: 'Description:', label: 'Description:',
style: TextStyles.Paragraph, style: TextStyles.Paragraph,
}] }],
}] }],
}, },
}) }).catch((e: Error) => utils.commonLoggers.interactionSendError('step1-gameSelection.ts:modal', interaction, e));
return; return;
} }
@ -112,17 +114,27 @@ const execute = async (bot: Bot, interaction: Interaction) => {
} 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))) { if (tokenMap.has(generateMapId(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)); 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)); 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((guildId, channelId, memberId) => { timeoutId: setTimeout(
bot.helpers.deleteOriginalInteractionResponse(tokenMap.get(generateMapId(guildId, channelId, memberId))?.token || '').catch((e: Error) => utils.commonLoggers.interactionSendError('step1-gameSelection.ts:delete', interaction, e)); (guildId, channelId, memberId) => {
bot.helpers.deleteOriginalInteractionResponse(tokenMap.get(generateMapId(guildId, channelId, memberId))?.token || '').catch((e: Error) =>
utils.commonLoggers.interactionSendError('step1-gameSelection.ts:delete', interaction, e)
);
tokenMap.delete(generateMapId(guildId, channelId, memberId)); tokenMap.delete(generateMapId(guildId, channelId, memberId));
}, tokenTimeoutMS, interaction.guildId, interaction.channelId, interaction.member.id), },
tokenTimeoutMS,
interaction.guildId,
interaction.channelId,
interaction.member.id,
),
}); });
// Calculate destruction time // Calculate destruction time

View File

@ -1,5 +1,5 @@
import { Activity } from './activities.ts'; import { Activity } from './activities.ts';
import { SelectOption, ActionRow, MessageComponentTypes } from '../../../deps.ts'; import { ActionRow, MessageComponentTypes, SelectOption } from '../../../deps.ts';
export const pathIdxSeparator = '|'; export const pathIdxSeparator = '|';
export const pathIdxEnder = '&'; export const pathIdxEnder = '&';
@ -14,11 +14,12 @@ export const getNestedActivity = (idxPath: Array<number>, activities: Array<Acti
} }
}; };
const getSelectOptions = (baseValue: string, activities: Array<Activity>, defaultIdx?: number): Array<SelectOption> => activities.map((act, idx) => ({ const getSelectOptions = (baseValue: string, activities: Array<Activity>, defaultIdx?: number): Array<SelectOption> =>
activities.map((act, idx) => ({
label: act.name, label: act.name,
value: `${baseValue}${idx}${act.maxMembers ? pathIdxEnder : pathIdxSeparator}`, value: `${baseValue}${idx}${act.maxMembers ? pathIdxEnder : pathIdxSeparator}`,
default: idx === defaultIdx, default: idx === defaultIdx,
})); }));
export const generateActionRow = (baseValue: string, activities: Array<Activity>, customId: string, defaultIdx?: number): ActionRow => ({ export const generateActionRow = (baseValue: string, activities: Array<Activity>, customId: string, defaultIdx?: number): ActionRow => ({
type: MessageComponentTypes.ActionRow, type: MessageComponentTypes.ActionRow,

View File

@ -18,13 +18,13 @@ export const interactionCreate = (rawBot: Bot, interaction: Interaction) => {
return; return;
} }
const customId = interaction.data.customId ? interaction.data.customId.replace(/\$/g, '') : '' const customId = interaction.data.customId ? interaction.data.customId.replace(/\$/g, '') : '';
if (customId && buttonNames.includes(customId)) { if (customId && buttonNames.includes(customId)) {
const btnIdx = buttonNames.indexOf(customId); const btnIdx = buttonNames.indexOf(customId);
buttons[btnIdx].execute(bot, interaction); buttons[btnIdx].execute(bot, interaction);
return; return;
} }
console.log('interaction NOT HANDLED') console.log('interaction NOT HANDLED');
} }
}; };