From 33d9770b5f50b74d2f0ed7f2300a69ad25cf34b4 Mon Sep 17 00:00:00 2001 From: "Ean Milligan (Bastion)" Date: Thu, 4 May 2023 05:24:26 -0400 Subject: [PATCH] V1.0.1 - Fix setup permission issue, sonar fixes --- config.example.ts | 2 +- src/buttons/event-creation/step1-gameSelection.ts | 4 ++-- .../event-creation/step1b-verifyCustomActivity.ts | 6 +++--- src/buttons/event-creation/step2-finalize.ts | 8 ++++---- src/buttons/live-event/applyDateTime.ts | 2 +- src/buttons/live-event/applyDescription.ts | 6 +++--- src/buttons/live-event/editActivity.ts | 10 +++++----- src/buttons/live-event/leaveViaDM.ts | 2 +- src/buttons/tokenCleanup.ts | 6 +++--- src/commands/managerJLA.ts | 4 ++-- src/commands/setup.ts | 11 ++++++----- src/notificationSystem.ts | 8 ++++---- 12 files changed, 35 insertions(+), 34 deletions(-) diff --git a/config.example.ts b/config.example.ts index cfec9c7..12603e5 100644 --- a/config.example.ts +++ b/config.example.ts @@ -1,6 +1,6 @@ export const config = { 'name': 'Group Up', // Name of the bot - 'version': '1.0.0', // Version of the bot + 'version': '1.0.1', // Version of the bot 'token': 'the_bot_token', // Discord API Token for this bot 'localToken': 'local_testing_token', // Discord API Token for a secondary OPTIONAL testing bot, THIS MUST BE DIFFERENT FROM "token" 'prefix': '/', // Prefix for all commands diff --git a/src/buttons/event-creation/step1-gameSelection.ts b/src/buttons/event-creation/step1-gameSelection.ts index a5e66b6..f5ec4c1 100644 --- a/src/buttons/event-creation/step1-gameSelection.ts +++ b/src/buttons/event-creation/step1-gameSelection.ts @@ -49,7 +49,7 @@ const execute = async (bot: Bot, interaction: Interaction) => { let prefillTimeZone = ''; let prefillDate = ''; let prefillDescription = ''; - if (interaction.message && interaction.message.embeds[0].fields && interaction.message.embeds[0].fields[LfgEmbedIndexes.StartTime].name === lfgStartTimeName) { + if (interaction.message?.embeds[0].fields && interaction.message.embeds[0].fields[LfgEmbedIndexes.StartTime].name === lfgStartTimeName) { if (interaction.message.embeds[0].fields[LfgEmbedIndexes.StartTime].value !== invalidDateTimeStr) { let rawEventDateTime = interaction.message.embeds[0].fields[LfgEmbedIndexes.StartTime].value.split('\n')[0].split(' '); const monthIdx = rawEventDateTime.findIndex((item) => monthsShort.includes(item.toUpperCase())); @@ -103,7 +103,7 @@ const execute = async (bot: Bot, interaction: Interaction) => { }); selectMenus.push(generateCustomEventRow(prefillArray.length ? prefillArray[0] : '', prefillArray.length > 1 ? prefillArray[prefillArray.length - 1] : '')); - if (interaction.data.customId && interaction.data.customId.includes(fillerChar)) { + if (interaction.data.customId?.includes(fillerChar)) { // Let discord know we didn't ignore the user await bot.helpers.sendInteractionResponse(interaction.id, interaction.token, { type: InteractionResponseTypes.DeferredUpdateMessage, diff --git a/src/buttons/event-creation/step1b-verifyCustomActivity.ts b/src/buttons/event-creation/step1b-verifyCustomActivity.ts index 9a8e772..8cbd9e0 100644 --- a/src/buttons/event-creation/step1b-verifyCustomActivity.ts +++ b/src/buttons/event-creation/step1b-verifyCustomActivity.ts @@ -22,9 +22,9 @@ const execute = async (bot: Bot, interaction: Interaction) => { } // Remove any pipe characters to avoid issues down the process - const activityTitle = (tempDataMap.get(activityTitleId) || '').replace(/\|/g, ''); - const activitySubtitle = (tempDataMap.get(activitySubtitleId) || '').replace(/\|/g, ''); - const activityMaxPlayers = parseInt(tempDataMap.get(activityMaxPlayersId) || '0'); + const activityTitle = (tempDataMap.get(activityTitleId) ?? '').replace(/\|/g, ''); + const activitySubtitle = (tempDataMap.get(activitySubtitleId) ?? '').replace(/\|/g, ''); + const activityMaxPlayers = parseInt(tempDataMap.get(activityMaxPlayersId) ?? '0'); if (isNaN(activityMaxPlayers) || activityMaxPlayers < 1 || activityMaxPlayers > 99) { bot.helpers.sendInteractionResponse(interaction.id, interaction.token, { type: InteractionResponseTypes.ChannelMessageWithSource, diff --git a/src/buttons/event-creation/step2-finalize.ts b/src/buttons/event-creation/step2-finalize.ts index 3d4cc54..6a1403a 100644 --- a/src/buttons/event-creation/step2-finalize.ts +++ b/src/buttons/event-creation/step2-finalize.ts @@ -55,10 +55,10 @@ const execute = async (bot: Bot, interaction: Interaction) => { ); } - const rawEventTime = tempDataMap.get(eventTimeId) || ''; - const rawEventTimeZone = tempDataMap.get(eventTimeZoneId) || ''; - const rawEventDate = tempDataMap.get(eventDateId) || ''; - const eventDescription = tempDataMap.get(eventDescriptionId) || noDescProvided; + const rawEventTime = tempDataMap.get(eventTimeId) ?? ''; + const rawEventTimeZone = tempDataMap.get(eventTimeZoneId) ?? ''; + const rawEventDate = tempDataMap.get(eventDateId) ?? ''; + const eventDescription = tempDataMap.get(eventDescriptionId) ?? noDescProvided; if (!rawEventTime || !rawEventTimeZone || !rawEventDate) { // Error out if user somehow failed to provide one of the fields (eventDescription is allowed to be null/empty) somethingWentWrong(bot, interaction, `missingFieldFromEventDescription@${rawEventTime}_${rawEventTimeZone}_${rawEventDate}`); diff --git a/src/buttons/live-event/applyDateTime.ts b/src/buttons/live-event/applyDateTime.ts index 02bd1a8..e452385 100644 --- a/src/buttons/live-event/applyDateTime.ts +++ b/src/buttons/live-event/applyDateTime.ts @@ -55,7 +55,7 @@ const execute = async (bot: Bot, interaction: Interaction) => { return; } - if (eventMessage && eventMessage.embeds[0].fields) { + if (eventMessage?.embeds[0].fields) { eventMessage.embeds[0].fields[LfgEmbedIndexes.StartTime].value = generateTimeFieldStr(eventDateTimeStr, eventDateTime); const tIdx = eventMessage.embeds[0].fields[LfgEmbedIndexes.ICSLink].value.indexOf('?t=') + 3; const nIdx = eventMessage.embeds[0].fields[LfgEmbedIndexes.ICSLink].value.indexOf('&n='); diff --git a/src/buttons/live-event/applyDescription.ts b/src/buttons/live-event/applyDescription.ts index a2d52ed..ca85d1b 100644 --- a/src/buttons/live-event/applyDescription.ts +++ b/src/buttons/live-event/applyDescription.ts @@ -17,13 +17,13 @@ const execute = async (bot: Bot, interaction: Interaction) => { for (const row of interaction.data.components) { if (row.components?.[0]) { const textField = row.components[0]; - tempDataMap.set(textField.customId || 'missingCustomId', textField.value || ''); + tempDataMap.set(textField.customId || 'missingCustomId', textField.value || noDescProvided); } } const newDescription = tempDataMap.get(eventDescriptionId); - if (eventMessage && eventMessage.embeds[0].fields) { - eventMessage.embeds[0].fields[LfgEmbedIndexes.Description].value = newDescription || noDescProvided; + if (eventMessage?.embeds[0].fields) { + eventMessage.embeds[0].fields[LfgEmbedIndexes.Description].value = newDescription ?? noDescProvided; // Send edit confirmation addTokenToMap(bot, interaction, interaction.guildId, interaction.channelId, interaction.member.id); diff --git a/src/buttons/live-event/editActivity.ts b/src/buttons/live-event/editActivity.ts index b3926bd..4f2983e 100644 --- a/src/buttons/live-event/editActivity.ts +++ b/src/buttons/live-event/editActivity.ts @@ -65,9 +65,9 @@ const execute = async (bot: Bot, interaction: Interaction) => { } // Remove any pipe characters to avoid issues down the process - const activityTitle = (tempDataMap.get(activityTitleId) || '').replace(/\|/g, ''); - const activitySubtitle = (tempDataMap.get(activitySubtitleId) || '').replace(/\|/g, ''); - const activityMaxPlayers = parseInt(tempDataMap.get(activityMaxPlayersId) || '0'); + const activityTitle = (tempDataMap.get(activityTitleId) ?? '').replace(/\|/g, ''); + const activitySubtitle = (tempDataMap.get(activitySubtitleId) ?? '').replace(/\|/g, ''); + const activityMaxPlayers = parseInt(tempDataMap.get(activityMaxPlayersId) ?? '0'); if (isNaN(activityMaxPlayers) || activityMaxPlayers < 1 || activityMaxPlayers > 99) { bot.helpers.sendInteractionResponse(interaction.id, interaction.token, { type: InteractionResponseTypes.ChannelMessageWithSource, @@ -114,7 +114,7 @@ const execute = async (bot: Bot, interaction: Interaction) => { // Get event to apply edit const eventMessage = await bot.helpers.getMessage(evtChannelId, evtMessageId).catch((e: Error) => utils.commonLoggers.messageGetError('editActivity.ts', 'get eventMessage', e)); - if (eventMessage && eventMessage.embeds[0].fields) { + if (eventMessage?.embeds[0].fields) { await deleteTokenEarly(bot, interaction, interaction.guildId, interaction.channelId, interaction.member.id); // Update member lists const [currentMemberCount, _oldMaxMemberCount] = getEventMemberCount(eventMessage.embeds[0].fields[LfgEmbedIndexes.JoinedMembers].name); @@ -173,7 +173,7 @@ const execute = async (bot: Bot, interaction: Interaction) => { selectMenus.push(makeCustomEventRow(interaction.data.customId.replaceAll(fillerChar, '').split(idSeparator)[1] || '')); - if (interaction.data.customId && interaction.data.customId.includes(fillerChar)) { + if (interaction.data.customId.includes(fillerChar)) { // Let discord know we didn't ignore the user await bot.helpers.sendInteractionResponse(interaction.id, interaction.token, { type: InteractionResponseTypes.DeferredUpdateMessage, diff --git a/src/buttons/live-event/leaveViaDM.ts b/src/buttons/live-event/leaveViaDM.ts index 195244b..bf65f78 100644 --- a/src/buttons/live-event/leaveViaDM.ts +++ b/src/buttons/live-event/leaveViaDM.ts @@ -15,7 +15,7 @@ const execute = async (bot: Bot, interaction: Interaction) => { const [evtGuildId, evtChannelId, evtMessageId] = (interaction.data.customId.replaceAll(pathIdxEnder, '').split(idSeparator)[1] || '').split(pathIdxSeparator).map((id) => BigInt(id || '0')); const eventMessage = await bot.helpers.getMessage(evtChannelId, evtMessageId).catch((e: Error) => utils.commonLoggers.messageGetError('editActivity.ts', 'get eventMessage', e)); - if (eventMessage && eventMessage.embeds[0]) { + if (eventMessage?.embeds[0]) { // Remove user from event removeMemberFromEvent(bot, interaction, eventMessage.embeds[0], evtMessageId, evtChannelId, interaction.user.id, evtGuildId, true); } else { diff --git a/src/buttons/tokenCleanup.ts b/src/buttons/tokenCleanup.ts index a1395e0..51b3e3b 100644 --- a/src/buttons/tokenCleanup.ts +++ b/src/buttons/tokenCleanup.ts @@ -19,7 +19,7 @@ export const addTokenToMap = (bot: Bot, interaction: Interaction, guildId: bigin token: interaction.token, timeoutId: setTimeout( (guildId, channelId, userId) => { - deleteTokenEarly(bot, interaction, guildId, channelId, userId); + deleteTokenEarly(bot, interaction, guildId, channelId, userId).catch((e) => utils.commonLoggers.interactionSendError('tokenCleanup.ts:addTokenToMap', interaction, e)); }, tokenTimeoutMS, guildId, @@ -30,8 +30,8 @@ export const addTokenToMap = (bot: Bot, interaction: Interaction, guildId: bigin 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) => + if (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); diff --git a/src/commands/managerJLA.ts b/src/commands/managerJLA.ts index 9ac394d..468812b 100644 --- a/src/commands/managerJLA.ts +++ b/src/commands/managerJLA.ts @@ -61,8 +61,8 @@ const execute = async (bot: Bot, interaction: Interaction) => { for (const option of interaction.data.options[0].options) { tempDataMap.set(option.name || 'missingCustomId', option.value as string || ''); } - const eventLink = tempDataMap.get(eventLinkName) || ''; - const userToAdd = BigInt(tempDataMap.get(userName) || '0'); + const eventLink = tempDataMap.get(eventLinkName) ?? ''; + const userToAdd = BigInt(tempDataMap.get(userName) ?? '0'); const eventIds = utils.messageUrlToIds(eventLink); // Verify fields exist diff --git a/src/commands/setup.ts b/src/commands/setup.ts index ece6c1e..564d07a 100644 --- a/src/commands/setup.ts +++ b/src/commands/setup.ts @@ -10,7 +10,6 @@ import { DiscordEmbedField, Interaction, InteractionResponseTypes, - LT, log, MessageComponentTypes, OverwriteTypes, } from '../../deps.ts'; @@ -110,7 +109,7 @@ const execute = async (bot: Bot, interaction: Interaction) => { name: 'Editing/Deleting your event:', value: 'To edit or delete your event, simply click on the ✏️ or 🗑️ buttons respectively.', }]; - const sendMessagePermSting = '`SEND_MESSAGES`\n`VIEW_CHANNEL`\n`EMBED_LINKS`' + const sendMessagePermSting = '`SEND_MESSAGES`\n`VIEW_CHANNEL`\n`EMBED_LINKS`'; const permissionFields: Array = [ { name: `Please make sure ${config.name} has the following permissions for the current channel:`, @@ -258,7 +257,7 @@ The Discord Slash Command system will ensure you provide all the required detail // Retrofit all old LFG posts that we found oldLfgMsgs.forEach((oldEventId) => { const oldEvent = messages.get(oldEventId); - if (oldEvent && oldEvent.embeds[0].fields && oldEvent.embeds[0].footer) { + if (oldEvent?.embeds[0].fields && oldEvent.embeds[0].footer) { const eventMembers = [...getLfgMembers(oldEvent.embeds[0].fields[LfgEmbedIndexes.JoinedMembers].value), ...getLfgMembers(oldEvent.embeds[0].fields[LfgEmbedIndexes.AlternateMembers].value)]; const eventDateTime = new Date(parseInt((oldEvent.embeds[0].fields[LfgEmbedIndexes.StartTime].value.split('tz#')[1] || ' ').slice(0, -1))); if (!isNaN(eventDateTime.getTime())) { @@ -339,10 +338,12 @@ The Discord Slash Command system will ensure you provide all the required detail } else { // Clean up DB and map lfgChannelSettings.delete(lfgChannelSettingKey); - await dbClient.execute('DELETE FROM guild_settings WHERE guildId = ? AND lfgChannelId = ?', [interaction.guildId, interaction.channelId]).catch((e) => utils.commonLoggers.dbError('setup.ts', 'delete guild/lfgChannel', e)); + await dbClient.execute('DELETE FROM guild_settings WHERE guildId = ? AND lfgChannelId = ?', [interaction.guildId, interaction.channelId]).catch((e) => + utils.commonLoggers.dbError('setup.ts', 'delete guild/lfgChannel', e) + ); if (introMsg) { - bot.helpers.deleteMessage(interaction.channelId, introMsg.id, 'embed missing').catch((e) => utils.commonLoggers.messageDeleteError('setup.ts', 'embed missing cleanup', e)) + bot.helpers.deleteMessage(interaction.channelId, introMsg.id, 'embed missing').catch((e) => utils.commonLoggers.messageDeleteError('setup.ts', 'embed missing cleanup', e)); } // Could not send initial message diff --git a/src/notificationSystem.ts b/src/notificationSystem.ts index 89da3ba..1e5d648 100644 --- a/src/notificationSystem.ts +++ b/src/notificationSystem.ts @@ -128,7 +128,7 @@ export const notifyEventMembers = async (bot: Bot, event: ActiveEvent, secondTry dbClient.execute(queries.updateEventFlags(1, 0), [event.channelId, event.messageId]).catch((e) => utils.commonLoggers.dbError('notificationSystem.ts@notifySuccess', 'update event in', e)); return true; } else { - if (!secondTry) loudLogFailure(bot, event, notifyStepName); + if (!secondTry) loudLogFailure(bot, event, notifyStepName).catch((e) => utils.commonLoggers.messageSendError('notificationSystem.ts@notify', 'loudLog Failed', e)); // Update DB to indicate notifications have not been sent out dbClient.execute(queries.updateEventFlags(-1, 0), [event.channelId, event.messageId]).catch((e) => utils.commonLoggers.dbError('notificationSystem.ts@notifyFail', 'update event in', e)); return false; @@ -182,7 +182,7 @@ export const lockEvent = async (bot: Bot, event: ActiveEvent, secondTry = false) dbClient.execute(queries.updateEventFlags(1, 1), [event.channelId, event.messageId]).catch((e) => utils.commonLoggers.dbError('notificationSystem.ts@lockSuccess', 'update event in', e)); return true; } else { - if (!secondTry) loudLogFailure(bot, event, lockStepName); + if (!secondTry) loudLogFailure(bot, event, lockStepName).catch((e) => utils.commonLoggers.messageSendError('notificationSystem.ts@lock', 'loudLog Failed', e)); // Update DB to indicate event has not been locked dbClient.execute(queries.updateEventFlags(1, -1), [event.channelId, event.messageId]).catch((e) => utils.commonLoggers.dbError('notificationSystem.ts@lockFail', 'update event in', e)); return false; @@ -202,7 +202,7 @@ export const deleteEvent = async (bot: Bot, event: ActiveEvent, secondTry = fals dbClient.execute(queries.deleteEvent, [event.channelId, event.messageId]).catch((e) => utils.commonLoggers.dbError('notificationSystem.ts@deleteSuccess', 'delete event from', e)); return true; } else { - if (!secondTry) loudLogFailure(bot, event, deleteStepName); + if (!secondTry) loudLogFailure(bot, event, deleteStepName).catch((e) => utils.commonLoggers.messageSendError('notificationSystem.ts@delete', 'loudLog Failed', e)); // Update DB to indicate delete failed dbClient.execute(queries.updateEventFlags(-1, -1), [event.channelId, event.messageId]).catch((e) => utils.commonLoggers.dbError('notificationSystem.ts@deleteFail', 'update event in', e)); return false; @@ -232,7 +232,7 @@ export const handleFailures = async (bot: Bot, event: ActiveEvent) => { if (!rerunSuccess) { // Failed at completing a step! Event may have been deleted? - loudLogFailure(bot, event, stepName, true); + loudLogFailure(bot, event, stepName, true).catch((e) => utils.commonLoggers.messageSendError('notificationSystem.ts@rerun', 'loudLog Failed', e)); dbClient.execute(queries.deleteEvent, [event.channelId, event.messageId]).catch((e) => utils.commonLoggers.dbError('notificationSystem.ts@handleFailures', 'delete event from', e)); } };