Add system to change event between whitelisted and public

This commit is contained in:
Ean Milligan (Bastion) 2023-04-26 07:23:38 -04:00
parent edeffc8669
commit 39f1bb68c8
4 changed files with 99 additions and 22 deletions

View File

@ -29,6 +29,8 @@ const actions = [
'btn-eeChangeAct', 'btn-eeChangeAct',
'btn-eeChangeTime', 'btn-eeChangeTime',
'btn-eeChangeDesc', 'btn-eeChangeDesc',
'btn-eeMakePublic',
'btn-eeMakeWL',
]; ];
for (const action of actions) { for (const action of actions) {
await dbClient.execute('INSERT INTO command_cnt(command) values(?)', [action]).catch((e) => { await dbClient.execute('INSERT INTO command_cnt(command) values(?)', [action]).catch((e) => {

View File

@ -17,6 +17,7 @@ import { editDateTimeButton } from './live-event/editDateTime.ts';
import { applyDescriptionButton } from './live-event/applyDescription.ts'; import { applyDescriptionButton } from './live-event/applyDescription.ts';
import { applyDateTimeButton } from './live-event/applyDateTime.ts'; import { applyDateTimeButton } from './live-event/applyDateTime.ts';
import { updateEventButton } from './live-event/updateEvent.ts'; import { updateEventButton } from './live-event/updateEvent.ts';
import { toggleWLStatusButton } from './live-event/toggleWLStatus.ts';
export const buttons: Array<Button> = [ export const buttons: Array<Button> = [
gameSelectionButton, gameSelectionButton,
@ -37,4 +38,5 @@ export const buttons: Array<Button> = [
applyDescriptionButton, applyDescriptionButton,
applyDateTimeButton, applyDateTimeButton,
updateEventButton, updateEventButton,
toggleWLStatusButton,
]; ];

View File

@ -1,4 +1,4 @@
import { ApplicationCommandFlags, Bot, ButtonStyles, Interaction, InteractionResponseTypes, MessageComponentTypes } from '../../../deps.ts'; import { ActionRow, ApplicationCommandFlags, Bot, ButtonStyles, Interaction, InteractionResponseTypes, MessageComponentTypes } from '../../../deps.ts';
import { dbClient, generateGuildSettingKey, lfgChannelSettings, queries } from '../../db.ts'; import { dbClient, generateGuildSettingKey, lfgChannelSettings, queries } from '../../db.ts';
import { infoColor1, somethingWentWrong, stopThat } from '../../commandUtils.ts'; import { infoColor1, somethingWentWrong, stopThat } from '../../commandUtils.ts';
import { idSeparator, pathIdxEnder, pathIdxSeparator } from '../eventUtils.ts'; import { idSeparator, pathIdxEnder, pathIdxSeparator } from '../eventUtils.ts';
@ -6,11 +6,15 @@ import { addTokenToMap, selfDestructMessage } from '../tokenCleanup.ts';
import utils from '../../utils.ts'; import utils from '../../utils.ts';
import { customId as editDescriptionCustomId } from './editDescription.ts'; import { customId as editDescriptionCustomId } from './editDescription.ts';
import { customId as editDateTimeCustomId } from './editDateTime.ts'; import { customId as editDateTimeCustomId } from './editDateTime.ts';
import { customId as toggleWLStatusCustomId } from './toggleWLStatus.ts';
export const customId = 'editEvent'; export const customId = 'editEvent';
const execute = async (bot: Bot, interaction: Interaction) => { const execute = async (bot: Bot, interaction: Interaction) => {
if (interaction.data?.customId && interaction.member && interaction.channelId && interaction.guildId && interaction.message) { if (
interaction.data?.customId && interaction.member && interaction.channelId && interaction.guildId && interaction.message && interaction.message.components &&
interaction.message.components[0].components
) {
// Light Telemetry // Light Telemetry
dbClient.execute(queries.callIncCnt('btn-editEvent')).catch((e) => utils.commonLoggers.dbError('editEvent.ts', 'call sproc INC_CNT on', e)); dbClient.execute(queries.callIncCnt('btn-editEvent')).catch((e) => utils.commonLoggers.dbError('editEvent.ts', 'call sproc INC_CNT on', e));
@ -24,11 +28,50 @@ const execute = async (bot: Bot, interaction: Interaction) => {
// Make sure this is being done by the owner or a Group Up Manager // Make sure this is being done by the owner or a Group Up Manager
if (interaction.member.id === ownerId || (lfgChannelSetting.managed && interaction.member.roles.includes(lfgChannelSetting.managerRoleId))) { if (interaction.member.id === ownerId || (lfgChannelSetting.managed && interaction.member.roles.includes(lfgChannelSetting.managerRoleId))) {
const actionByManager = interaction.member.id !== ownerId; const actionByManager = interaction.member.id !== ownerId;
const editIdxPath = `${idSeparator}${interaction.channelId}${pathIdxSeparator}${interaction.message.id}${actionByManager ? pathIdxEnder : ''}`; const baseEditIdxPath = `${idSeparator}${interaction.channelId}${pathIdxSeparator}${interaction.message.id}`;
const editIdxPath = `${baseEditIdxPath}${actionByManager ? pathIdxEnder : ''}`;
const whitelistedEvent = interaction.message.components[0].components.some((component) => component.customId?.includes(idSeparator));
// Store token for later use // Store token for later use
addTokenToMap(bot, interaction, interaction.guildId, interaction.channelId, interaction.member.id); addTokenToMap(bot, interaction, interaction.guildId, interaction.channelId, interaction.member.id);
const editButtons: ActionRow = {
type: MessageComponentTypes.ActionRow,
components: [{
type: MessageComponentTypes.Button,
label: 'Change Activity',
style: ButtonStyles.Primary,
customId: `a${editIdxPath}`, // TODO: add customId
}, {
type: MessageComponentTypes.Button,
label: 'Change Date/Time',
style: ButtonStyles.Primary,
customId: `${editDateTimeCustomId}${editIdxPath}`,
}, {
type: MessageComponentTypes.Button,
label: 'Edit Description',
style: ButtonStyles.Primary,
customId: `${editDescriptionCustomId}${editIdxPath}`,
}],
};
if (!actionByManager) {
editButtons.components.push(
whitelistedEvent
? {
type: MessageComponentTypes.Button,
label: 'Make Event Public',
style: ButtonStyles.Primary,
customId: `${toggleWLStatusCustomId}${baseEditIdxPath}`, // TODO: add customId
}
: {
type: MessageComponentTypes.Button,
label: 'Make Event Whitelisted',
style: ButtonStyles.Primary,
customId: `${toggleWLStatusCustomId}${baseEditIdxPath}${pathIdxEnder}`, // TODO: add customId
},
);
}
// Open Edit Options // Open Edit Options
bot.helpers.sendInteractionResponse( bot.helpers.sendInteractionResponse(
interaction.id, interaction.id,
@ -50,25 +93,7 @@ const execute = async (bot: Bot, interaction: Interaction) => {
${selfDestructMessage(new Date().getTime())}`, ${selfDestructMessage(new Date().getTime())}`,
}], }],
components: [{ components: [editButtons],
type: MessageComponentTypes.ActionRow,
components: [{
type: MessageComponentTypes.Button,
label: 'Change Activity',
style: ButtonStyles.Primary,
customId: `a${editIdxPath}`, // TODO: add customId
}, {
type: MessageComponentTypes.Button,
label: 'Change Date/Time',
style: ButtonStyles.Primary,
customId: `${editDateTimeCustomId}${editIdxPath}`,
}, {
type: MessageComponentTypes.Button,
label: 'Edit Description',
style: ButtonStyles.Primary,
customId: `${editDescriptionCustomId}${editIdxPath}`,
}],
}],
}, },
}, },
).catch((e: Error) => utils.commonLoggers.interactionSendError('editEvent.ts', interaction, e)); ).catch((e: Error) => utils.commonLoggers.interactionSendError('editEvent.ts', interaction, e));

View File

@ -0,0 +1,48 @@
import { ApplicationCommandFlags, Bot, Interaction, InteractionResponseTypes, MessageComponentTypes } from '../../../deps.ts';
import { safelyDismissMsg, somethingWentWrong, successColor } from '../../commandUtils.ts';
import { idSeparator, pathIdxEnder, pathIdxSeparator } from '../eventUtils.ts';
import { deleteTokenEarly } from '../tokenCleanup.ts';
import utils from '../../utils.ts';
import { dbClient, queries } from '../../db.ts';
import { generateLFGButtons } from '../event-creation/utils.ts';
export const customId = 'toggleWLStatus';
const execute = async (bot: Bot, interaction: Interaction) => {
if (interaction.data?.customId && interaction.member && interaction.channelId && interaction.guildId) {
deleteTokenEarly(bot, interaction, interaction.guildId, interaction.channelId, interaction.member.id);
const makeWhitelisted = interaction.data.customId.endsWith(pathIdxEnder);
dbClient.execute(queries.callIncCnt(makeWhitelisted ? 'btn-eeMakeWL' : 'btn-eeMakePublic')).catch((e) => utils.commonLoggers.dbError('toggleWLStatus.ts', 'call sproc INC_CNT on', e));
const [evtChannelId, evtMessageId] = (interaction.data.customId.replaceAll(pathIdxEnder, '').split(idSeparator)[1] || '').split(pathIdxSeparator).map((id) => BigInt(id || '0'));
// TODO: verify we can DM owner before applying update
bot.helpers.editMessage(evtChannelId, evtMessageId, {
components: [{
type: MessageComponentTypes.ActionRow,
components: generateLFGButtons(makeWhitelisted),
}],
}).then(() =>
bot.helpers.sendInteractionResponse(interaction.id, interaction.token, {
type: InteractionResponseTypes.ChannelMessageWithSource,
data: {
flags: ApplicationCommandFlags.Ephemeral,
embeds: [{
color: successColor,
title: 'Update successfully applied.',
description: `This event is now ${makeWhitelisted ? 'whitelisted, meaning you will have to approve join requests from all future members' : 'public'}.\n\n${safelyDismissMsg}`,
}],
},
}).catch((e: Error) => utils.commonLoggers.interactionSendError('toggleWLStatus.ts', interaction, e))
).catch((e) => {
utils.commonLoggers.messageEditError('toggleWLStatus.ts', 'toggleWLStatusFailed', e);
somethingWentWrong(bot, interaction, 'editFailedInToggleWLStatusButton');
});
} else {
somethingWentWrong(bot, interaction, 'noDataFromToggleWLStatusButton');
}
};
export const toggleWLStatusButton = {
customId,
execute,
};