From 850014eddf8897be4328af4cb9f26e2d23cea19b Mon Sep 17 00:00:00 2001 From: Kristofer Date: Wed, 6 Dec 2023 16:06:28 +0100 Subject: [PATCH] Add setting to enable/disable bot notifications when sending praise. --- ...iscord_bot_praise_notifications_enabled.ts | 36 ++++++++++++++++ packages/discord-bot/src/utils/givePraise.ts | 41 +++++++++++-------- 2 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 packages/api/src/database/migrations/42_settings_add_discord_bot_praise_notifications_enabled.ts diff --git a/packages/api/src/database/migrations/42_settings_add_discord_bot_praise_notifications_enabled.ts b/packages/api/src/database/migrations/42_settings_add_discord_bot_praise_notifications_enabled.ts new file mode 100644 index 000000000..3ed4af6d8 --- /dev/null +++ b/packages/api/src/database/migrations/42_settings_add_discord_bot_praise_notifications_enabled.ts @@ -0,0 +1,36 @@ +import { SettingGroup } from '../../settings/enums/setting-group.enum'; +import { SettingModel } from '../schemas/settings/23_settings.schema'; + +const settings = [ + { + key: 'DISCORD_BOT_PRAISE_NOTIFICATIONS_ENABLED', + value: true, + defaultValue: true, + type: 'Boolean', + periodOverridable: false, + label: 'Praise notifications enabled', + description: + 'Unchecking this will disable all bot notifications from the Discord bot to individual users when praising.', + group: SettingGroup.DISCORD, + subgroup: 1, + }, +]; + +const up = async (): Promise => { + const settingUpdates = settings.map((s) => ({ + updateOne: { + filter: { key: s.key }, + update: { $setOnInsert: { ...s } }, // Insert setting if not found, otherwise continue + upsert: true, + }, + })) as any; + + await SettingModel.bulkWrite(settingUpdates); +}; + +const down = async (): Promise => { + const allKeys = settings.map((s) => s.key); + await SettingModel.deleteMany({ key: { $in: allKeys } }); +}; + +export { up, down }; diff --git a/packages/discord-bot/src/utils/givePraise.ts b/packages/discord-bot/src/utils/givePraise.ts index 5e990d8ff..7a9b913f2 100644 --- a/packages/discord-bot/src/utils/givePraise.ts +++ b/packages/discord-bot/src/utils/givePraise.ts @@ -103,23 +103,30 @@ export const givePraise = async ( ? process.env?.FRONTEND_URL || 'undefined:/' : `https://${host}`; - await Promise.all( - praiseItems.map(async (praise) => { - await sendReceiverDM( - praise._id, - receivers.filter( - (receiver) => - receiver.userAccount.accountId === praise.receiver.accountId - )[0], - member, - reason, - responseUrl, - host, - hostUrl, - interaction.channelId - ); - }) - ); + const praiseNotificationsEnabled = (await getSetting( + 'DISCORD_BOT_PRAISE_NOTIFICATIONS_ENABLED', + host + )) as boolean; + + if (praiseNotificationsEnabled) { + await Promise.all( + praiseItems.map(async (praise) => { + await sendReceiverDM( + praise._id, + receivers.filter( + (receiver) => + receiver.userAccount.accountId === praise.receiver.accountId + )[0], + member, + reason, + responseUrl, + host, + hostUrl, + interaction.channelId + ); + }) + ); + } const warningMsgParts: string[] = [];