Skip to content

Commit

Permalink
Merge pull request #1147 from givepraise/add/disable-bot-notifications
Browse files Browse the repository at this point in the history
Add setting to enable/disable bot notifications when sending praise.
  • Loading branch information
kristoferlund authored Dec 6, 2023
2 parents 52b421b + 850014e commit 0517f80
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 0517f80

Please sign in to comment.