Verify bot can dm users when they try to whitelist an event + deno fmt

This commit is contained in:
Ean Milligan (Bastion) 2023-04-29 04:29:15 -04:00
parent ed18f556b1
commit afeb6beda0
5 changed files with 64 additions and 15 deletions

View File

@ -47,7 +47,7 @@ const execute = async (bot: Bot, interaction: Interaction) => {
// Log custom event to see if we should add it as a preset // Log custom event to see if we should add it as a preset
if (customAct) { if (customAct) {
dbClient.execute(queries.insertCustomActivity, [category, activity.name, activity.maxMembers]).catch((e) => utils.commonLoggers.dbError('step2-finalize.ts@custom', 'insert into', e)) dbClient.execute(queries.insertCustomActivity, [category, activity.name, activity.maxMembers]).catch((e) => utils.commonLoggers.dbError('step2-finalize.ts@custom', 'insert into', e));
} }
const rawEventTime = tempDataMap.get(eventTimeId) || ''; const rawEventTime = tempDataMap.get(eventTimeId) || '';

View File

@ -1,8 +1,8 @@
import { Bot, Interaction, InteractionResponseTypes, MessageComponentTypes } from '../../../deps.ts'; import { ApplicationCommandFlags, Bot, Interaction, InteractionResponseTypes, MessageComponentTypes } from '../../../deps.ts';
import { generateLFGButtons } from './utils.ts'; import { generateLFGButtons } from './utils.ts';
import { idSeparator, LfgEmbedIndexes } from '../eventUtils.ts'; import { idSeparator, LfgEmbedIndexes } from '../eventUtils.ts';
import { deleteTokenEarly } from '../tokenCleanup.ts'; import { deleteTokenEarly } from '../tokenCleanup.ts';
import { somethingWentWrong } from '../../commandUtils.ts'; import { dmTestMessage, safelyDismissMsg, sendDirectMessage, somethingWentWrong, warnColor } from '../../commandUtils.ts';
import { dbClient, queries } from '../../db.ts'; import { dbClient, queries } from '../../db.ts';
import utils from '../../utils.ts'; import utils from '../../utils.ts';
@ -23,6 +23,25 @@ const execute = async (bot: Bot, interaction: Interaction) => {
const ownerId: bigint = BigInt(interaction.message.embeds[0].footer?.iconUrl?.split('#')[1] || '0'); const ownerId: bigint = BigInt(interaction.message.embeds[0].footer?.iconUrl?.split('#')[1] || '0');
const eventTime: Date = new Date(parseInt(interaction.message.embeds[0].fields[LfgEmbedIndexes.ICSLink].value.split('?t=')[1].split('&n=')[0] || '0')); const eventTime: Date = new Date(parseInt(interaction.message.embeds[0].fields[LfgEmbedIndexes.ICSLink].value.split('?t=')[1].split('&n=')[0] || '0'));
// Check if we need to ensure DMs are open
if (interaction.data.customId.includes(idSeparator)) {
const dmSuccess = Boolean(await sendDirectMessage(bot, interaction.member.id, dmTestMessage).catch((e: Error) => utils.commonLoggers.messageSendError('toggleWLStatus.ts', 'send DM fail', e)));
if (!dmSuccess) {
bot.helpers.sendInteractionResponse(interaction.id, interaction.token, {
type: InteractionResponseTypes.ChannelMessageWithSource,
data: {
flags: ApplicationCommandFlags.Ephemeral,
embeds: [{
color: warnColor,
title: 'Event not created.',
description: `In order to create a whitelisted event, your DMs must be open to receive Join Requests. Please open your DMs and try again.\n\n${safelyDismissMsg}`,
}],
},
}).catch((e: Error) => utils.commonLoggers.interactionSendError('toggleWLStatus.ts@dmFail', interaction, e));
return;
}
}
// Send Event Message // Send Event Message
const eventMessage = await bot.helpers.sendMessage(interaction.channelId, { const eventMessage = await bot.helpers.sendMessage(interaction.channelId, {
embeds: [interaction.message.embeds[0]], embeds: [interaction.message.embeds[0]],

View File

@ -76,8 +76,9 @@ const execute = async (bot: Bot, interaction: Interaction) => {
embeds: [{ embeds: [{
color: failColor, color: failColor,
title: 'Invalid Max Member count!', title: 'Invalid Max Member count!',
description: `${config.name} parsed the max members as \`${isNaN(activityMaxPlayers) ? 'Not a Number' : activityMaxPlayers description: `${config.name} parsed the max members as \`${
}\`, which is outside of the allowed range. Please re-edit this activity, but make sure the maximum player count is between 1 and 99.\n\n${safelyDismissMsg}`, isNaN(activityMaxPlayers) ? 'Not a Number' : activityMaxPlayers
}\`, which is outside of the allowed range. Please re-edit this activity, but make sure the maximum player count is between 1 and 99.\n\n${safelyDismissMsg}`,
}], }],
}, },
}).catch((e: Error) => utils.commonLoggers.interactionSendError('editActivity.ts@invalidPlayer', interaction, e)); }).catch((e: Error) => utils.commonLoggers.interactionSendError('editActivity.ts@invalidPlayer', interaction, e));
@ -93,7 +94,9 @@ const execute = async (bot: Bot, interaction: Interaction) => {
selectedActivity.maxMembers = activityMaxPlayers; selectedActivity.maxMembers = activityMaxPlayers;
// Log custom event to see if we should add it as a preset // Log custom event to see if we should add it as a preset
dbClient.execute(queries.insertCustomActivity, [selectedCategory, selectedActivity.name, selectedActivity.maxMembers]).catch((e) => utils.commonLoggers.dbError('editActivity.ts@custom', 'insert into', e)) dbClient.execute(queries.insertCustomActivity, [selectedCategory, selectedActivity.name, selectedActivity.maxMembers]).catch((e) =>
utils.commonLoggers.dbError('editActivity.ts@custom', 'insert into', e)
);
} else { } else {
const rawIdxPath: Array<string> = finalizedIdxPath.split(pathIdxSeparator); const rawIdxPath: Array<string> = finalizedIdxPath.split(pathIdxSeparator);
const idxPath: Array<number> = rawIdxPath.map((rawIdx) => rawIdx ? parseInt(rawIdx) : -1); const idxPath: Array<number> = rawIdxPath.map((rawIdx) => rawIdx ? parseInt(rawIdx) : -1);
@ -195,12 +198,13 @@ const execute = async (bot: Bot, interaction: Interaction) => {
data: { data: {
embeds: [{ embeds: [{
title: 'Please select a Game and Activity, or create a Custom Event.', title: 'Please select a Game and Activity, or create a Custom Event.',
description: `Changing activity for [this event](${utils.idsToMessageUrl({ description: `Changing activity for [this event](${
guildId: interaction.guildId, utils.idsToMessageUrl({
channelId: evtChannelId, guildId: interaction.guildId,
messageId: evtMessageId, channelId: evtChannelId,
}) messageId: evtMessageId,
}).\n\n${selfDestructMessage(new Date().getTime())}`, })
}).\n\n${selfDestructMessage(new Date().getTime())}`,
color: infoColor1, color: infoColor1,
}], }],
flags: ApplicationCommandFlags.Ephemeral, flags: ApplicationCommandFlags.Ephemeral,

View File

@ -1,5 +1,5 @@
import { ApplicationCommandFlags, Bot, Interaction, InteractionResponseTypes, MessageComponentTypes } from '../../../deps.ts'; import { ApplicationCommandFlags, Bot, Interaction, InteractionResponseTypes, MessageComponentTypes } from '../../../deps.ts';
import { safelyDismissMsg, somethingWentWrong, successColor } from '../../commandUtils.ts'; import { dmTestMessage, safelyDismissMsg, sendDirectMessage, somethingWentWrong, successColor, warnColor } from '../../commandUtils.ts';
import { idSeparator, pathIdxEnder, pathIdxSeparator } from '../eventUtils.ts'; import { idSeparator, pathIdxEnder, pathIdxSeparator } from '../eventUtils.ts';
import { deleteTokenEarly } from '../tokenCleanup.ts'; import { deleteTokenEarly } from '../tokenCleanup.ts';
import utils from '../../utils.ts'; import utils from '../../utils.ts';
@ -15,7 +15,25 @@ const execute = async (bot: Bot, interaction: Interaction) => {
dbClient.execute(queries.callIncCnt(makeWhitelisted ? 'btn-eeMakeWL' : 'btn-eeMakePublic')).catch((e) => utils.commonLoggers.dbError('toggleWLStatus.ts', 'call sproc INC_CNT on', e)); 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')); 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 // Check if we need to ensure DMs are open
if (makeWhitelisted) {
const dmSuccess = Boolean(await sendDirectMessage(bot, interaction.member.id, dmTestMessage).catch((e: Error) => utils.commonLoggers.messageSendError('toggleWLStatus.ts', 'send DM fail', e)));
if (!dmSuccess) {
bot.helpers.sendInteractionResponse(interaction.id, interaction.token, {
type: InteractionResponseTypes.ChannelMessageWithSource,
data: {
flags: ApplicationCommandFlags.Ephemeral,
embeds: [{
color: warnColor,
title: 'Event not modified.',
description: `In order to turn the whitelist on, your DMs must be open to receive Join Requests. Please open your DMs and try again.\n\n${safelyDismissMsg}`,
}],
},
}).catch((e: Error) => utils.commonLoggers.interactionSendError('toggleWLStatus.ts@dmFail', interaction, e));
return;
}
}
bot.helpers.editMessage(evtChannelId, evtMessageId, { bot.helpers.editMessage(evtChannelId, evtMessageId, {
components: [{ components: [{
type: MessageComponentTypes.ActionRow, type: MessageComponentTypes.ActionRow,
@ -32,7 +50,7 @@ const execute = async (bot: Bot, interaction: Interaction) => {
description: `This event is now ${makeWhitelisted ? 'whitelisted, meaning you will have to approve join requests from all future members' : 'public'}.\n\n${safelyDismissMsg}`, 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: Error) => utils.commonLoggers.interactionSendError('toggleWLStatus.ts@dmSuccess', interaction, e))
).catch((e) => { ).catch((e) => {
utils.commonLoggers.messageEditError('toggleWLStatus.ts', 'toggleWLStatusFailed', e); utils.commonLoggers.messageEditError('toggleWLStatus.ts', 'toggleWLStatusFailed', e);
somethingWentWrong(bot, interaction, 'editFailedInToggleWLStatusButton'); somethingWentWrong(bot, interaction, 'editFailedInToggleWLStatusButton');

View File

@ -80,3 +80,11 @@ Ran into a bug? Report it to my developers using \`/${reportSlashName} [issue d
text: `Current Version: ${config.version}`, text: `Current Version: ${config.version}`,
}, },
}; };
export const dmTestMessage: CreateMessage = {
embeds: [{
color: infoColor2,
title: 'Heyo! Just making sure I can reach you.',
description: 'This message is just to make sure I can DM you any Join Requests to your event. If you are reading this message, it means you have everything set up correctly.',
}],
};