Skip to content

Commit

Permalink
Add setting to enable/disable bot notifications
Browse files Browse the repository at this point in the history
when sending praise.
  • Loading branch information
kristoferlund committed Dec 6, 2023
1 parent f302161 commit 850014e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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<void> => {
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<void> => {
const allKeys = settings.map((s) => s.key);
await SettingModel.deleteMany({ key: { $in: allKeys } });
};

export { up, down };
41 changes: 24 additions & 17 deletions packages/discord-bot/src/utils/givePraise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];

Expand Down

0 comments on commit 850014e

Please sign in to comment.